\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{university_thesis}[2026/01/20 University Thesis Class]

% -------------------------------------------------
% Base class
% -------------------------------------------------
% \LoadClass[12pt,twoside,openright]{report}
% \LoadClass[12pt,twoside]{report}
\LoadClass[12pt,oneside]{report}

% -------------------------------------------------
% Required packages
% -------------------------------------------------
\RequirePackage{geometry}
\RequirePackage{graphicx}
\RequirePackage{setspace}
\RequirePackage{fontspec}
\RequirePackage{ragged2e}
\RequirePackage{tabularx}
\RequirePackage[hidelinks]{hyperref}
\RequirePackage[acronym]{glossaries}
\makeglossaries



% -------------------------------------------------
% Page numbering control (ADD THIS)
% -------------------------------------------------
\newcommand{\frontmatter}{
  \cleardoublepage
  \pagenumbering{roman}
  \setcounter{page}{1}
}

\newcommand{\mainmatter}{
  \cleardoublepage
  \pagenumbering{arabic}
  \setcounter{page}{1}
}


\newenvironment{mbupage}{
  \clearpage
  \thispagestyle{plain}
}{
  \clearpage
}

% -------------------------------------------------
% Page margins (EXACT)
% -------------------------------------------------
\geometry{
  a4paper,
  left=1.5in,
  right=1.0in,
  top=1.25in,
  bottom=1.25in
}

% -------------------------------------------------
% Line spacing
% -------------------------------------------------
\onehalfspacing

% -------------------------------------------------
% Font definitions (WITH PATH)
% -------------------------------------------------
\setmainfont[
  Path = fonts/,
  UprightFont = calibri.ttf,
  BoldFont = calibrib.ttf,
  ItalicFont = calibrii.ttf,
  BoldItalicFont = calibriz.ttf
]{Calibri}

\newfontfamily\CalibriBlack[
  Path = fonts/,
  UprightFont = calibrib.ttf
]{Calibri Black}

\newfontfamily\CalibriBoldItalic[
  Path = fonts/,
  UprightFont = calibrib.ttf,
  ItalicFont = calibrii.ttf,
  BoldFont = calibrib.ttf,
  BoldItalicFont = calibriz.ttf
]{Calibri}

\newfontfamily\MonotypeCorsiva[
  Path = fonts/,
  UprightFont = MTCORSVA.TTF
]{Monotype Corsiva}


