\ProvidesPackage{kaotheorems}

\RequirePackage{kvoptions} % Handle package options
\SetupKeyvalOptions{
    family = kaotheorems,
    prefix = kaotheorems@
}

\DeclareBoolOption{framed}

\newcommand{\kaotheorems@defaultbg}{Goldenrod!45!white}
\DeclareStringOption[\kaotheorems@defaultbg]{background}
\DeclareStringOption[\kaotheorems@defaultbg]{theorembackground}
\DeclareStringOption[\kaotheorems@defaultbg]{propositionbackground}
\DeclareStringOption[\kaotheorems@defaultbg]{lemmabackground}
\DeclareStringOption[\kaotheorems@defaultbg]{corollarybackground}
\DeclareStringOption[\kaotheorems@defaultbg]{definitionbackground}
\DeclareStringOption[\kaotheorems@defaultbg]{assumptionbackground}
\DeclareStringOption[\kaotheorems@defaultbg]{remarkbackground}
\DeclareStringOption[\kaotheorems@defaultbg]{examplebackground}
\DeclareStringOption[\kaotheorems@defaultbg]{exercisebackground}

\ProcessKeyvalOptions{kaotheorems}

\let\openbox\relax
\RequirePackage{amsmath} % Improved mathematics
\RequirePackage{amsthm} % Mathematical environments
\RequirePackage{thmtools} % Theorem styles

