\ExplSyntaxOn

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Optionen für den Aufgabenpool
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\clist_new:N \g__schule_aufgabenoptionen_clist

\DeclareDocumentCommand{\ifAufgabenpoolOptionTF}{m +m +m}{
    \clist_if_in:NnTF \g__schule_aufgabenoptionen_clist {#1} {#2} {#3}
}

\DeclareDocumentCommand{\ifAufgabenpoolOptionT}{m +m}{
    \clist_if_in:NnT \g__schule_aufgabenoptionen_clist {#1} {#2}
}

\DeclareDocumentCommand{\ifAufgabenpoolOptionF}{m +m}{
    \clist_if_in:NnF \g__schule_aufgabenoptionen_clist {#1} {#2}
}

\DeclareExerciseProperty-{poolOptionen}

\DeclareDocumentCommand{\setAufgabenpoolOptionen}{m}{
    \clist_set:Nn \g__schule_aufgabenoptionen_clist {#1}%
}

\DeclareDocumentCommand{\set@AufgabenpoolOptionen@Property}{}{
    \exp_args:Nnf\SetExerciseProperty{poolOptionen}{\clist_use:Nn \g__schule_aufgabenoptionen_clist {,}}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Basedir Elemente
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\str_new:N \g__schule_basedir_string
\str_gset:Nn \g__schule_basedir_string {.}

\DeclareExerciseProperty-{basedir}

\DeclareDocumentCommand{\setBasedir}{}{
    \exp_args:Nnf\SetExerciseProperty{basedir}{\str_use:N \g__schule_basedir_string}%
}

\DeclareDocumentCommand{\getBasedir}{}{
    \str_use:N \g__schule_basedir_string
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Aufgabenhinweise
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\bool_new:N \g__schule_inAufgabenInput_bool
\bool_gset_false:N \g__schule_inAufgabenInput_bool
\NewDocumentEnvironment{aufgabenpoolHinweis}{+b}{
     \bool_if:NF \g__schule_inAufgabenInput_bool {#1}
}{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Aufgabeneinbindung
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\DeclareDocumentCommand{\aufgabeninput}{o m m}{
    \bool_gset_true:N \g__schule_inAufgabenInput_bool
    \str_gset:Nn \g__schule_basedir_string {#2}
    \clist_set:Nn \g__schule_aufgabenoptionen_clist {#1}
    \input{#2/#3}
    \bool_gset_false:N \g__schule_inAufgabenInput_bool
}

\xsimsetup{
  aufgabe/begin-hook = \setBasedir\set@AufgabenpoolOptionen@Property,
  loesung/begin-hook = \clist_set:Nx \g__schule_aufgabenoptionen_clist {\GetExerciseProperty{poolOptionen}} \str_gset:Nx \g__schule_basedir_string {\GetExerciseProperty{basedir}},
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% InputOnce
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\clist_new:N \g__schule_inputonce_clist
\clist_new:N \g__schule_inputonceAppendix_clist
\prop_new:N \g__schule_appendix_prop
\prop_new:N \g__schule_appendixBasedir_prop

\DeclareDocumentCommand{\inputOnce}{m}{
    \clist_if_in:NnF \g__schule_inputonce_clist {#1} {
        \clist_gput_right:Nn \g__schule_inputonce_clist {#1}
        \input{\getBasedir/#1}
    }
}

\DeclareDocumentCommand{\inputOnceAppendix}{m}{
    \exp_args:Nnf \clist_if_in:NnF {\g__schule_inputonceAppendix_clist} {\getBasedir/#1} {
        \exp_args:Nnf \clist_gput_right:Nx {\g__schule_inputonceAppendix_clist} {\getBasedir/#1}
        \bool_if:NF \g__schule_inAufgabenInput_bool {\input{\getBasedir/#1}}
    }
}

\DeclareDocumentCommand{\showIncludeAppendix}{}{
    \clist_map_inline:Nn \g__schule_inputonceAppendix_clist {
        \input{##1}
    }
    \prop_map_inline:Nn \g__schule_appendix_prop {
        \exp_args:Nnf \str_gset:Nn \g__schule_basedir_string {\prop_item:Nn \g__schule_appendixBasedir_prop {##1}}
        ##2
    }
}

\NewDocumentEnvironment{onceAppendix}{m +b}{
    \prop_gput:Nnn \g__schule_appendix_prop {#1} {#2}
    \exp_args:Nnnf \prop_gput:Nnn {\g__schule_appendixBasedir_prop} {#1} {\getBasedir}
    \bool_if:NF \g__schule_inAufgabenInput_bool {
        #2
    }
}{}

\ExplSyntaxOff
