\ProvidesPackage{mqformula}

\def\descwidth{0.3\textwidth}
\def\eqwidth{0.69\textwidth}

\RequirePackage{mqfqname}
\RequirePackage{mqconstant}
\RequirePackage{mqquantity}

%
% FORMULA ENVIRONMENT
% The following commands are meant to be used with the formula environment
%

% Name in black and below description in gray
% [1]: minipage width
% 2: fqname of name
% 3: fqname of a translation that holds the explanation
\newcommand{\NameWithDescription}[3][\descwidth]{
    \begin{minipage}{#1}
        \IfTranslationExists{#2}{
            \raggedright
            \GT{#2}
        }{\detokenize{#2}}
        \IfTranslationExists{#3}{
            \\ {\color{fg1} \GT{#3}}
        }{}
    \end{minipage}
}


% TODO: rename
\newsavebox{\contentBoxBox}
% [1]: minipage width
% 2: fqname of a translation that holds the explanation
\newenvironment{ContentBoxWithExplanation}[2][\eqwidth]{
    \def\ContentFqName{#2}
    \begin{lrbox}{\contentBoxBox}
    \begin{minipage}{#1}
}{
        \IfTranslationExists{\ContentFqName}{%
            \smartnewline%
            \noindent%
            \begingroup%
                \color{fg1}%
                \raggedright%
                \GT{\ContentFqName}%
            \endgroup%
        }{}
    \end{minipage}
    \end{lrbox}
    \fbox{\usebox{\contentBoxBox}}
}


% Class defining commands shared by all formula environments
% 1: key
\newenvironment{formulainternal}[1]{
    \mqfqname@enter{#1}
    % [1]: language
    % 2: name
    % 3: description
    % 4: definitions/links
    \newcommand{\desc}[4][english]{
        % language, name, description, definitions
        \ifblank{##2}{}{\dt{##1}{##2}}
        \ifblank{##3}{}{\dt[desc]{##1}{##3}}
        \ifblank{##4}{}{\dt[defs]{##1}{##4}}
    }
    \directlua{n_formulaEntries = 0}

    % makes this formula referencable with \abbrRef{<name>}
    % [1]: label to use
    % 2: Abbreviation to use for references
    \newcommand{\abbrLabel}[2][#1]{
        \abbrLink[\fqname]{##1}{##2}
    }
    % makes this formula referencable with \absRef{<name>}
    % [1]: label to use
    \newcommand{\absLabel}[1][#1]{
        \absLink[\fqname]{\fqname}{##1}
    }

    \newcommand{\newFormulaEntry}{
        \directlua{
            if n_formulaEntries > 0 then
            tex.print("\\vspace{0.3\\baselineskip}\\hrule\\vspace{0.3\\baselineskip}")
            end
            n_formulaEntries = n_formulaEntries + 1
        }
        % \par\noindent\ignorespaces
    }
    % 1: equation for align environment
    \newcommand{\eq}[1]{
        \newFormulaEntry
        \begin{align}
            % \label{eq:\fqname:#1}
            ##1
        \end{align}
    }
    % 1: equation for flalign environment
    \newcommand{\eqFLAlign}[2]{
        \newFormulaEntry
        \begin{alignat}{##1}%
            % dont place label when one is provided
            % \IfSubStringInString{label}\unexpanded{#3}{}{
            %     \label{eq:#1}
            % }
            ##2%
        \end{alignat}
    }
    \newcommand{\fig}[1]{
        \newFormulaEntry
        \centering
        \includegraphics{##1}
    }
    % 1: content for the ttext environment
    \newcommand{\ttxt}[2][text]{
        \newFormulaEntry
        \begin{ttext}[##1]
            ##2
        \end{ttext}
    }
    % 1: symbol
    % 2: units
    % 3: comment key to translation
    \newcommand{\quantity}[3]{%
        \quantity@new[\fqname]{#1}{##1}{##2}{##3}
        \newFormulaEntry
        \quantity@print{#1}
    }
    \newcommand{\hiddenQuantity}[3]{%
        \quantity@new[\fqname]{#1}{##1}{##2}{##3}
    }

    % must be used only in third argument of "constant" command
    % 1: value
    % 2: unit
    \newcommand{\val}[2]{
        \constant@addValue{#1}{##1}{##2}
    }
    % 1: symbol
    % 2: either exp or def; experimentally or defined constant
    % 3: one or more \val{value}{unit} commands
    \newcommand{\constant}[3]{
        \constant@new[\fqname]{#1}{##1}{##2}
        \begingroup
            ##3
        \endgroup
        \newFormulaEntry
        \constant@print{#1}
    }

    \newcommand{\fsplit}[3][0.5]{
        \begin{minipage}{##1\linewidth}
            ##2
        \end{minipage}
        \begin{minipage}{\luavar{0.99-##1}\linewidth}
            ##3
        \end{minipage}
    }
}{
    \mqfqname@leave
}

\newenvironment{formula}[1]{
    \begin{formulainternal}{#1}

    \begingroup
        \mqfqname@label
        \par\noindent\ignorespaces
        % \textcolor{gray}{\hrule}
        % \vspace{0.5\baselineskip}
        \NameWithDescription[\descwidth]{\fqname}{\fqname:desc}
        \hfill
        \begin{ContentBoxWithExplanation}{\fqname:defs}
}{
        \end{ContentBoxWithExplanation}
    \endgroup
    \separateEntries
    % \textcolor{fg3}{\hrule}
    % \vspace{0.5\baselineskip}
    \ignorespacesafterend
    \end{formulainternal}
}


% BIG FORMULA
\newenvironment{bigformula}[1]{
    \begin{formulainternal}{#1}

    \par\noindent
    \begin{minipage}{\textwidth}  % using a minipage to now allow line breaks within the bigformula
        \mqfqname@label
        \par\noindent\ignorespaces
        % \textcolor{gray}{\hrule}
        % \vspace{0.5\baselineskip}
        \textbf{
            \raggedright
            \GT{\fqname}
        }
        \IfTranslationExists{\fqname:desc}{
               : {\color{fg1} \GT{\fqname:desc}}
        }{}
        \hfill
        \par
}{
        \IfTranslationExists{\fqname:defs}{%
            \smartnewline
            \noindent
            \begingroup
                \color{fg1} 
                \GT{\fqname:defs}
                % \edef\temp{\GT{#1_defs}}
                % \expandafter\StrSubstitute\expandafter{\temp}{:}{\\}
            \endgroup
        }{}
    \end{minipage}
    \separateEntries
    % \textcolor{fg3}{\hrule}
    % \vspace{0.5\baselineskip}
    \ignorespacesafterend
    \end{formulainternal}
}

\newenvironment{hiddenformula}[1]{
    \begin{formulainternal}{#1}
    \renewcommand{\eq}[1]{}
    \renewcommand{\eqFLAlign}[2]{}
    \renewcommand{\fig}[2][1.0]{}
    \renewcommand{\ttxt}[2][#1:desc]{}
    % 1: symbol
    % 2: units
    % 3: comment key to translation
    \renewcommand{\quantity}[3]{%
        \quantity@new[\fqname]{#1}{##1}{##2}{##3}
    }
    % 1: symbol
    % 2: either exp or def; experimentally or defined constant
    % 3: one or more \val{value}{unit} commands
    \renewcommand{\constant}[3]{
        \constant@new[\fqname]{#1}{##1}{##2}
        \begingroup
            ##3
        \endgroup
    }
}{
    \end{formulainternal}
}
