%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright © 2021 Maximilian Keßler
% 
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the “Software”), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
% 
% THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
% SOFTWARE.
% 
% This package has been generated by PyTeX, available at
%    https://github.com/kesslermaximilian/PyTeX
% and built from source file 'fancythm.pysty'.
% It is STRONGLY DISCOURAGED to edit this source file directly, since local
% changes will not be versioned by Git and overwritten by the next build. Always
% edit the source file and build the package again.
% 
% Build details:
%   PyTeX version: v1.0.0-18 (commit 126d420)
%   Source code version: v2.4.0-beta-4 (commit 4aa3ce8)
% 
% This LaTeX package is free software and distributed under the MIT License. You
% may use it freely for your purposes. The latest version of the package can be
% obtained via GitHub under
%    https://github.com/kesslermaximilian/LatexPackages
% For further information see the url above.
% Reportings of bugs, suggestions and improvements are welcome, see the README
% at the Git repository for further information.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mkessler-fancythm}[2021/12/12 - Theorem-Environment Package]


%Provides fancy theorem-like-environments used in the write-ups of my lecture notes

\RequirePackage{xkeyval}

\newif\ifmkessler@fancythm@showstars\mkessler@fancythm@showstarstrue
\newif\ifmkessler@fancythm@showdaggers\mkessler@fancythm@showdaggerstrue
\newif\ifmkessler@fancythm@includestars\mkessler@fancythm@includestarstrue
\newif\ifmkessler@fancythm@includeoral\mkessler@fancythm@includeoraltrue
\newif\ifmkessler@fancythm@markoral\mkessler@fancythm@markoraltrue

\newif\ifmkessler@fancythm@lecturenumbers\mkessler@fancythm@lecturenumberstrue
\newif\ifmkessler@fancythm@numbersmallenvironmentswiththeorem\mkessler@fancythm@numbersmallenvironmentswiththeoremfalse
\newif\ifmkessler@fancythm@numbersmallenvironments\mkessler@fancythm@numbersmallenvironmentsfalse
\newif\ifmkessler@fancythm@defaulttheorems\mkessler@fancythm@defaulttheoremstrue

\define@choicekey*{mkessler-fancythm}{mkessler@fancythm@numbersmallenvironments}[\val\nr]{no,section,theorem}{%
    \ifcase\nr\relax
    \mkessler@fancythm@numbersmallenvironmentsfalse
    \or
    \mkessler@fancythm@numbersmallenvironmentstrue
        %numbering small environments within sections is forbidden whilst using lecturenumbes
        \ifmkessler@fancythm@lecturenumbers
            \mkessler@fancythm@numbersmallenvironmentswiththeoremtrue
        \else
            \mkessler@fancythm@numbersmallenvironmentswiththeoremfalse
        \fi
    \or
        \mkessler@fancythm@numbersmallenvironmentstrue
        \mkessler@fancythm@numbersmallenvironmentswiththeoremtrue
    \fi
}

\define@choicekey*{mkessler-fancythm}{mkessler@fancythm@lecturenumbers}[\val\nr]{true,false}{%
    \ifcase\nr\relax
        \mkessler@fancythm@lecturenumberstrue
    \else
        \mkessler@fancythm@lecturenumbersfalse
    \fi
}

\define@choicekey*{mkessler-fancythm}{mkessler@fancythm@defaulttheorems}[\val\nr]{true,false}{%
    \ifcase\nr\relax
        \mkessler@fancythm@defaulttheoremstrue
    \else
        \mkessler@fancythm@defaulttheoremsfalse
    \fi
}

\define@choicekey*{mkessler-fancythm}{mkessler@fancythm@showdaggers}[\val\nr]{true,false}{%
    \ifcase\nr\relax
        \mkessler@fancythm@showdaggerstrue
    \else
        \mkessler@fancythm@showdaggersfalse
    \fi
}

\define@choicekey*{mkessler-fancythm}{ownenvironments}[\val\nr]{on, natural, off}{%
    \ifcase\nr\relax
        \mkessler@fancythm@includestarstrue
        \mkessler@fancythm@showstarstrue
    \or
        \mkessler@fancythm@includestarstrue
        \mkessler@fancythm@showstarsfalse
    \or
        \mkessler@fancythm@includestarsfalse
        \mkessler@fancythm@showstarsfalse
    \fi
}

