%
% Copyright 2016 (c) Anna Schumaker.
%
\ProvidesClass{cheese}

\RequirePackage{kvoptions}
\DeclareStringOption[10pt]{rowheight}
\ProcessKeyvalOptions*
\LoadClass[letterpaper]{letter}

\RequirePackage{gensymb}
\RequirePackage[landscape,top=0.8in,bottom=0.5in,left=0.7in,right=0.7in]{geometry}
\RequirePackage{graphicx}
\RequirePackage{hhline}
\RequirePackage{ifthen}
\RequirePackage{makecell}
\RequirePackage{multirow}
\RequirePackage{tabularx}
\RequirePackage[table]{xcolor}

\pagestyle{empty}

% Preformat some fractions
\newcommand{\half}         {\(\frac{1}{2}\)}
\newcommand{\third}        {\(\frac{1}{3}\)}
\newcommand{\quarter}      {\(\frac{1}{4}\)}
\newcommand{\threequarter} {\(\frac{3}{4}\)}
\newcommand{\eighth}       {\(\frac{1}{8}\)}
\newcommand{\sixteenth}    {\(\frac{1}{16}\)}

% Format a Fahrenheit temperature
\newcommand{\ftemp}[1] {#1\degree F}

% Make reusable messages for the notes section
\newcommand{\diluted}     {Diluted in \quarter cup distilled water}
\newcommand{\dilutemilk}  {Diluted in \quarter cup cheese milk}
\newcommand{\simplebrine} {simple brine\footnote{1\half teaspoons salt in 1 cup water.}}
\newcommand{\wash}[1]     {Slowly add \ftemp{#1} water.  Stir continuously}

% Are we using cheese color or a secondary culture?
\newif\ifcolor
\colorfalse
\newif\ifsecondary
\secondaryfalse
\newcounter{cheese@acid}
\stepcounter{cheese@acid}
\stepcounter{cheese@acid}

% Macros for setting cheese variables
\newcommand{\cheeseName}[1]{\def\cheese@name{#1}}
\newcommand{\cheeseCulture}[3]
{
	\def\cheese@culture{#1}
	\def\cheese@cultureTemp{\ftemp{#3}}
	\def\cheese@cultureAmount{#2}
}
\newcommand{\cheeseSecondary}[3]
{
	\secondarytrue
	\def\cheese@secondary{#1}
	\def\cheese@secondaryTemp{\ftemp{#3}}
	\def\cheese@secondaryAmount{#2}
	\stepcounter{cheese@acid}
}
\newcommand{\cheeseRipening}[2]
{
	\def\cheese@ripening{#1}
	\def\cheese@ripeningTemp{\ftemp{#2}}
}
\newcommand{\cheeseColor}[1]
{
	\colortrue
	\def\cheese@color{#1}
	\stepcounter{cheese@acid}
}
\newcommand{\cheeseRennet}[1]{\def\cheese@rennet{#1}}
\newcommand{\cheeseSetting}[1]{\def\cheese@setting{#1}}
\newcommand{\cheeseFlocculation}[1]{\def\cheese@flocculation{#1}}

\renewcommand{\_}{ \\ \hhline{|~*{7}{|-}|}}

\newcommand{\cheeseStep}[5]       { & #1 & #2 & &  #3   &  #4   & & #5 \_ }
\newcommand{\cheeseStepTemp}[4]   { & #1 & #2 & &  #3   &       & & #4 \_ }
\newcommand{\cheeseStepNoTemp}[3] { & #1 & #2 & & \gray & \gray & & #3 \_ }

% Create some useful table formatting macros
\newcommand{\thickline}{\Xhline{2\arrayrulewidth}}
\newcommand{\tbhead}[1]{\bfseries\centering\arraybackslash\footnotesize{#1}}
\newcommand{\gray}{\cellcolor{gray!30}}

\newcolumntype{C}{>{\centering\arraybackslash}p{0.85in}}
\newcolumntype{'}{!{\vrule width 1pt}}


\newcommand{\section}[2]
{
	\thickline
	\multirow{#2}{*}{\rotatebox{90}{\tbhead{#1}}}
}

% Extend the \begin{document} command
\AtBeginDocument
{
	\setlength{\extrarowheight}{2pt}
	\begin{tabularx}{\textwidth}{ p{2.5in}lllX }
		Cheese: \textbf{\cheese@name} & Flavors: \hfill & Date: & Yield: & Age Until: \\
		Milk Brand: \hfill & Type: ( Whole / \hspace{0.5in} )
				   & Homogenized: ( Y / N )
				   & Pastuerized: ( Y / N )
				   & Milked On: \\
	\end{tabularx}
}

\newenvironment{cheese}
{
	\setlength{\tabcolsep}{3pt}
	\setlength{\extrarowheight}{\cheese@rowheight}
	\tabularx{\textwidth}{ 'c'c|C|C|C|C|c|X' }
		\thickline
		\tbhead{} & \tbhead{Operation} & \tbhead{Amount} & \tbhead{Time}
			  & \tbhead{Goal Temp} & \tbhead{Actual Temp}
			  & \tbhead{\ pH\ }    & \tbhead{Comments And Notes} \\

	\section{Setup}{3}
	\cheeseStepTemp{Prepare Milk }{ }{\gray}{ }
	\cheeseStepNoTemp{Begin Heating }{ }{ }
	\cheeseStepTemp{Remove from Heat}{\gray}{ }

	\section{Acid}{\thecheese@acid}
	\cheeseStepTemp{\cheese@culture}{ }{\cheese@cultureTemp}
		       {(Optional for raw milk) \cheese@cultureAmount.}
	\ifsecondary
		\cheeseStepTemp{\cheese@secondary}{ }{\cheese@secondaryTemp}
			       {\cheese@secondaryAmount.}
	\fi
	\cheeseStepTemp{Ripening}{\cheese@ripening}{\cheese@ripeningTemp}{ }
	\ifcolor
		\cheeseStepNoTemp{Cheese Color}{\cheese@color}{(Optional) \dilutemilk.}
	\fi

	\section{Gel Devel}{4}
	\cheeseStepTemp{Rennet    }{\cheese@rennet }{\gray}{\diluted. Stir 1 minute.}
	\cheeseStepNoTemp{Setting }{\cheese@setting}{ }
	\cheeseStepNoTemp{Onset of Flocculation}{\cheese@flocculation}{ }
	\cheeseStepNoTemp{Clean Break}{\gray}{ }
}
{
	\thickline
	\endtabularx

	% FIXME: A newline was required for the \thickline to show up?
}
