
\usepackage[usenames]{xcolor}

\definecolor{dgrey}{RGB}{80,80,80}

\definecolor{course1}{RGB}{97, 153, 158}
\definecolor{course2}{RGB}{97, 97, 158}
\definecolor{course3}{RGB}{153, 97, 158}
\definecolor{course4}{RGB}{97, 153, 97}
\definecolor{course5}{RGB}{158, 97, 97}
\definecolor{course6}{RGB}{158, 153, 97}

\definecolor{office}{RGB}{206, 206, 134}
\definecolor{meeting}{RGB}{150, 150, 150}

\usepackage{pgf,tikz}


\tikzset{
  officehours/.style={        % style for office hour boxes
    very thick,
    office,
    fill=office!30!white,
    rounded corners,
  },
  office text/.style = {     % style for office hour text
    font = \itshape,
    black!69!white,
  },
  unavailablehours/.style={  % style for unavailable/meeting boxes
    very thick,
    meeting,
    fill=meeting!30!white,
    % rounded corners,
  },
  unavailable text/.style = {     % style for unavailable text
    font = \itshape,
    black!69!white,
  },
  classtime/.style={         % style for class boxes, color added later
    very thick,
    % rounded corners
  },
  class text/.style = {     % style for class text
    font = \bfseries,
    black,
  },
  borderstyle/.style={       % style for gridlines behind schedule
    thick,
    dotted,
    dgrey
  },
  rowheader/.style={         % style for time label boxes on left
    thick,
    dgrey
  },
  colheader/.style={         % style for weekday label boxes on top
    thick,
    dgrey,
    fill=blue!5!white
  },
  header text/.style = {     % style for hour labels on left and day labels on top
    font = \bfseries,
    dgrey,
  },
}




\pgfkeys{/tikz/.cd,
  class start/.initial=800,
  class start/.get=\ClassStart,
  class start/.store in=\ClassStart,
  class end/.initial=850,
  class end/.get=\ClassEnd,
  class end/.store in=\ClassEnd,
  class days/.initial={0,2,4},
  class days/.get=\ClassDays,
  class days/.store in=\ClassDays,
  class title/.initial=Math XXX,
  class title/.get=\ClassTitle,
  class title/.store in=\ClassTitle,
  class loc/.initial=Erie,
  class loc/.get=\ClassLoc,
  class loc/.store in=\ClassLoc,
  class color/.initial=white,
  class color/.get=\ClassColor,
  class color/.store in=\ClassColor,
  office start/.initial=900,
  office start/.get=\OfficeStart,
  office start/.store in=\OfficeStart,
  office end/.initial=950,
  office end/.get=\OfficeEnd,
  office end/.store in=\OfficeEnd,
  office days/.initial={1},
  office days/.get=\OfficeDays,
  office days/.store in=\OfficeDays,
  office loc/.initial= ,
  office loc/.get=\OfficeLoc,
  office loc/.store in=\OfficeLoc,
  unavailable start/.initial=1000,
  unavailable start/.get=\UnavailableStart,
  unavailable start/.store in=\UnavailableStart,
  unavailable end/.initial=1200,
  unavailable end/.get=\UnavailableEnd,
  unavailable end/.store in=\UnavailableEnd,
  unavailable days/.initial={1},
  unavailable days/.get=\UnavailableDays,
  unavailable days/.store in=\UnavailableDays,
  unavailable title/.initial=Unavailable,
  unavailable title/.get=\UnavailableTitle,
  unavailable title/.store in=\UnavailableTitle,
  table height/.initial={15},
  table height/.get=\THeight,
  table height/.store in=\THeight,
  table width/.initial={20},
  table width/.get=\TWidth,
  table width/.store in=\TWidth,
  start time/.initial={8},
  start time/.get=\StartTime,
  start time/.store in=\StartTime,
  end time/.initial={16},
  end time/.get=\EndTime,
  end time/.store in=\EndTime,
  col header height/.initial={14},
  col header height/.get=\WeekLabelPercent,
  col header height/.store in=\WeekLabelPercent,
  row header width/.initial={10},
  row header width/.get=\HourLabelPercent,
  row header width/.store in=\HourLabelPercent,
  col header color/.initial=dgrey!5!white,
  col header color/.get=\WeekLabelColor,
  col header color/.store in=\WeekLabelColor,
  row header color/.initial=dgrey!5!white,
  row header color/.get=\HourLabelColor,
  row header color/.store in=\HourLabelColor,
}