\ifkaotheorems@framed%
    \RequirePackage{tikz} % Colorful boxes
    \RequirePackage[framemethod=TikZ]{mdframed}

    % Box style
    \mdfsetup{skipabove=\topskip,skipbelow=0pt}%-.5\topskip}
    \mdfdefinestyle{mdfkao}{
        skipabove=\topskip,
        skipbelow=\topskip, % Does not work :(
        rightmargin=0pt,
        leftmargin=0pt,
        innertopmargin=7pt,
        innerbottommargin=3pt,
        innerrightmargin=5pt,
        innerleftmargin=5pt,
        topline=false,
        bottomline=false,
        rightline=false,
        leftline=false,
        %linewidth=1pt,
        %roundcorner=0pt,
        %font={},
        %frametitlefont={},
        frametitlerule=true,
        %linecolor=black,
        %backgroundcolor=LightBlue,
        %fontcolor=black,
        %frametitlebackgroundcolor=LightBlue,
    }

    % Theorem styles
    \declaretheoremstyle[
        %spaceabove=.5\thm@preskip,
        %spacebelow=.5\thm@postskip,
        %headfont=\normalfont\bfseries,%\scshape,
        %notefont=\normalfont, notebraces={ (}{)},
        bodyfont=\normalfont\itshape,
        %headformat={\NAME\space\NUMBER\space\NOTE},
        headpunct={},
        %postheadspace={.5em plus .1em minus .1em},
        %prefoothook={\hfill\qedsymbol}
    ]{kaoplain}
    \declaretheoremstyle[
        %spaceabove=.5\thm@preskip,
        %spacebelow=.5\thm@postskip,
        %headfont=\normalfont\bfseries,%\scshape,
        %notefont=\normalfont, notebraces={ (}{)},
        bodyfont=\normalfont\itshape,
        %headformat={\NAME\space\NUMBER\space\NOTE},
        headpunct={},
        postheadspace={.5em plus .1em minus .1em},
        %prefoothook={\hfill\qedsymbol}
    ]{kaodefinition}
    \declaretheoremstyle[
        %spaceabove=.5\thm@preskip,
        %spacebelow=.5\thm@postskip,
        %headfont=\normalfont\bfseries,%\scshape,
        %notefont=\normalfont, notebraces={ (}{)},
        bodyfont=\normalfont\itshape,
        %headformat={\NAME\space\NUMBER\space\NOTE},
        headpunct={},
        postheadspace={.5em plus .1em minus .1em},
        %prefoothook={\hfill\qedsymbol}
    ]{kaoassumption}
    \declaretheoremstyle[
        %spaceabove=.5\thm@preskip,
        %spacebelow=.5\thm@postskip,
        %headfont=\normalfont\bfseries,
        %notefont=\normalfont, notebraces={ (}{)},
        %bodyfont=\normalfont,
        %headformat={\footnotesize$\triangleright$\space\normalsize\NAME\space\NUMBER\space\NOTE},
        %headformat={\NAME\space\NUMBER\space\NOTE},
        headpunct={},
        postheadspace={.5em plus .1em minus .1em},
        %refname={theorem,theorems},
        %Refname={Theorem,Theorems},
    ]{kaoremark}
    \declaretheoremstyle[
        %spaceabove=.5\thm@preskip,
        %spacebelow=.5\thm@postskip,
        %headfont=\normalfont\bfseries,
        %notefont=\normalfont, notebraces={ (}{)},
        %bodyfont=\normalfont,
        %headformat={\NAME\space\NUMBER\space\NOTE},
        headpunct={},
        postheadspace={.5em plus .1em minus .1em},
        %prefoothook={\hfill\qedsymbol}
        %refname={theorem,theorems},
        %Refname={Theorem,Theorems},
    ]{kaoexample}
    \declaretheoremstyle[
        %spaceabove=.5\thm@preskip,
        %spacebelow=.5\thm@postskip,
        %headfont=\normalfont\bfseries,
        %notefont=\normalfont, notebraces={ (}{)},
        %bodyfont=\small,
        %headformat={\NAME\space\NUMBER\space\NOTE},
        headpunct={},
        postheadspace={.5em plus .1em minus .1em},
        %prefoothook={\hfill\qedsymbol}
        %refname={theorem,theorems},
        %Refname={Theorem,Theorems},
    ]{kaoexercise}

    \theoremstyle{kaoplain}
    \declaretheorem[
        name=Theorem,
        style=kaoplain,
        %refname={theorem,theorems},
        refname={Theorem,Theorems},
        Refname={Theorem,Theorems},
        numberwithin=section,
        mdframed={
            style=mdfkao,
            backgroundcolor=\kaotheorems@theorembackground,
            %frametitlebackgroundcolor=\@theorembackground,
        },
    ]{theorem}
    \declaretheorem[
        name=Proposition,
        %refname={proposition,propositions},
        refname={Proposition,Propositions},
        Refname={Proposition,Propositions},
        sibling=theorem,
        mdframed={
            style=mdfkao,
            backgroundcolor=\kaotheorems@propositionbackground,
            %frametitlebackgroundcolor=\@theorembackground,
        },
    ]{proposition}
    \declaretheorem[
        name=Lemma,
        %refname={lemma,lemmas},
        refname={Lemma,Lemmas},
        Refname={Lemma,Lemmas},
        sibling=theorem,
        mdframed={
            style=mdfkao,
            backgroundcolor=\kaotheorems@lemmabackground,
            %frametitlebackgroundcolor=\@theorembackground,
        },
    ]{lemma}
    \declaretheorem[
        name=Corollary,
        %refname={corollary,corollaries},
        refname={Corollary,Corollaries},
        Refname={Corollary,Corollaries},
        sibling=theorem,
        mdframed={
            style=mdfkao,
            backgroundcolor=\kaotheorems@corollarybackground,
            %frametitlebackgroundcolor=\@theorembackground,
        },
    ]{corollary}

    \theoremstyle{kaodefinition}
    \declaretheorem[
        name=Definition,
        %refname={definition,definitions},
        refname={Definition,Definitions},
        Refname={Definition,Definitions},
        numberwithin=section,
        mdframed={
            style=mdfkao,
            backgroundcolor=\kaotheorems@definitionbackground,
            %frametitlebackgroundcolor=\@theorembackground,
        },
    ]{definition}

    \theoremstyle{kaoassumption}
    \declaretheorem[
        name=Assumption,
        %refname={assumption,assumptions},
        refname={Assumption,Assumptions},
        Refname={Assumption,Assumptions},
        numberwithin=section,
        mdframed={
            style=mdfkao,
            backgroundcolor=\kaotheorems@assumptionbackground,
            %frametitlebackgroundcolor=\@theorembackground,
        },
    ]{assumption}

    \theoremstyle{kaoremark}
    \declaretheorem[
        name=Remark,
        %refname={remark,remarks},
        refname={Remark,Remarks},
        Refname={Remark,Remarks},
        numberwithin=section,
        mdframed={
            style=mdfkao,
            backgroundcolor=\kaotheorems@remarkbackground,
            %frametitlebackgroundcolor=\@theorembackground,
        },
    ]{remark}

    \theoremstyle{kaoexample}
    \declaretheorem[
        name=Example,
        %refname={example,examples},
        refname={Example,Examples},
        Refname={Example,Examples},
        numberwithin=section,
        mdframed={
            style=mdfkao,
            backgroundcolor=\kaotheorems@examplebackground,
            %frametitlebackgroundcolor=\@theorembackground,
        },
    ]{example}

    \theoremstyle{kaoexercise}
    \declaretheorem[
        name=Exercise,
        %refname={example,examples},
        refname={Exercise,Exercises},
        Refname={Exercise,Exercises},
        numberwithin=section,
        mdframed={
            style=mdfkao,
            backgroundcolor=\kaotheorems@exercisebackground,
            %frametitlebackgroundcolor=\@theorembackground,
        },
    ]{exercise}

    %\renewcommand{\thetheorem}{\arabic{chapter}.\arabic{section}.\arabic{theorem}}
    %\renewcommand{\thetheorem}{\arabic{subsection}.\arabic{theorem}}
    %\renewcommand{\qedsymbol}{$\blacksquare$}
\else

    % Theorem styles
    \declaretheoremstyle[
        spaceabove=.6\thm@preskip,
        spacebelow=.1\thm@postskip,
        %headfont=\normalfont\bfseries,%\scshape,
        %notefont=\normalfont, notebraces={ (}{)},
        bodyfont=\normalfont\itshape,
        %headformat={\NAME\space\NUMBER\space\NOTE},
        headpunct={},
        %postheadspace={.5em plus .1em minus .1em},
        %prefoothook={\hfill\qedsymbol}
    ]{kaoplain}
    \declaretheoremstyle[
        spaceabove=.6\thm@preskip,
        spacebelow=.1\thm@postskip,
        %headfont=\normalfont\bfseries,%\scshape,
        %notefont=\normalfont, notebraces={ (}{)},
        bodyfont=\normalfont\itshape,
        %headformat={\NAME\space\NUMBER\space\NOTE},
        headpunct={},
        %postheadspace={.5em plus .1em minus .1em},
        %prefoothook={\hfill\qedsymbol}
    ]{kaodefinition}
    \declaretheoremstyle[
        spaceabove=.6\thm@preskip,
        spacebelow=.1\thm@postskip,
        %headfont=\normalfont\bfseries,
        %notefont=\normalfont, notebraces={ (}{)},
        %bodyfont=\normalfont,
        %headformat={\footnotesize$\triangleright$\space\normalsize\NAME\space\NUMBER\space\NOTE},
        %headformat={\NAME\space\NUMBER\space\NOTE},
        headpunct={},
        %postheadspace={.5em plus .1em minus .1em},
        %refname={theorem,theorems},
        %Refname={Theorem,Theorems},
    ]{kaoremark}
    \declaretheoremstyle[
        spaceabove=.6\thm@preskip,
        spacebelow=.1\thm@postskip,
        %headfont=\normalfont\bfseries,
        %notefont=\normalfont, notebraces={ (}{)},
        %bodyfont=\normalfont,
        %headformat={\NAME\space\NUMBER\space\NOTE},
        headpunct={},
        %postheadspace={.5em plus .1em minus .1em},
        %prefoothook={\hfill\qedsymbol}
        %refname={theorem,theorems},
        %Refname={Theorem,Theorems},
    ]{kaoexample}
    \declaretheoremstyle[
        %spaceabove=.5\thm@preskip,
        %spacebelow=.5\thm@postskip,
        %headfont=\normalfont\bfseries,
        %notefont=\normalfont, notebraces={ (}{)},
        %bodyfont=\normalfont,
        %headformat={\NAME\space\NUMBER\space\NOTE},
        headpunct={},
        postheadspace={.5em plus .1em minus .1em},
        %prefoothook={\hfill\qedsymbol}
        %refname={theorem,theorems},
        %Refname={Theorem,Theorems},
    ]{kaoexercise}

    \theoremstyle{kaoplain}
    \declaretheorem[
        name=Theorem,
        refname={theorem,theorems},
        Refname={Theorem,Theorems},
        numberwithin=section,
    ]{theorem}
    \declaretheorem[
        name=Proposition,
        refname={proposition,propositions},
        Refname={Proposition,Propositions},
        sibling=theorem,
    ]{proposition}
    \declaretheorem[
        name=Lemma,
        refname={lemma,lemmas},
        Refname={Lemma,Lemmas},
        sibling=theorem,
    ]{lemma}
    \declaretheorem[
        name=Corollary,
        refname={corollary,corollaries},
        Refname={Corollary,Corollaries},
        sibling=theorem,
    ]{corollary}

    \theoremstyle{kaodefinition}
    \declaretheorem[
        name=Definition,
        refname={definition,definitions},
        Refname={Definition,Definitions},
        numberwithin=section,
    ]{definition}

    \theoremstyle{kaoremark}
    \declaretheorem[
        name=Remark,
        refname={remark,remarks},
        Refname={Remark,Remarks},
        numberwithin=section,
    ]{remark}

    \theoremstyle{kaoexample}
    \declaretheorem[
        name=Example,
        refname={example,examples},
        Refname={Example,Examples},
        numberwithin=section,
    ]{example}

    \theoremstyle{kaoexercise}
    \declaretheorem[
        name=Exercise,
        %refname={example,examples},
        refname={Exercise,Exercises},
        Refname={Exercise,Exercises},
        numberwithin=section,
        mdframed={
            style=mdfkao,
            backgroundcolor=\@exercisebackground,
            %frametitlebackgroundcolor=\@theorembackground,
        },
    ]{exercise}

    %\renewcommand{\thetheorem}{\arabic{chapter}.\arabic{section}.\arabic{theorem}}
    %\renewcommand{\thetheorem}{\arabic{subsection}.\arabic{theorem}}
    %\renewcommand{\qedsymbol}{$\blacksquare$}
\fi

