% montgomery.cls
%   https://git.benjidial.net/latex-classes
% Written by Benji Dial, 2024
% Released under CC0 1.0
%   https://creativecommons.org/publicdomain/zero/1.0/

\ProvidesClass{montgomery}
\LoadClass{book}

\usepackage{amssymb}
\usepackage{geometry}
\usepackage{enumitem}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage[svgnames]{xcolor}
\usepackage{thmtools}
\usepackage{unicode-math}
\usepackage{pdftexcmds}
\usepackage[skins,breakable]{tcolorbox}
\usepackage{hyperref}
\usepackage[capitalize]{cleveref}
\usepackage{rotating}
\usepackage{environ}
\usepackage[sorting=nyt,style=authoryear]{biblatex}
\usepackage{imakeidx}
\usepackage{titlesec}
\usepackage{emptypage}

\setmainfont{CMU Sans Serif}
\setmathfont{Concrete Math}

\makeatletter

\newcommand{\barcolor}[1]{%
  \ifnum\pdf@strcmp{#1}{definition}=0
    DarkBlue%
  \else\ifnum\pdf@strcmp{#1}{example}=0
    DarkGreen%
  \else\ifnum\pdf@strcmp{#1}{exercise}=0
    OrangeRed%
  \else\ifnum\pdf@strcmp{#1}{theorem}=0
    Maroon%
  \else\ifnum\pdf@strcmp{#1}{suggestion}=0
    MediumVioletRed%
  \fi\fi\fi\fi\fi
}

\newcommand{\myenvir}{\@currenvir}

\renewenvironment{proof}[1][\proofname]{
  \par\pushQED{\qed}\normalfont
  \relax{\itshape \bfseries #1\@addpunct{.}}\hspace\labelsep\ignorespaces
}{\popQED\par}

\newcounter{solution}

\newcommand{\solutions}{\setcounter{solution}{0}}

\newcommand{\addsolution}[1]{
  \g@addto@macro\solutions{
    \stepcounter{solution}
    \begin{proof}[\cref{solution-\thesolution-exercise}]
      \par
      \label{solution-\thesolution}
      #1
    \end{proof}
  }
}

\newenvironment{solution}{%
  \unskip%
  \stepcounter{solution}%
  \label{solution-\thesolution-exercise}%
  \textit{Solution on page \pageref{solution-\thesolution}.}
  \Collect@Body\addsolution
}{}

\makeatother

\declaretheoremstyle[
  spaceabove=0pt,
  spacebelow=0pt,
  headfont=\bfseries,
  notefont=\mdseries,
  bodyfont=\mdseries,
  notebraces={(}{)},
  preheadhook={
    \edef\mycolor{\barcolor{\myenvir}}
    \begin{tcolorbox}[
      enhanced,
      frame hidden,
      interior hidden,
      borderline west={2pt}{0pt}{\mycolor},
      left*=\topsep,
      right*=0pt,
      top=0pt,
      bottom=0pt,
      parbox=false,
      breakable
    ]
  },
  postfoothook={
    \end{tcolorbox}
  }
]{boxedthm}

\declaretheorem[style=boxedthm,numbered=no]{definition}
\declaretheorem[style=boxedthm,numbered=no]{example}
\declaretheorem[style=boxedthm,parent=section]{exercise}
\declaretheorem[style=boxedthm,parent=section]{theorem}
\declaretheorem[style=boxedthm,numbered=no]{suggestion}

\setlist[itemize]{itemsep=0pt,topsep=0pt}
\setlist[enumerate]{itemsep=0pt,topsep=0pt,label=\alph*)}
\setlist[enumerate,2]{label=\roman*)}

\titleformat{\chapter}[display]{%
  \thispagestyle{empty}%
  \Large\itshape\center%
}{%
  \chaptertitlename~\thechapter:%
}{0pt}{%
  \Huge\bfseries%
}

\titleformat{\section}[block]{%
  \cleardoublepage%
  \thispagestyle{empty}%
  \Large\itshape\bfseries\center%
}{%
  Section~\thesection: %
}{0pt}{}

\titleformat{\subsection}[block]{%
  \large\itshape\bfseries\center%
}{}{0pt}{}

\AtBeginDocument{
  \setlength{\parskip}{\topsep}
  \setlength{\parindent}{0pt}
}
