\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{thesis}[2025/05/10 Bachelor Thesis Class]

\newif\if@draftmark
\@draftmarkfalse

\DeclareOption{draftmark}{\@draftmarktrue}
\ProcessOptions \relax
\LoadClass[a4paper,12pt,oneside]{book}

% Load common packages
\RequirePackage{polyglossia}
\RequirePackage{fontspec}
\RequirePackage{titlesec}
\RequirePackage{fancyhdr}
\RequirePackage{geometry}
\RequirePackage{setspace}
\RequirePackage{graphicx}
\RequirePackage{hyperref}
\RequirePackage{etoolbox}
\RequirePackage{tocloft}
\RequirePackage{tocbibind}
\RequirePackage{amsmath,amsfonts,amssymb}
\RequirePackage{svg}           % Allows including SVG images directly
\RequirePackage{indentfirst}   % Makes first paragraph after headings indented
\RequirePackage{float}         % Provides [H] option to force figure/table placement

% Polyglossia set language
\setmainlanguage{bahasai}
% \setotherlanguage{english}

% Conditionally load the watermark package and settings
\if@draftmark
  \RequirePackage{draftwatermark}
  \SetWatermarkText{Draft: \today [wip]}
  \SetWatermarkColor[gray]{0.7}
  \SetWatermarkFontSize{2cm}
  \SetWatermarkAngle{90}
  \SetWatermarkHorCenter{1.5cm}
\fi

% Page layout
\geometry{left=3cm, top=3cm, right=3cm, bottom=3cm}
\setlength{\parskip}{0.5em}
\setlength{\parindent}{0pt}
\onehalfspacing

% Fonts
\defaultfontfeatures{Ligatures=TeX}
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont{Courier New}

% Metadata commands
\input{metadata}

\newcommand{\setthesisinfo}[7]{%
  \renewcommand{\thesistitle}{#1}%
  \renewcommand{\studentname}{#2}%
  \renewcommand{\studentid}{#3}%
  \renewcommand{\program}{#4}%
  \renewcommand{\faculty}{#5}%
  \renewcommand{\university}{#6}%
  \renewcommand{\yearofsubmission}{#7}%
}

% % Header and footer
\fancypagestyle{fancy}{%
    \fancyhf{}
    \fancyhead[R]{\nouppercase{\rightmark}}
    \fancyhead[L]{\nouppercase{\leftmark}}
    \fancyfoot[C]{\thepage}
}
\fancypagestyle{fancyplainfrontmatter}{%
    \renewcommand{\headrulewidth}{0pt}
    \fancyfoot[C]{\thepage}
}
\fancypagestyle{fancyplain}{%
    \fancyhf{}
    \renewcommand{\headrulewidth}{0pt}
    \fancyhead[R]{\thepage}
}

% Chapter formatting
\titlespacing{\chapter}{0pt}{0pt}{*1.5}
\titleformat{\chapter}[display]
  {\normalsize\bfseries\centering}
  {BAB~\Roman{chapter}} % << display format
  {1ex}
  {\MakeUppercase}
\titleformat{\section}
    {\normalsize\bfseries}{\thesection}{1em}{}

\titleformat{\subsection}
    {\normalsize\bfseries}{\thesubsection}{1em}{}

% Ensure chapter reference in TOC matches
\renewcommand{\cftchappresnum}{BAB~}
\renewcommand{\cftchapaftersnum}{\quad}

% \titlespacing*{\chapter}{0pt}{-10pt}{20pt}

% Redefine \maketitle
\renewcommand{\maketitle}{\input{frontmatter/maketitle}}

% Chapter & Section format
\renewcommand{\cftchapfont}{\normalsize\MakeUppercase}
% \renewcommand{\cftsecfont}{}
% \renewcommand{\cftsubsecfont}{\itshape}
% \renewcommand{\thesection}{\textup{\Roman{chapter}}.\arabic{section}}


% Dot leaders, spacing, indentation
\setlength{\cftbeforechapskip}{0em}
\setlength{\cftchapindent}{0pt}
\setlength{\cftsecindent}{0em}
\setlength{\cftsubsecindent}{2.5em}
\setlength{\cftchapnumwidth}{3.5em}
\setlength{\cftsecnumwidth}{3.5em}
\setlength{\cftsubsecnumwidth}{2.5em}
\setlength{\cftfignumwidth}{5em}
\setlength{\cfttabnumwidth}{4em}
\renewcommand \cftchapdotsep{4.5} % https://tex.stackexchange.com/a/273764
\renewcommand{\cftchapleader}{\normalfont\cftdotfill{\cftsecdotsep}}
\renewcommand{\cftchappagefont}{\normalfont}
\renewcommand{\cftfigpresnum}{\figurename~}
\renewcommand{\cfttabpresnum}{\tablename~}

% Ensure TOC and References Respect Custom Numbering
\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand\thesection{\arabic{chapter}.\arabic{section}}

% Change figure numbering to include chapter (e.g., Figure 1.1, 1.2...)
\renewcommand{\thefigure}{\arabic{chapter}.\arabic{figure}}
\renewcommand{\thetable}{\arabic{chapter}.\arabic{table}}
\renewcommand{\theequation}{\arabic{chapter}.\arabic{equation}}

% Table of Content (TOC) Title styling
\renewcommand{\cfttoctitlefont}{\hfill\bfseries\MakeUppercase}
\renewcommand{\cftaftertoctitle}{\hfill} % https://tex.stackexchange.com/a/255699/394075
% List of Figures (LOF) Title styling
\renewcommand{\cftloftitlefont}{\hfill\bfseries\MakeUppercase}
\renewcommand{\cftafterloftitle}{\hfill}
% List of Tables (LOT) Title styling
\renewcommand{\cftlottitlefont}{\hfill\bfseries\MakeUppercase}
\renewcommand{\cftafterlottitle}{\hfill}
% \renewcommand{\cfttoctitlefont}{\bfseries\MakeUppercase}
% \renewcommand{\cftaftertoctitle}{\vskip 2em}

% % Apply a custom fancyhdr layout only on the first page of each \chapter, and use no header/footer elsewhere
% % \let\oldchapter\chapter
% % \renewcommand{\chapter}{%
% %   \cleardoublepage
% %   \pagestyle{fancyplainchapter}%
% %   \oldchapter
% %   \thispagestyle{fancyplainchapter} % ensure chapter start page uses it
% %   \pagestyle{fancyplain}% switch for subsequent pages
% % }

\endinput