% -------------------------------------------------
% Blank line control macros
% -------------------------------------------------
\newcommand{\OneBlankLine}{\vspace{1\baselineskip}}
\newcommand{\TwoBlankLines}{\vspace{2\baselineskip}}
\newcommand{\ThreeBlankLines}{\vspace{3\baselineskip}}
\newcommand{\FourBlankLines}{\vspace{4\baselineskip}}
\newcommand{\EightBlankLines}{\vspace{8\baselineskip}}
% -------------------------------------------------
% Title page variables
% -------------------------------------------------
\newcommand{\thesistitle}[1]{\def\@thesistitle{#1}}
\newcommand{\thesisauthor}[1]{\def\@thesisauthor{#1}}
\newcommand{\regnumber}[1]{\def\@regnumber{#1}}
\newcommand{\thesisdegree}[1]{\def\@thesisdegree{#1}}
\newcommand{\thesisdept}[1]{\def\@thesisdept{#1}}
\newcommand{\thesisuniv}[1]{\def\@thesisuniv{#1}}
\newcommand{\thesisaddress}[1]{\def\@thesisaddress{#1}}
\newcommand{\thesisdate}[1]{\def\@thesisdate{#1}}
\newcommand{\thesislogo}[1]{\def\@thesislogo{#1}}


% -------------------------------------------------
% First Title Page (MBU – EXACT ORDER)
% -------------------------------------------------
\renewcommand{\maketitle}{
\begin{titlepage}
\centering

% Title
{\fontsize{20}{22}\selectfont\CalibriBlack\MakeUppercase{\@thesistitle}}

\TwoBlankLines

% A Thesis
{\fontsize{22}{24}\selectfont\MonotypeCorsiva A Thesis}

\TwoBlankLines

% Submitted to
{\fontsize{14}{16}\selectfont\MakeUppercase{Submitted to}}

\OneBlankLine

% University Name
{\fontsize{16}{18}\selectfont\CalibriBlack\MakeUppercase{\@thesisuniv}}

\ThreeBlankLines

% -------- LOGO (CENTERED) --------
\includegraphics[width=2.5in,height=1.5in]{\@thesislogo}
% --------------------------------

\ThreeBlankLines

% For Award Of
{\fontsize{14}{16}\selectfont\MakeUppercase{For Award of}}

\OneBlankLine

% Degree
{\fontsize{16}{18}\selectfont\CalibriBlack\MakeUppercase{The Degree of Doctor of Philosophy}}

\TwoBlankLines

% By
{\fontsize{14}{16}\selectfont By}

\OneBlankLine

% Research Scholar Name
{\fontsize{16}{18}\selectfont\CalibriBlack\MakeUppercase{\@thesisauthor}}

\TwoBlankLines
% ----------------------------------

% Department
{\fontsize{16}{18}\selectfont\CalibriBlack\MakeUppercase{\@thesisdept}}

% \OneBlankLine

% University name
{\fontsize{18}{16}\selectfont\CalibriBlack\MakeUppercase{\@thesisuniv},}

% \OneBlankLine

% Address
{\fontsize{14}{16}\selectfont\CalibriBlack \@thesisaddress}

% \ThreeBlankLines

% % Department
% {\fontsize{16}{18}\selectfont\CalibriBlack\MakeUppercase{\@thesisdept}}

\end{titlepage}

% % -------- EMPTY PAGE AFTER PAGE --------
% \clearpage
% \thispagestyle{empty}
% \mbox{}
% \clearpage
% \newcommand{\blankpagenonumber}{
%   \clearpage
%   \thispagestyle{empty}
%   \null
%   \clearpage
% }

}



% -------------------------------------------------
% Second Title Page – Full MBU Requirement (WITH LOGO)
% -------------------------------------------------
\newcommand{\makesecondtitle}{
\begin{titlepage}
\centering

% Title
{\fontsize{15}{17}\selectfont\CalibriBlack\MakeUppercase{\@thesistitle}}

\TwoBlankLines

% A THESIS
{\fontsize{12}{14}\selectfont\CalibriBlack\MakeUppercase{A Thesis}}

\OneBlankLine

% submitted by
{\fontsize{12}{14}\selectfont submitted by}

\TwoBlankLines

% Scholar name
{\fontsize{15}{17}\selectfont\CalibriBlack\MakeUppercase{\@thesisauthor}}

\OneBlankLine

% Regd No
{\fontsize{12}{14}\selectfont Regd No : \@regnumber}

\TwoBlankLines

% for the award of the degree
{\fontsize{12}{14}\selectfont for the award of the degree}

\OneBlankLine

% of
{\fontsize{12}{14}\selectfont of}

\OneBlankLine

% Degree
{\fontsize{15}{17}\selectfont\CalibriBlack\MakeUppercase{\@thesisdegree}}

\TwoBlankLines

% -------- LOGO (ADDED HERE) --------
\includegraphics[width=3in,height=2in]{\@thesislogo}

\TwoBlankLines
% ----------------------------------

% Department
{\fontsize{16}{18}\selectfont\CalibriBlack\MakeUppercase{\@thesisdept}}

% \OneBlankLine

% University name
{\fontsize{18}{16}\selectfont\CalibriBlack\MakeUppercase{\@thesisuniv},}

% \OneBlankLine

% Address
{\fontsize{14}{16}\selectfont\CalibriBlack \@thesisaddress}

\TwoBlankLines

% Date
{\fontsize{15}{17}\selectfont\CalibriBlack\MakeUppercase{\@thesisdate}}

\end{titlepage}

% % -------- EMPTY PAGE AFTER PAGE --------
% \clearpage
% \thispagestyle{empty}
% \mbox{}
% \clearpage

}


% -------------------------------------------------
% Dedication Page (Centered, 24pt, Bold Italic, Calibri)
% -------------------------------------------------
\newcommand{\makededication}[1]{
\begin{titlepage}
\centering

\vspace*{\fill}

{\fontsize{24}{28}\selectfont\bfseries\itshape #1}

\vspace*{\fill}

\end{titlepage}

% % -------- EMPTY PAGE AFTER PAGE --------
% \clearpage
% \thispagestyle{empty}
% \mbox{}
% \clearpage

}


% -------------------------------------------------
% Declaration Page (Final Layout)
% -------------------------------------------------
\newcommand{\makedeclaration}[3]{
\begin{titlepage}
\centering

% Heading
{\fontsize{15}{17}\selectfont\CalibriBlack\MakeUppercase{Declaration}}

\FourBlankLines

% Declaration text
\begin{justify}
{\fontsize{12}{14}\selectfont #1}
\end{justify}

\vspace{2cm}

% Name & Date bottom-right
\begin{flushright}
{\fontsize{12}{14}\selectfont
#2 \\
#3
}
\end{flushright}

\end{titlepage}
}



% -------------------------------------------------
% Certificate Page
% -------------------------------------------------
\newcommand{\makecertificate}[3]{
\begin{titlepage}
\centering

% Heading
{\fontsize{15}{17}\selectfont\CalibriBlack\MakeUppercase{Certificate}}

\FourBlankLines

% Certificate text (Justified)
\begin{justify}
{\fontsize{12}{14}\selectfont #1}
\end{justify}

\EightBlankLines

% Signatures row
\noindent
\begin{minipage}[t]{0.48\textwidth}
{\fontsize{12}{14}\selectfont
#2
}
\end{minipage}
\hfill
\begin{minipage}[t]{0.42\textwidth}
{\fontsize{12}{14}\selectfont
#3
}
\end{minipage}

\end{titlepage}

% % -------- EMPTY PAGE AFTER PAGE --------
% \clearpage
% \thispagestyle{empty}
% \mbox{}
% \clearpage

}



% -------------------------------------------------
% Acknowledgements Page
% -------------------------------------------------
\newcommand{\makeacknowledgements}[2]{
\begin{mbupage}
\phantomsection
\label{toc:ack}
\centering

% Heading
{\fontsize{15}{17}\selectfont\CalibriBlack\MakeUppercase{Acknowledgements}}

\OneBlankLine

% Acknowledgements text (Justified)
\begin{justify}
{\fontsize{12}{14}\selectfont #1}
\end{justify}

% Author name (Right aligned)
\begin{flushright}
{\fontsize{12}{14}\selectfont #2}
\end{flushright}

\end{mbupage}

% % -------- EMPTY PAGE AFTER PAGE --------
% \clearpage
% \thispagestyle{empty}
% \mbox{}
% \clearpage



% \stepcounter{page} % <<< THIS IS THE FIX
}




% -------------------------------------------------
% Abstract Page
% -------------------------------------------------
\newcommand{\makeabstract}[3]{
\begin{mbupage}
\phantomsection
\label{toc:abs}
\centering

% Heading
{\fontsize{15}{17}\selectfont\CalibriBlack\MakeUppercase{Abstract}}

\OneBlankLine

% Abstract Title
{\fontsize{14}{16}\selectfont\bfseries #1}

\OneBlankLine

% Keywords
{\fontsize{12}{14}\selectfont
\begin{list}{}%
  {\leftmargin=2.6cm \labelwidth=2.6cm \labelsep=0pt}
  \item[\MakeUppercase{Keywords: }] #2
\end{list}
}

\OneBlankLine

% Abstract Body
\begin{justify}
{\fontsize{12}{14}\selectfont #3}
\end{justify}

\end{mbupage}

% % -------- EMPTY PAGE AFTER PAGE --------
% \clearpage
% \thispagestyle{empty}
% \mbox{}
% \clearpage

}




% -------------------------------------------------
% Table of Contents Page
% -------------------------------------------------
\newcommand{\maketableofcontentspage}{
\begin{mbupage}

\thispagestyle{empty}

\centering
{\fontsize{15}{17}\selectfont\CalibriBlack TABLE OF CONTENTS}

\TwoBlankLines

\tableofcontents   % <-- STANDARD DYNAMIC COMMAND

\end{mbupage}



% % -------- EMPTY PAGE AFTER PAGE --------
% \clearpage
% \thispagestyle{empty}
% \mbox{}
% \clearpage

}


% -------------------------------------------------
% List of Tables Page
% -------------------------------------------------
\newcommand{\makelistoftablespage}{
\begin{mbupage}

\centering
{\fontsize{15}{17}\selectfont\CalibriBlack LIST OF TABLES}

\TwoBlankLines

\listoftables   % <-- DYNAMIC

\end{mbupage}


% % -------- EMPTY PAGE AFTER PAGE --------
% \clearpage
% \thispagestyle{empty}
% \mbox{}
% \clearpage

}




% -------------------------------------------------
% List of Figures Page
% -------------------------------------------------
\newcommand{\makelistoffigurespage}{
\begin{mbupage}

\centering
{\fontsize{15}{17}\selectfont\CalibriBlack LIST OF FIGURES}

\TwoBlankLines

\listoffigures   

\end{mbupage}


% % -------- EMPTY PAGE AFTER PAGE --------
% \clearpage
% \thispagestyle{empty}
% \mbox{}
% \clearpage

}



% -------------------------------------------------
% Abbreviations Page
% -------------------------------------------------
\newcommand{\makeabbreviationspage}{
\begin{mbupage}

\centering
{\fontsize{15}{17}\selectfont\CalibriBlack ABBREVIATIONS}

\TwoBlankLines

\printnomenclature[1cm]

\end{mbupage}


% % -------- EMPTY PAGE AFTER PAGE --------
% \clearpage
% \thispagestyle{empty}
% \mbox{}
% \clearpage

}

% -------------------------------------------------
% Notations Page (ADD THIS BELOW ABBREVIATIONS)
% -------------------------------------------------
\newcommand{\makenotationspage}{
\begin{mbupage}

\centering
{\fontsize{15}{17}\selectfont\CalibriBlack NOTATIONS}

\TwoBlankLines

\printnomenclature[1cm]

\end{mbupage}



% % -------- EMPTY PAGE AFTER PAGE --------
% \clearpage
% \thispagestyle{empty}
% \mbox{}
% \clearpage

}

% -------------------------------------------------
% Command to add notation entries
% -------------------------------------------------
\newcommand{\NOTentry}[2]{%
  \addcontentsline{not}{section}{#1 \hfill #2}
}

% ------------------------------------------------
% Chapter formatting (MBU – exact spacing & centering)
% ------------------------------------------------
\RequirePackage{titlesec}

\titleformat{\chapter}[display]
  {\centering\CalibriBlack}
  {\fontsize{14}{16}\selectfont\MakeUppercase{CHAPTER~\thechapter}}
  {18pt} % space between CHAPTER 1 and title
  {\fontsize{16}{22}\selectfont\MakeUppercase}

% Exact vertical spacing as per MBU sample
\titlespacing*{\chapter}
  {0pt}
  {24pt}  % space from top margin to CHAPTER 1
  {36pt}  % space after INTRODUCTION

% ------------------------------------------------
% Section formatting (left-aligned)
% ------------------------------------------------
\titleformat{\section}
  {\normalfont\fontsize{14}{15}\selectfont\bfseries}
  {\thesection}
  {1em}
  {}

\titlespacing*{\section}
  {0pt}
  {18pt}
  {12pt}

% ------------------------------------------------
% Subsection formatting
% ------------------------------------------------
\titleformat{\subsection}
  {\normalfont\fontsize{12}{15}\selectfont\bfseries}
  {\thesubsection}
  {1em}
  {}

\titlespacing*{\subsection}
  {0pt}
  {12pt}
  {6pt}

% ------------------------------------------------
% Paragraph indentation (MBU thesis standard)
% ------------------------------------------------
\setlength{\parindent}{1.27cm}
\setlength{\parskip}{0pt}

% Chapter page should be plain (no header)
\makeatletter
\renewcommand{\chapter}{%
  \if@openright\cleardoublepage\else\clearpage\fi
  \thispagestyle{plain}%
  \global\@topnum\z@
  \@afterindentfalse
  \secdef\@chapter\@schapter
}
\makeatother

% Ensure correct chapter title in header (not uppercase)
\renewcommand{\chaptermark}[1]{%
  \markboth{#1}{}
}

% ------------------------------------------------
% Mirrored headers and footers (Clean Version)
% ------------------------------------------------
\RequirePackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{} % clear all header and footer fields

% -------------------------------
% Mirrored Headers
% -------------------------------
% Right page (odd page) header
\fancyhead[RO]{\itshape \leftmark}

% Left page (even page) header
\fancyhead[LE]{\itshape \leftmark}

% -------------------------------
% Footer (Page number centered)
% -------------------------------
\fancyfoot[C]{\thepage}

\renewcommand{\headrulewidth}{0pt}

% ------------------------------------------------
% Chapter opening pages (plain style)
% ------------------------------------------------
\fancypagestyle{plain}{
  \fancyhf{}
  \fancyfoot[C]{\thepage}
  \renewcommand{\headrulewidth}{0pt}
}

% ------------------------------------------------
% Remove header & footer on automatically inserted blank pages
% ------------------------------------------------
\makeatletter
\renewcommand{\cleardoublepage}{%
  \clearpage
  \if@openright
    \ifodd\c@page
    \else
      \thispagestyle{empty}
      \null
      \clearpage
    \fi
  \fi
}
\makeatother


% Odd pages (right side)
% Headers only (mirrored)
\fancyhead[RO]{\itshape  \leftmark}
\fancyhead[LE]{\itshape \leftmark}

% Footer — SINGLE definition
\fancyfoot[C]{\thepage}


\renewcommand{\headrulewidth}{0pt}



% ------------------------------------------------
% Ensure page number appears on chapter opening pages
% ------------------------------------------------
\fancypagestyle{plain}{
  \fancyhf{}
  \fancyfoot[C]{\thepage}
  \renewcommand{\headrulewidth}{0pt}
}


