\ProvidesPackage{kaotheorems}

%----------------------------------------------------------------------------------------
%	PACKAGE OPTIONS AND DEPENDENCIES
%----------------------------------------------------------------------------------------

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

\DeclareBoolOption{framed}% If true, put theorems into colorful boxes, otherwise write them like normal text

% Define the options to finely tune the background color of each element.
% If only the 'background' option is specified, all types of theorem will have that background. If more specific options are set, the previous option will be overwritten.
\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} % Process the options

\let\openbox\relax % Workaround to avoid a nasty error
\RequirePackage{amsmath} % Improved mathematics
\RequirePackage{amsthm} % Mathematical environments
\RequirePackage{thmtools} % Theorem styles

%----------------------------------------------------------------------------------------
%	STYLE DEFINITIONS
%----------------------------------------------------------------------------------------

\ifkaotheorems@framed% Define the style of the mdframed boxes for theorems
	\RequirePackage[most]{tcolorbox}

	% Box style
	\tcbset{tcbkao/.style={
		breakable,
		before skip=\topskip,
		after skip=\topskip,
		left skip=0pt,
		right skip=0pt,
		top=5pt,
		bottom=3pt,
		left=2pt,
		right=2pt,
		sharp corners,
		boxrule=0pt,
		frame hidden,
	}}

	% 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}

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

	% Theorems using the 'kaodefinition' style
	\theoremstyle{kaodefinition}
	\declaretheorem[
		name=Definition,
		%refname={definition,definitions},
		refname={Definition,Definitions},
		Refname={Definition,Definitions},
		numberwithin=section,
	]{definition}
	\tcolorboxenvironment{definition}{
		colback=\kaotheorems@definitionbackground,tcbkao
	}

	% Theorems using the 'kaoassumption' style
	\theoremstyle{kaoassumption}
	\declaretheorem[
		name=Assumption,
		%refname={assumption,assumptions},
		refname={Assumption,Assumptions},
		Refname={Assumption,Assumptions},
		numberwithin=section,
	]{assumption}
	\tcolorboxenvironment{assumption}{
		colback=\kaotheorems@assumptionbackground,tcbkao
	}

	% Theorems using the 'kaoremark' style
	\theoremstyle{kaoremark}
	\declaretheorem[
		name=Remark,
		%refname={remark,remarks},
		refname={Remark,Remarks},
		Refname={Remark,Remarks},
		numberwithin=section,
	]{remark}
	\tcolorboxenvironment{remark}{
		colback=\kaotheorems@remarkbackground,tcbkao
	}

	% Theorems using the 'kaoexample' style
	\theoremstyle{kaoexample}
	\declaretheorem[
		name=Example,
		%refname={example,examples},
		refname={Example,Examples},
		Refname={Example,Examples},
		numberwithin=section,
	]{example}
	\tcolorboxenvironment{example}{
		colback=\kaotheorems@examplebackground,tcbkao
	}

	% Theorems using the 'kaoexercise' style
	\theoremstyle{kaoexercise}
	\declaretheorem[
		name=Exercise,
		%refname={exercise,exercises},
		refname={Exercise,Exercises},
		Refname={Exercise,Exercises},
		numberwithin=section,
	]{exercise}
	\tcolorboxenvironment{exercise}{
		colback=\kaotheorems@exercisebackground,tcbkao
	}

	%\renewcommand{\thetheorem}{\arabic{chapter}.\arabic{section}.\arabic{theorem}}
	%\renewcommand{\thetheorem}{\arabic{subsection}.\arabic{theorem}}
	%\renewcommand{\qedsymbol}{$\blacksquare$}
\else % If not using mdframed
	% 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,%\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=.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}

	% Theorems using the 'kaoplain' style
	\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}

	% Theorems using the 'kaodefinition' style
	\theoremstyle{kaodefinition}
	\declaretheorem[
		name=Definition,
		refname={Definition,Definitions},
		Refname={Definition,Definitions},
		numberwithin=section,
	]{definition}
	
	% Theorems using the 'kaoassumption' style
	\theoremstyle{kaoassumption}
	\declaretheorem[
		name=Assumption,
		refname={Assumption,Assumptions},
		Refname={Assumption,Assumptions},
		numberwithin=section,
	]{assumption}

	% Theorems using the 'kaoremark' style
	\theoremstyle{kaoremark}
	\declaretheorem[
		name=Remark,
		refname={Remark,Remarks},
		Refname={Remark,Remarks},
		numberwithin=section,
	]{remark}

	% Theorems using the 'kaoexample' style
	\theoremstyle{kaoexample}
	\declaretheorem[
		name=Example,
		refname={Example,Examples},
		Refname={Example,Examples},
		numberwithin=section,
	]{example}

	% Theorems using the 'kaoexercise' style
	\theoremstyle{kaoexercise}
	\declaretheorem[
		name=Exercise,
		refname={Exercise,Exercises},
		Refname={Exercise,Exercises},
		numberwithin=section,
	]{exercise}

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

