%%%==============================================================================
%% Copyright 2022-23 by Alceu Frigeri
%%
%% This work may be distributed and/or modified under the conditions of
%%
%% * The [LaTeX Project Public License](http://www.latex-project.org/lppl.txt),
%%   version 1.3c (or later), and/or
%% * The [GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.html),
%%   version 3 (or later)
%%
%% This work has the LPPL maintenance status *maintained*.
%%
%% The Current Maintainer of this work is Alceu Frigeri
%%
%% This is version 1.0.7 (2023/01/17)
%%
%% The list of files that compose this work can be found in the README.md file at
%% https://ctan.org/pkg/ufrgscca
%%
%%%==============================================================================
%% UFRGS stands for "Federal University of Rio Grande do Sul" in south Brazil
%% EE    stands for "Engineering School"
%% CCA   stands for "Control and Automation Engineering Course" (Portuguese acronym)
%%%==============================================================================
\NeedsTeXFormat{LaTeX2e}[2022/06/01]
\def\tc@tmp@packname{ufrgscca-curr}
\def\tc@tmp@altpackname{ufrgsccacurr}
\def\tc@tmp@packdesc{UFRGS/CCA curricula commands}
\def\tc@tmp@packdate{2023/01/17}
\def\tc@tmp@packversion{1.0.7beta}
\let\@exp\expandafter
\@exp\edef\csname\tc@tmp@altpackname version\endcsname{\tc@tmp@packversion}
\@exp\@exp\@exp\ProvidesPackage\@exp\@exp\@exp{\@exp\tc@tmp@packname\@exp}\@exp[\tc@tmp@packdate\space v\tc@tmp@packversion\space\tc@tmp@packdesc]


\newcounter{gr@credob}
\newcounter{gr@credel}
\newcounter{gr@credalt}
\NewDocumentCommand{\gr@setcred}{}{\setcounter{gr@credob}{0}\setcounter{gr@credel}{0}\setcounter{gr@credalt}{0}}
\NewDocumentCommand{\gr@addclasscred}{m}{\addtocounter{gr@cred\csname gr@class:#1.typ\endcsname}{\csname gr@class:#1.cred\endcsname}}

\let\@ex\expandafter

%%%%%%%%%%%
%%%%%%%%%%%
%%%%%%%%%%%

\NewDocumentCommand\topicdef{O{black}mm}{
    \expandafter\gdef\csname gr@topic:#2.txt\endcsname{#3}%
    \expandafter\gdef\csname gr@topic:#2.color\endcsname{#1}%
}

\NewDocumentCommand\defaulttopic{m}{\xdef\gr@defaulttopic{#1}}

%%%%%%%%%%%
%%%%%%%%%%%
%%%%%%%%%%%

\NewDocumentCommand\semdef{O{#3}D<>{#3}m}{%
    \expandafter\gdef\csname gr@et:#3.idx\endcsname{x#3}%
    \expandafter\gdef\csname gr@etidx:x#3.cod\endcsname{#2}%
    \expandafter\gdef\csname gr@etidx:x#3.nam\endcsname{#3}%
    \expandafter\gdef\csname gr@etidx:x#3.pos\endcsname{#1}%
    \gdef\gr@sem{#3}%
    \gdef\gr@semcod{#2}%
}
%%%%%%%%%%%
%%%%%%%%%%%
%%%%%%%%%%%

\NewDocumentCommand\classdef{O{\gr@defaulttopic}D<>{}mmmmm}{%
    \@ifundefined{gr@topic:#1.txt}%
		    {\expandafter\gdef\csname gr@class:#3.topic\endcsname{\gr@defaulttopic}}%
				{\expandafter\gdef\csname gr@class:#3.topic\endcsname{#1}}%
    \expandafter\gdef\csname gr@class:#3.pos\endcsname{#2}%
    \expandafter\gdef\csname gr@class:#3.cred\endcsname{#4}%
    \expandafter\gdef\csname gr@class:#3.typ\endcsname{#5}%
    \expandafter\gdef\csname gr@class:#3.nam\endcsname{#6}%
    \expandafter\gdef\csname gr@class:#3.desc\endcsname{#7}%
    \expandafter\xdef\csname gr@class:#3.et\endcsname{\gr@semcod}%
    \gdef\gr@disc{#3}
		\newcounter{gr@class:#3.bibcnt}
		\newcounter{gr@class:#3.basiccnt}
		\newcounter{gr@class:#3.compcnt}
		\newcounter{gr@class:#3.dep}
		\stepcounter{gr@class:#3.dep}
    \ifcsundef{gr@et:\gr@sem}{\csgdef{gr@et:\gr@sem}{#3}}{\csgappto{gr@et:\gr@sem}{,#3}}
    \ifcsundef{gr@topic:#1}{\csgdef{gr@topic:#1}{#3}}{\csgappto{gr@topic:#1}{,#3}}
}
\NewDocumentCommand{\setclass}{m}{\ifcsundef{gr@class:#1.et}{{\color{red} #1 undef\\}}{\gdef\gr@disc{#1}}}

\NewDocumentCommand{\classremark}{m}{%
   \expandafter\gdef\csname gr@class:\gr@disc.remark\endcsname{#1}%
}

%%%%%%%%%%%
%%%%%%%%%%%
%%%%%%%%%%%

\NewDocumentCommand\depdef{O{\csname gr@class:\gr@disc.topic\endcsname}D<>{}m}{%
    \ifcsundef{gr@class:\gr@disc.dep\Alph{gr@class:\gr@disc.dep}}{\csgdef{gr@class:\gr@disc.dep\Alph{gr@class:\gr@disc.dep}}{#3}}{\csgappto{gr@class:\gr@disc.dep\Alph{gr@class:\gr@disc.dep}}{,#3}}
    \expandafter\gdef\csname gr@class:\gr@disc.dep\Alph{gr@class:\gr@disc.dep}:#3.topic\endcsname{#1}%
    \expandafter\gdef\csname gr@class:\gr@disc.dep\Alph{gr@class:\gr@disc.dep}:#3.pos\endcsname{#2}%
}

%%%%%%%%%%%
%%%%%%%%%%%
%%%%%%%%%%%

\NewDocumentCommand\bibdef{O{bib}m}{%
		\stepcounter{gr@class:\gr@disc.#1cnt}%
    \expandafter\gdef\csname gr@class:\gr@disc.#1\Alph{gr@class:\gr@disc.#1cnt}\endcsname{#2}%
}


%%%%%%%%%%%
%%%%%%%%%%%
%%%%%%%%%%%


\NewDocumentCommand\altdef{}{%
	\stepcounter{gr@class:\gr@disc.dep}%
}

%%%%%%%%%%%
%%%%%%%%%%%
%%%%%%%%%%%

\NewDocumentCommand\goo{m}{}
\NewDocumentCommand\gooble{m}{}

\NewDocumentCommand\LstDep{O{\goo}D<>{A}m}{%
    \xdef\gr@disc{#3}%
    \xdef\gr@depSeq{#2}%
    \ifcsdef{gr@class:#3.dep#2}{\@ex\@ex\@ex\forcsvlist\@ex\@ex\@ex#1\@ex\@ex\@ex{\csname gr@class:#3.dep#2\endcsname}}{}
}

\NewDocumentCommand\LstClass{O{\gooble}m}{%
\xdef\gr@sem{#2}%
\gr@setcred%
\@ex\@ex\@ex\forcsvlist\@ex\@ex\@ex\gr@addclasscred\@ex\@ex\@ex{\csname gr@et:#2\endcsname}%
\@ex\@ex\@ex\forcsvlist\@ex\@ex\@ex#1\@ex\@ex\@ex{\csname gr@et:#2\endcsname}%
}

\NewDocumentCommand\LstTopic{O{\goo}m}{%
\xdef\gr@topic{#2}%
\@ex\@ex\@ex\forcsvlist\@ex\@ex\@ex#1\@ex\@ex\@ex{\csname gr@topic:#2\endcsname}%
}