\newcommand{\DrawClass}[1][]{%
  \tikzset{#1}% the default options will be overwritten by the one introduced
  \foreach \x in \ClassDays {
      \pgfmathsetmacro{\hoursAfterStart}{int((\ClassStart - 100*\StartTime)/100)}
      \pgfmathsetmacro{\minsAfterStart}{int(\ClassStart - 100*\StartTime - \hoursAfterStart*100)}

      \pgfmathsetmacro{\hoursAfterEnd}{int((\ClassEnd - 100*\StartTime)/100)}
      \pgfmathsetmacro{\minsAfterEnd}{int(\ClassEnd - 100*\StartTime - \hoursAfterEnd*100)}

      \pgfmathsetmacro{\cstarty}{\THeight - \HRowH - \hoursAfterStart*60*\MinH - \minsAfterStart*\MinH}
      \pgfmathsetmacro{\cendy}{\THeight - \HRowH - \hoursAfterEnd*60*\MinH - \minsAfterEnd*\MinH}


      \pgfmathsetmacro{\ClassHeight}{\cstarty-\cendy}


      \draw[ classtime, \ClassColor, fill=\ClassColor!30!white  ] (\HColW + \x*\ColW + 0.005*\ColW, \cstarty) rectangle (\HColW + \x*\ColW + 0.995*\ColW, \cendy);

       \node [class text] at (\HColW + \x*\ColW + 0.5*\ColW, \cstarty - 0.33*\ClassHeight) {\ClassTitle};
      \node [class text] at (\HColW + \x*\ColW + 0.5*\ColW, \cstarty - 0.66*\ClassHeight) {\ClassLoc};
  }
}

\newcommand{\DrawOffice}[1][]{%
  \tikzset{#1}% the default options will be overwritten by the one introduced
  \foreach \x in \OfficeDays {
      \pgfmathsetmacro{\hoursAfterStart}{int((\OfficeStart - 100*\StartTime)/100)}
      \pgfmathsetmacro{\minsAfterStart}{int(\OfficeStart - 100*\StartTime - \hoursAfterStart*100)}

      \pgfmathsetmacro{\hoursAfterEnd}{int((\OfficeEnd - 100*\StartTime)/100)}
      \pgfmathsetmacro{\minsAfterEnd}{int(\OfficeEnd - 100*\StartTime - \hoursAfterEnd*100)}

      \pgfmathsetmacro{\cstarty}{\THeight - \HRowH - \hoursAfterStart*60*\MinH - \minsAfterStart*\MinH}
      \pgfmathsetmacro{\cendy}{\THeight - \HRowH - \hoursAfterEnd*60*\MinH - \minsAfterEnd*\MinH}


      \pgfmathsetmacro{\ClassHeight}{\cstarty-\cendy}


      \draw[ officehours ] (\HColW + \x*\ColW + 0.005*\ColW, \cstarty) rectangle (\HColW + \x*\ColW + 0.995*\ColW, \cendy);
      \node [office text] at (\HColW + \x*\ColW + 0.5*\ColW, \cstarty - 0.33*\ClassHeight) {\small Office Hours \OfficeLoc};

      \pgfmathsetmacro{\OSH}{int(\OfficeStart/100)}
      \ifnum\OSH > 12
        \pgfmathsetmacro{\OSHH}{int(\OSH - 12)}
      \else
        \pgfmathsetmacro{\OSHH}{\OSH}
      \fi

      \pgfmathsetmacro{\OSM}{int(\OfficeStart - 100*\OSH)}
      \ifnum\OSM < 10
        \pgfmathsetmacro{\OSMM}{0\OSM}
      \else
        \pgfmathsetmacro{\OSMM}{\OSM}
      \fi

      \pgfmathsetmacro{\OEH}{int(\OfficeEnd/100)}
      \ifnum\OEH > 12
        \pgfmathsetmacro{\OEHH}{int(\OEH - 12)}
      \else
        \pgfmathsetmacro{\OEHH}{\OEH}
      \fi

      \pgfmathsetmacro{\OEM}{int(\OfficeEnd - 100*\OEH)}
      \ifnum\OEM < 10
        \pgfmathsetmacro{\OEMM}{0\OEM}
      \else
        \pgfmathsetmacro{\OEMM}{\OEM}
      \fi

      \node [office text] at (\HColW + \x*\ColW + 0.5*\ColW, \cstarty - 0.66*\ClassHeight) {\OSHH:\OSMM \ - \OEHH:\OEMM};
  }
}

\newcommand{\DrawUnavailable}[1][]{%
  \tikzset{#1}
  \foreach \x in \UnavailableDays {
      \pgfmathsetmacro{\hoursAfterStart}{int((\UnavailableStart - 100*\StartTime)/100)}
      \pgfmathsetmacro{\minsAfterStart}{int(\UnavailableStart - 100*\StartTime - \hoursAfterStart*100)}

      \pgfmathsetmacro{\hoursAfterEnd}{int((\UnavailableEnd - 100*\StartTime)/100)}
      \pgfmathsetmacro{\minsAfterEnd}{int(\UnavailableEnd - 100*\StartTime - \hoursAfterEnd*100)}

      \pgfmathsetmacro{\cstarty}{\THeight - \HRowH - \hoursAfterStart*60*\MinH - \minsAfterStart*\MinH}
      \pgfmathsetmacro{\cendy}{\THeight - \HRowH - \hoursAfterEnd*60*\MinH - \minsAfterEnd*\MinH}


      \pgfmathsetmacro{\ClassHeight}{\cstarty-\cendy}

      \draw[ unavailablehours ] (\HColW + \x*\ColW + 0.005*\ColW, \cstarty) rectangle (\HColW + \x*\ColW + 0.995*\ColW, \cendy);
      \node [unavailable text] at (\HColW + \x*\ColW + 0.5*\ColW, \cstarty - 0.33*\ClassHeight) {\UnavailableTitle};

      \pgfmathsetmacro{\OSH}{int(\UnavailableStart/100)}
      \ifnum\OSH > 12
        \pgfmathsetmacro{\OSHH}{int(\OSH - 12)}
      \else
        \pgfmathsetmacro{\OSHH}{\OSH}
      \fi

      \pgfmathsetmacro{\OSM}{int(\UnavailableStart - 100*\OSH)}
      \ifnum\OSM < 10
        \pgfmathsetmacro{\OSMM}{0\OSM}
      \else
        \pgfmathsetmacro{\OSMM}{\OSM}
      \fi

      \pgfmathsetmacro{\OEH}{int(\UnavailableEnd/100)}
      \ifnum\OEH > 12
        \pgfmathsetmacro{\OEHH}{int(\OEH - 12)}
      \else
        \pgfmathsetmacro{\OEHH}{\OEH}
      \fi

      \pgfmathsetmacro{\OEM}{int(\UnavailableEnd - 100*\OEH)}
      \ifnum\OEM < 10
        \pgfmathsetmacro{\OEMM}{0\OEM}
      \else
        \pgfmathsetmacro{\OEMM}{\OEM}
      \fi

      \node [unavailable text] at (\HColW + \x*\ColW + 0.5*\ColW, \cstarty - 0.66*\ClassHeight) {\OSHH:\OSMM \ - \OEHH:\OEMM};
  }
}

\newcommand{\DrawGrid}[1][]{%
  \tikzset{#1}
  % Calculated Values

  \pgfmathsetmacro{\HColW}{\HourLabelPercent*\TWidth/100}
  \pgfmathsetmacro{\HRowH}{\WeekLabelPercent*\THeight/100}


  \pgfmathsetmacro{\NumRows}{\EndTime - \StartTime }              % Number of hour rows: 8:00 - 9:00, etc
  \pgfmathsetmacro{\SchedH}{\THeight - \HRowH}                   % Table height without header
  \pgfmathsetmacro{\RowH}{\SchedH/\NumRows}                      % Height of one hour row
  \pgfmathsetmacro{\SchedW}{\TWidth - \HColW}                    % Table width without header
  \pgfmathsetmacro{\ColW}{\SchedW/5}                             % Width of one day column

  \pgfmathsetmacro{\MinH}{\RowH/60}                              % Height of one minute

  \pgfmathsetmacro{\nEnd}{\EndTime - 1}
  % table border:



  \foreach \x [count=\i] in {Monday,Tuesday,Wednesday,Thursday,Friday} {
    \draw [colheader, fill=\WeekLabelColor] ( \HColW + \i*\ColW - \ColW, \THeight) rectangle (\HColW + \i*\ColW, \THeight-\HRowH);
    \node [header text] at ( \HColW + \i*\ColW - 0.5*\ColW , \THeight - 0.5*\HRowH) {\x};
    \draw [borderstyle] (\HColW + \i*\ColW, 0) -- (\HColW + \i*\ColW,\THeight-\HRowH);
  }

  \foreach \x [count=\i] in {\StartTime,...,\nEnd} {
    \pgfmathsetmacro{\end}{int(\x + 1)}
    \ifnum\end > 12
      \pgfmathsetmacro{\cend}{int(\end - 12)}
    \else
      \pgfmathsetmacro{\cend}{\end}
    \fi
    \ifnum\x > 12
      \pgfmathsetmacro{\cstart}{int(\x - 12)}
    \else
      \pgfmathsetmacro{\cstart}{\x}
    \fi
    \draw [rowheader, fill=\HourLabelColor] ( 0, \THeight - \HRowH - \i*\RowH ) rectangle ( \HColW, \THeight - \HRowH - \i*\RowH + \RowH);
    \node [header text] at ( 0.5*\HColW, \THeight - \HRowH - \i*\RowH + 0.5*\RowH) {\cstart:00 - \cend:00};
    \draw [borderstyle] (\HColW,\THeight - \HRowH - \i*\RowH ) -- (\TWidth, \THeight - \HRowH - \i*\RowH );
  }
}
