% Internal Chinese UI bindings for *_zh wrapper classes.

\ifdefined\NextSystemUiZhInternalLoaded
  \endinput
\fi
\def\NextSystemUiZhInternalLoaded{1}

\RequirePackage{etoolbox}
\RequirePackage{xparse}
\RequirePackage{zhnumber}

\makeatletter
\newcommand{\NextUiZhSetName}[2]{%
  \@ifundefined{#1}
    {\expandafter\def\csname #1\endcsname{#2}}
    {\expandafter\renewcommand\csname #1\endcsname{#2}}%
}

\newcommand{\UseZhInlineTitleSubtitle}{%
  \renewcommand{\NextTitleBlock}{%
    \begin{center}%
      {\NextTitleFont
        \@title
        \ifx\@subtitle\@empty
        \else
          ：\@subtitle
        \fi
        \par}%
      \vskip 1.5em%
      {\NextTitleAuthorFont
        \lineskip 0.5em%
        \begin{tabular}[t]{c}%
          \@author
        \end{tabular}\par}%
      \vskip 1em%
      {\NextTitleDateFont \@date \par}%
    \end{center}%
  }%
}
\newcommand{\NextUseZhInlineTitleSubtitle}{\UseZhInlineTitleSubtitle}

\newif\ifNextUiZhDateUserSet
\def\NextUiZhDefaultDate{\today}%
\ifx\@date\NextUiZhDefaultDate
  \NextUiZhDateUserSetfalse
\else
  \NextUiZhDateUserSettrue
\fi
\let\NextUiZhOriginalDate\date
\protected\def\date{%
  \NextUiZhDateUserSettrue
  \NextUiZhOriginalDate
}

\newcommand{\NextUiZhApplyDate}{%
  \def\today{\number\year 年\number\month 月\number\day 日}%
  \@ifclassloaded{beamer}
    {%
      \ifNextUiZhDateUserSet\else
        \NextUiZhOriginalDate[\number\year-\number\month-\number\day]{%
          \protect\mbox{\number\year 年\number\month 月\number\day 日}%
        }%
      \fi
    }
    {%
      \ifNextUiZhDateUserSet\else
        \NextUiZhOriginalDate{\today}%
      \fi
    }%
}

\newcommand{\NextUiZhApplyChapterFormat}{%
  \renewcommand{\thechapter}{第\zhnumber{\arabic{chapter}}章}%
  \NextUiZhApplySectionNumbering
  \renewcommand{\thefigure}{\arabic{chapter}.\arabic{figure}}%
  \renewcommand{\thetable}{\arabic{chapter}.\arabic{table}}%
  \def\@makechapterhead##1{%
    \vspace*{50\p@}%
    {\parindent \z@ \raggedright \normalfont
      \ifnum \c@secnumdepth >\m@ne
        \@ifundefined{if@mainmatter}{%
          \huge\bfseries \thechapter
          \par\nobreak
          \vskip 20\p@
        }{%
          \if@mainmatter
            \huge\bfseries \thechapter
            \par\nobreak
            \vskip 20\p@
          \fi
        }%
      \fi
      \interlinepenalty\@M
      \Huge \bfseries ##1\par\nobreak
      \vskip 40\p@
    }}%
}

\newcommand{\NextUiZhApplySectionNumbering}{%
  \setcounter{secnumdepth}{3}%
  \setcounter{tocdepth}{3}%
  \renewcommand{\thesection}{\zhnumber{\arabic{section}}}%
  \renewcommand{\thesubsection}{（\zhnumber{\arabic{subsection}}）}%
  \renewcommand{\thesubsubsection}{\arabic{subsubsection}.}%
}

\newdimen\NextUiZhTocMinLabelWidth
\newdimen\NextUiZhTocLabelWidth
\newcommand{\NextUiZhTocNumberline}[1]{%
  \settowidth{\NextUiZhTocLabelWidth}{#1\hspace{0.8em}}%
  \ifdim\NextUiZhTocLabelWidth<\NextUiZhTocMinLabelWidth
    \NextUiZhTocLabelWidth=\NextUiZhTocMinLabelWidth
  \fi
  \hb@xt@\NextUiZhTocLabelWidth{#1\hfil}%
}
\newcommand{\NextUiZhDottedTocLine}[5]{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax
     \rightskip \@tocrmarg
     \parfillskip -\rightskip
     \parindent #2\relax
     \@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \NextUiZhTocMinLabelWidth=#3\relax
     \advance\leftskip \NextUiZhTocMinLabelWidth
     \null\nobreak\hskip -\leftskip
     \begingroup
       \let\numberline\NextUiZhTocNumberline
       #4%
     \endgroup
     \nobreak
     \leaders\hbox{$\m@th
       \mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont\normalcolor #5}%
     \par}%
  \fi
}

\newcommand{\NextUiZhApplyTocSpacing}{%
  \@ifclassloaded{beamer}{}{%
    \@ifundefined{@dottedtocline}{}{%
      \@ifundefined{chapter}{}{%
        \renewcommand*{\l@chapter}{\NextUiZhDottedTocLine{0}{0em}{1.5em}}%
      }%
      \renewcommand*{\l@section}{\NextUiZhDottedTocLine{1}{2em}{2.3em}}%
      \renewcommand*{\l@subsection}{\NextUiZhDottedTocLine{2}{4em}{3.2em}}%
      \renewcommand*{\l@subsubsection}{\NextUiZhDottedTocLine{3}{6em}{4.1em}}%
    }%
  }%
}

\newcommand{\NextUiZhApplySectionFormat}{%
  \NextUiZhApplySectionNumbering
  \NextUiZhApplyTocSpacing
}

\newcommand{\NextUiZhApplyCaptionFormat}{%
  \@ifundefined{fnum@figure}{}{%
    \renewcommand{\fnum@figure}{\figurename\thefigure}%
  }%
  \@ifundefined{fnum@table}{}{%
    \renewcommand{\fnum@table}{\tablename\thetable}%
  }%
}

\newif\ifNextUiZhStarredToc
\NextUiZhStarredToctrue
\newcommand{\NoTocNext}{\global\NextUiZhStarredTocfalse}
\newcommand{\NextNoTocNext}{\NoTocNext}
\newcommand{\NextUiZhMaybeAddContents}[2]{%
  \ifNextUiZhStarredToc
    \@ifpackageloaded{hyperref}{\phantomsection}{}%
    \addcontentsline{toc}{#1}{#2}%
  \else
    \global\NextUiZhStarredToctrue
  \fi
}
\newcommand{\NextUiZhPatchStarredToc}{%
  \@ifclassloaded{beamer}{}{%
    \@ifundefined{NextUiZhStarredTocPatched}{%
      \gdef\NextUiZhStarredTocPatched{1}%
      \let\NextUiZhOriginalTableOfContents\tableofcontents
      \renewcommand{\tableofcontents}{%
        \NoTocNext
        \NextUiZhOriginalTableOfContents
      }%
      % hyperref-aware heading patches also wrap \chapter/\section; stacking both
      % wrappers can recurse when \tableofcontents calls starred headings.
      % Keep the TOC guard active; hyperlinks.tex owns the heading wrappers.
      \@ifpackageloaded{hyperref}{}{%
        \@ifundefined{chapter}{}{%
          \let\NextUiZhOriginalChapter\chapter
          \RenewDocumentCommand{\chapter}{s o +m}{%
            \IfBooleanTF{##1}{%
              \NextUiZhOriginalChapter*{##3}%
              \IfNoValueTF{##2}
                {\NextUiZhMaybeAddContents{chapter}{##3}}
                {\NextUiZhMaybeAddContents{chapter}{##2}}%
            }{%
              \IfNoValueTF{##2}
                {\NextUiZhOriginalChapter{##3}}
                {\NextUiZhOriginalChapter[##2]{##3}}%
            }%
          }%
        }%
        \let\NextUiZhOriginalSection\section
        \RenewDocumentCommand{\section}{s o +m}{%
          \IfBooleanTF{##1}{%
            \NextUiZhOriginalSection*{##3}%
            \IfNoValueTF{##2}
              {\NextUiZhMaybeAddContents{section}{##3}}
              {\NextUiZhMaybeAddContents{section}{##2}}%
          }{%
            \IfNoValueTF{##2}
              {\NextUiZhOriginalSection{##3}}
              {\NextUiZhOriginalSection[##2]{##3}}%
          }%
        }%
      }%
    }{}%
  }%
}

\AtBeginDocument{%
  \NextUiZhSetName{abstractname}{摘要}%
  \NextUiZhSetName{contentsname}{目錄}%
  \NextUiZhSetName{indexname}{術語索引}%
  \NextUiZhSetName{figurename}{圖}%
  \NextUiZhSetName{tablename}{表}%
  \NextUiZhSetName{appendixname}{附錄}%
  \@ifundefined{bibname}{}{\renewcommand{\bibname}{參考文獻}}%
  \@ifundefined{refname}{}{\renewcommand{\refname}{參考文獻}}%
  \@ifundefined{AgendaTitle}{}{\renewcommand{\AgendaTitle}{目錄}}%
  \NextUiZhApplyDate
  \NextUiZhApplySectionFormat
  \NextUiZhApplyCaptionFormat
  \NextUiZhPatchStarredToc
  \@ifclassloaded{book}{%
    \NextUiZhApplyChapterFormat
  }{}%
  \@ifclassloaded{report}{%
    \NextUiZhApplyChapterFormat
  }{}%
}

\@ifundefined{dateenglish}
  {}
  {
    \renewcommand{\dateenglish}{%
      \def\today{\number\year 年\number\month 月\number\day 日}%
    }
  }

\@ifundefined{captionsenglish}
  {}
  {
    \gappto\captionsenglish{%
      \NextUiZhSetName{abstractname}{摘要}%
      \NextUiZhSetName{contentsname}{目錄}%
      \NextUiZhSetName{figurename}{圖}%
      \NextUiZhSetName{tablename}{表}%
      \NextUiZhSetName{appendixname}{附錄}%
      \NextUiZhSetName{bibname}{參考文獻}%
    }
  }
\makeatother