\define@choicekey*{mkessler-fancythm}{oralremarks}[\val\nr]{on,natural,off}{%
    \ifcase\nr\relax
        \mkessler@fancythm@includeoraltrue
        \mkessler@fancythm@markoraltrue
    \or
        \mkessler@fancythm@includeoraltrue
        \mkessler@fancythm@markoraltrue
    \or
        \mkessler@fancythm@includeoralfalse
    \fi
}

%%%%% Option section
\newif\ifmkessler@fancythm@english\mkessler@fancythm@englishtrue
\DeclareOptionX{german}{\mkessler@fancythm@englishfalse}
\DeclareOptionX{ngerman}{\mkessler@fancythm@englishfalse}
\DeclareOptionX{english}{\mkessler@fancythm@englishtrue}

\DeclareOptionX{showdaggers}{\setkeys{mkessler-fancythm}{mkessler@fancythm@showdaggers=#1}}
\DeclareOptionX{ownenvironments}{\setkeys{mkessler-fancythm}{ownenvironments=#1}}
\DeclareOptionX{oralremarks}{\setkeys{mkessler-fancythm}{oralremarks=#1}}

\DeclareOptionX{lecturenumbers}{\setkeys{mkessler-fancythm}{mkessler@fancythm@lecturenumbers=#1}}
\DeclareOptionX{numbersmallenvironments}{\setkeys{mkessler-fancythm}{mkessler@fancythm@numbersmallenvironments=#1}}

\DeclareOptionX{defaulttheorems}{\setkeys{mkessler-fancythm}{mkessler@fancythm@defaulttheorems=#1}}

\DeclareOptionX*{\PackageWarning{mkessler-fancythm}{Unknown '\CurrentOption'}}
\ProcessOptionsX*\relax


%%%%%%%% Implementation section

%Required Packages and setup
\RequirePackage{amsmath}
\RequirePackage{amsthm}
\RequirePackage{mdframed}
\RequirePackage{thmtools}
\RequirePackage[skins]{tcolorbox}
\RequirePackage{mfirstuc}
\RequirePackage{xifthen}
\RequirePackage{xparse}
\tcbuselibrary{breakable}

% Dummy counters
\declaretheorem[numberwithin=section]{mkessler@fancythm@dummy}
\declaretheorem[numberwithin=mkessler@fancythm@dummy]{mkessler@fancythm@smalldummy}

%%%%% Main part, i.e. providing macros for defining new theorems

%%Environments that are numbered by default have 3 versions:
% - the standard one, for numbering
% - one asterisk, to exclude them from numbering (because they were not numbered in the lecture) -> they will be numbered if 'truenmubers' option is set
% - two asterisks, for marking them as self-added, so they will not be numbered, but will receive a visual asterisk. -> They will be numbered if 'truenumbers' option is set



\NewDocumentCommand{\declarebigtheorem}{O{} O{} m m O{}}{
    %First, store the name of the theorem in \theoremname
    \ifmkessler@fancythm@english
        \ifthenelse{\isempty{#2}}{\def\theoremname{#4}}{\def\theoremname{#2}}
    \else
        \ifthenelse{\isempty{#1}}{\def\theoremname{#4}}{\def\theoremname{#1}}
    \fi

    %Define the mane version of the theorem
    \ifthenelse{\isempty{#5}}{\declaretheorem[style = #3, name = \makefirstuc{\expandafter\theoremname}, sibling = mkessler@fancythm@dummy]{#4}}{}

    %Define the versions theorem*, theorem**, dtheorem
\ifmkessler@fancythm@lecturenumbers
    \ifmkessler@fancythm@numbersmallenvironments
            \declaretheorem[style = #3, name = \makefirstuc{\expandafter\theoremname}, sibling=mkessler@fancythm@smalldummy]{#4*}
            \declaretheorem[style = #3, name = \makefirstuc{\expandafter\theoremname}\ifmkessler@fancythm@showstars *\else\fi, sibling=mkessler@fancythm@smalldummy]{#4**}
            \declaretheorem[style = #3, name = \makefirstuc{\expandafter\theoremname}\ifmkessler@fancythm@showdaggers $^{\dagger}$\else\fi, sibling=mkessler@fancythm@smalldummy]{d#4}
        \else
            \declaretheorem[style = #3, name = \makefirstuc{\expandafter\theoremname}, numbered=no]{#4*}
            \declaretheorem[style = #3, name = \makefirstuc{\expandafter\theoremname}\ifmkessler@fancythm@showstars *\else\fi, numbered = no]{#4**}
            \declaretheorem[style = #3, name = \makefirstuc{\expandafter\theoremname}\ifmkessler@fancythm@showdaggers $^{\dagger}$\else\fi, numbered = no]{d#4}
        \fi
    \else
        \declaretheorem[style = #3, name = \makefirstuc{\expandafter\theoremname}, sibling=mkessler@fancythm@dummy]{#4*}
        \declaretheorem[style = #3, name = \makefirstuc{\expandafter\theoremname}\ifmkessler@fancythm@showstars *\else\fi, sibling=mkessler@fancythm@dummy]{#4**}
        \declaretheorem[style = #3, name = \makefirstuc{\expandafter\theoremname}\ifmkessler@fancythm@showdaggers $^{\dagger}$\else\fi, sibling=mkessler@fancythm@dummy]{d#4}
    \fi
    \ifmkessler@fancythm@includestars\else\renewenvironment{#4**}{\comment}{\endcomment}\fi
}

\NewDocumentCommand{\declaresmalltheorem}{O{} O{} m m}{
    %Get the name of the theorem and store it in \theoremname
    \ifmkessler@fancythm@english
        \ifthenelse{\isempty{#2}}{\def\theoremname{#4}}{\def\theoremname{#2}}
    \else
        \ifthenelse{\isempty{#1}}{\def\theoremname{#4}}{\def\theoremname{#1}}
    \fi
    \ifmkessler@fancythm@numbersmallenvironments
        \ifmkessler@fancythm@numbersmallenvironmentswiththeorem
            \declaretheorem[style=#3,name=\makefirstuc{\expandafter\theoremname}, sibling = mkessler@fancythm@smalldummy]{#4}
            \declaretheorem[style=#3,name=\makefirstuc{\expandafter\theoremname}\ifmkessler@fancythm@showstars *\else\fi, sibling=mkessler@fancythm@smalldummy]{#4*}
            \declaretheorem[style=#3,name=\makefirstuc{\expandafter\theoremname}\ifmkessler@fancythm@showdaggers $^{\dagger}$\else\fi, sibling=mkessler@fancythm@smalldummy]{d#4}
        \else
            \declaretheorem[style=#3,name=\makefirstuc{\expandafter\theoremname}, sibling=mkessler@fancythm@dummy]{#4}
            \declaretheorem[style=#3,name=\makefirstuc{\expandafter\theoremname}\ifmkessler@fancythm@showstars *\else\fi, sibling=mkessler@fancythm@dummy]{#4*}
            \declaretheorem[style=#3,name=\makefirstuc{\expandafter\theoremname}\ifmkessler@fancythm@showdaggers $^{\dagger}$\else\fi, sibling=mkessler@fancythm@dummy]{d#4}
        \fi
    \else
        \declaretheorem[style=#3,name=\makefirstuc{\expandafter\theoremname}, numbered = no]{#4}
        \declaretheorem[style=#3,name=\makefirstuc{\expandafter\theoremname}\ifmkessler@fancythm@showstars *\else\fi, numbered = no]{#4*}
        \declaretheorem[style=#3,name=\makefirstuc{\expandafter\theoremname}\ifmkessler@fancythm@showdaggers $^{\dagger}$\else\fi, numbered = no]{d#4}
    \fi
    \declaretheorem[style=#3,name=\makefirstuc{\expandafter\theoremname}, sibling=mkessler@fancythm@dummy]{n#4}
    \ifmkessler@fancythm@includestars\else\renewenvironment{#4*}{\comment}{\endcomment}\fi
}


%%%% If requested, now define some default theoremstyles
\ifmkessler@fancythm@defaulttheorems
    \RequirePackage[default styles]{mkessler-thmstyle} % Easy setup of mdframed styles

    \declarebigtheorem[Satz][Theorem]{thmredmarginandfill}{theorem} \ifmkessler@fancythm@english\else\AtBeginDocument{\def\theoremautorefname{Satz}}\fi
    \declarebigtheorem{thmredmarginandfill}{proposition}
    \declarebigtheorem[Korollar]{thmredmarginandfill}{corollary}
    \declarebigtheorem{thmorangemarginandfill}{lemma}
    \declarebigtheorem[Lemma und Definition][Lemma and Definition]{thmorangemarginbluefill}{lemmadef}
    \declarebigtheorem{thmbluemarginandfill}{definition}
    \declarebigtheorem[Satz und Definition][Theorem and Definition]{thmredmarginbluefill}{theoremdef}
    \declarebigtheorem[Proposition und Definition][Proposition and Definition]{thmredmarginbluefill}{propositiondef}
    \declarebigtheorem{thmvioletmarginandfill}{notation}
    \declarebigtheorem[zuSatz][Addition to Theorem]{thmredmarginandfill}{additiontheorem}

    %%%Environments that are not numbered by default (notation, question, example, remark etc) have two forms:
    % - normal form: won't be numbered unless 'mkessler@fancythm@numbersmallenvironments' is set
    % - having an asterisk: will be displayed with an asterisk, will be numbered if 'numbersmallenvironments' is set
    %Notation

    \declaresmalltheorem[Notationsmissbrauch][Abuse of Notation]{thmvioletmargin}{abuse}
    \declaresmalltheorem[Beispiel]{thmgreenmargin}{example}
    \declaresmalltheorem[Bemerkung]{thmyellowmargin}{remark}
    \declaresmalltheorem[Lob]{thmgoldmargin}{praise}
    \declaresmalltheorem[Trivial Nonsense][Trivial Nonsense]{thmyellowmargin}{trivial}
    \declaresmalltheorem[Frage]{thmblackmarginandfill}{question}
    \declaresmalltheorem[Organisatorisches][Organisational stuff]{thmblackmargin}{orga}
    \declaresmalltheorem[Fakt]{thmredmargin}{fact}

    %%%% Specially treated stuff

    \declaresmalltheorem[\ifmkessler@fancythm@markoral Mündliche Anmerkung\else Bemerkung\fi][\ifmkessler@fancythm@markoral Oral remark\else remark\fi]{thmyellowmargin}{oral}
    % Option to remove oral remarks when needed
    \ifmkessler@fancythm@includeoral\else
        \renewenvironment{oral}{\comment}{\endcomment}
        \renewenvironment{oral*}{\comment}{\endcomment}
        \renewenvironment{doral}{\comment}{\endcomment}
    \fi


    %%%%%%%% Other mdframed style boxes

    \newtcolorbox{recap}{before skip = 0.5cm, after skip = 0.5cm, enhanced, sharp corners = all, colback = white, colframe = gray, toprule=0pt, bottomrule=0pt, leftrule=0pt,rightrule=0pt, overlay = {
            \draw[gray, line width = 2pt] (frame.north west) -- ++(0.5cm, 0pt);
            \draw[gray, line width=2pt] (frame.south east) -- ++(-0.5cm, 0pt);
            \draw[gray, line width=2pt] (frame.north west) -- ++ (0pt, -0.5cm);
            \draw[gray, line width=2pt] (frame.south east) -- ++(0pt, 0.5cm);
    }}

    \newenvironment{moral}{%
        \begin{mdframed}[linecolor=green!70!black]%
        \bfseries\color{green!50!black}}%
        {\end{mdframed}}

    \newenvironment{antimoral}{%
        \begin{mdframed}[linecolor=red!70!black]%
        \bfseries\color{red!50!black}}%
        {\end{mdframed}}


    %%%%%%%%%%%% Non-mdframed theorems

    \theoremstyle{plain}
    \newtheorem{variant}{\ifmkessler@fancythm@english Variant\else Variante\fi}
    \newtheorem{assumption}{\ifmkessler@fancythm@english Assumption\else Annahme\fi}

    \theoremstyle{definition}
    \newtheorem*{note}{\ifmkessler@fancythm@english Note\else Anmerkung\fi}
    \newtheorem*{warning}{\color{red}\ifmkessler@fancythm@english Warning \else Warnung\fi}
    \newtheorem*{goal}{\ifmkessler@fancythm@english Goal \else Ziel\fi}
    \newtheorem*{strategy}{\ifmkessler@fancythm@english Proof Strategy \else Beweisstrategie\fi}
    \newtheorem*{goal*}{\ifmkessler@fancythm@english Goal* \else Ziel*\fi}
    \newtheorem*{problem}{Problem}
    \newtheorem*{info}{Information}


    \newtheorem*{answer}{\ifmkessler@fancythm@english Answer\else Antwort\fi}
    \newtheorem{observe}[theorem]{\ifmkessler@fancythm@english Observe\else Beobachte\fi}
    \newtheorem*{property}{\ifmkessler@fancythm@english Property\else Eigenschaft\fi}
    \newtheorem*{intuition}{Intuition}
    \newtheorem*{recall}{\ifmkessler@fancythm@english Recall\else Erinnerung\fi}
    \newtheorem*{idea}{\ifmkessler@fancythm@english Idea\else Idee\fi}
    \newtheorem{exercise}{\ifmkessler@fancythm@english Exercise\else Aufgabe\fi}[section]
    \newtheorem{reminder}{\ifmkessler@fancythm@english Reminder\else Erinnerung\fi}

\fi % end of default theorems
