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

\newif\if@draftmark \@draftmarkfalse
\@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
\RequirePackage[style=apa, backend=biber, language=indonesian]{biblatex}
\RequirePackage[acronym, nogroupskip, toc]{glossaries}
% Polyglossia set language
\setdefaultlanguage[variant=indonesian]{malay}  % Proper Indonesian language setup
\setotherlanguage{english}             % Enables English as secondary language
\DefineBibliographyStrings{english}{%  % Customizes bibliography text
  andothers={dkk\adddot},              % Changes "et al." to "dkk."
  pages={hlm\adddot},                  % Changes "pp." to "hlm."
}

% Conditionally load the watermark package and settings
\if@draftmark
  \RequirePackage{draftwatermark}
  \SetWatermarkText{nuluh/thesis (wip) [draft: \today]}
  \SetWatermarkColor[gray]{0.8}                    % Opacity: 0.8 = 20% transparent  
  \SetWatermarkFontSize{1.5cm}
  \SetWatermarkAngle{90}
  \SetWatermarkHorCenter{1.5cm}
  \RequirePackage[left]{lineno}
  \linenumbers
\fi

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

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


\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}{0cm}{*1.5}    % 0pt→0cm: same value, different unit
                                             % 0pt = no space above chapter title
                                             % *1.5 = 1.5× line spacing after title

\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}{}

% Section numbering depth
\setcounter{secnumdepth}{3}               % Enables numbering for:
                                          % 1 = chapters, 2 = sections, 3 = subsections

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

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

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


% Dot leaders, spacing, indentation
\setlength{\cftbeforetoctitleskip}{0cm}   % Space above "DAFTAR ISI" title
\setlength{\cftbeforeloftitleskip}{0cm}   % Space above "DAFTAR GAMBAR" title  
\setlength{\cftbeforelottitleskip}{0cm}   % Space above "DAFTAR TABEL" title

\setlength{\cftbeforechapskip}{0em}
\setlength{\cftchapindent}{0pt}
\setlength{\cftsecindent}{0em}
\setlength{\cftsubsecindent}{2em}
\setlength{\cftchapnumwidth}{3.5em}
\setlength{\cftsecnumwidth}{2em}
\setlength{\cftsubsecnumwidth}{2.5em}
\setlength{\cftfignumwidth}{5em}
\setlength{\cfttabnumwidth}{4em}
\renewcommand \cftchapdotsep{1}           % Denser dots (closer together) https://tex.stackexchange.com/a/273764
\renewcommand \cftsecdotsep{1}            % Apply to sections too
\renewcommand \cftsubsecdotsep{1}         % Apply to subsections too
\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
% % }

% Define the header for the location column
\providecommand*{\locationname}{Location}

% Define the new glossary style called 'mylistalt' for main glossaries
\makeatletter
\newglossarystyle{mylistalt}{%
  % start the list, initializing glossaries internals
  \renewenvironment{theglossary}%
    {\glslistinit\begin{enumerate}}%
    {\end{enumerate}}%
  % suppress all headers/groupskips
  \renewcommand*{\glossaryheader}{}%
  \renewcommand*{\glsgroupheading}[1]{}%
  \renewcommand*{\glsgroupskip}{}%
  % main entries: let \item produce "1." etc., then break
  \renewcommand*{\glossentry}[2]{%
    \item \glstarget{##1}{\glossentryname{##1}}%
    \mbox{}\\
    \glossentrydesc{##1}\space 
    [##2] % appears on page x
  }%
  % sub-entries as separate paragraphs, still aligned
  \renewcommand*{\subglossentry}[3]{%
    \par
    \glssubentryitem{##2}%
    \glstarget{##2}{\strut}\space
    \glossentrydesc{##2}\space ##3%
  }%
}


% Define the new glossary style 'altlong3customheader' for notation
\newglossarystyle{altlong3customheader}{%
  % The glossary will be a longtable environment with three columns:
  % 1. Symbol (left-aligned)
  % 2. Description (paragraph, width \glsdescwidth)
  % 3. Location (paragraph, width \glspagelistwidth)
  \renewenvironment{theglossary}%
    {\begin{longtable}{lp{\glsdescwidth}p{\glspagelistwidth}}}%
    {\end{longtable}}%
  % Define the table header row
  \renewcommand*{\symbolname}{Simbol}
  \renewcommand*{\descriptionname}{Keterangan}
  \renewcommand*{\locationname}{Halaman}
  \renewcommand*{\glossaryheader}{%
    \bfseries\symbolname & \bfseries\descriptionname & \bfseries\locationname \tabularnewline\endhead}%
  % Suppress group headings (e.g., A, B, C...)
  \renewcommand*{\glsgroupheading}[1]{}%
  % Define how a main glossary entry is displayed
  % ##1 is the entry label
  % ##2 is the location list (page numbers)
  \renewcommand{\glossentry}[2]{%
    \glsentryitem{##1}% Inserts entry number if entrycounter option is used
    \glstarget{##1}{\glossentryname{##1}} & % Column 1: Symbol (with hyperlink target)
    \glossentrydesc{##1}\glspostdescription & % Column 2: Description (with post-description punctuation)
    ##2\tabularnewline % Column 3: Location list
  }%
  % Define how a sub-entry is displayed
  % ##1 is the sub-entry level (e.g., 1 for first sub-level)
  % ##2 is the entry label
  % ##3 is the location list
  \renewcommand{\subglossentry}[3]{%
    & % Column 1 (Symbol) is left blank for sub-entries to create an indented look
    \glssubentryitem{##2}% Inserts sub-entry number if subentrycounter is used
    \glstarget{##2}{\strut}\glossentrydesc{##2}\glspostdescription & % Column 2: Description (target on strut for hyperlink)
    ##3\tabularnewline % Column 3: Location list
  }%
  % Define the skip between letter groups (if group headings were enabled)
  % For 3 columns, we need 2 ampersands for a full blank row if not using \multicolumn
  \ifglsnogroupskip
    \renewcommand*{\glsgroupskip}{}%
  \else
    \renewcommand*{\glsgroupskip}{& & \tabularnewline}%
  \fi
}
\makeatother

\endinput