% This file is public domain. See the "Examples" chapter
% in the bib2gls user manual for a more detailed description
% of this file.

\documentclass{scrreprt}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[version=4]{mhchem}
\usepackage{siunitx}
\usepackage{etoolbox}
\usepackage{marvosym}
% package conflict, need to undefine conflicting commands
\undef\Sun
\undef\Lightning
\usepackage[weather]{ifsym}

\usepackage[en-GB]{datetime2}
\usepackage[colorlinks]{hyperref}

\usepackage[record,% use bib2gls
 section,% use \section* for glossary headings
 postdot,% insert dot after descriptions in glossaries
 nomain,% don't create 'main' glossary
 index,% create 'index' glossary
 nostyles,% don't load default styles
% load and patch required style packages:
 stylemods={list,mcols,tree,bookindex}
]{glossaries-extra}

\newglossary*{bacteria}{Bacteria}
\newglossary*{markuplanguage}{Markup Languages}
\newglossary*{vegetable}{Vegetables}
\newglossary*{mineral}{Minerals}
\newglossary*{animal}{Animals}
\newglossary*{chemical}{Chemical Formula}
\newglossary*{baseunit}{SI Units}
\newglossary*{measurement}{Measurements}
\newglossary*{film}{Films}
\newglossary*{book}{Books}
\newglossary*{person}{People}
\newglossary*{mediacontrol}{Media Control Symbols}
\newglossary*{information}{Information Symbols}
\newglossary*{weather}{Weather Symbols}

\newcommand*{\bibglsdate}[7]{\DTMdisplaydate{#1}{#2}{#3}{#4}}

\GlsXtrLoadResources[
  src={no-interpret-preamble},
  interpret-preamble=false
]

\GlsXtrLoadResources[
 src={interpret-preamble,people},
 field-aliases={
   identifier=category,
   born=user1,
   died=user2,
   othername=user3
 },
 replicate-fields={name={first}},
 type=person,
 save-locations=false,
 date-fields={user1,user2},
 date-field-format={d MMM y G}
]

% Abbreviation styles must be set before the resource set
% that defines the abbreviations:
\setabbreviationstyle[bacteria]{long-only-short-only}
\setabbreviationstyle[markuplanguage]{long-short-desc}

% And also the style-dependent name format:
\renewcommand*{\glsxtrlongshortdescname}{%
  \protect\glsabbrvfont{\the\glsshorttok}\space
  \glsxtrparen{\glslongfont{\the\glslongtok}}%
}

\GlsXtrLoadResources[
 src={bacteria,markuplanguages,vegetables,
  minerals,animals,books,films},
 field-aliases={
   identifier=category,
   year=user1,
   cast=user2
 },
 type={same as category},
 bibtex-contributor-fields={user2},
 contributor-order={forenames},
 save-locations=false
]

\GlsXtrLoadResources[
 src={chemicalformula},
 entry-type-aliases={chemical=symbol},
 field-aliases={
   identifier=category,
   formula=name,
   chemicalname=description,
 },
 type={chemical},
 set-widest,
 sort={letternumber-case},
 symbol-sort-fallback={name},
 save-locations=false
]

\GlsXtrLoadResources[
 src={baseunits,derivedunits},
 entry-type-aliases={measurement=symbol,unit=symbol},
 field-aliases={
   unitname=description,
   unitsymbol=symbol,
   measurement=name
 },
 category={measurement},
 type={measurement},
 set-widest,
 symbol-sort-fallback={name},
 save-locations=false
]

\GlsXtrLoadResources[
 src={baseunits},
 entry-type-aliases={unit=symbol},
 field-aliases={
   unitname=description,
   unitsymbol=name
 },
 category={measurement},
 type={baseunit},
 duplicate-label-suffix={.copy},
 symbol-sort-fallback={name},
 save-locations=false
]

\GlsXtrLoadResources[
 src={miscsymbols},
 field-aliases={
   identifier=category,
   icon=name,
   icondescription=description
 },
 entry-type-aliases={icon=symbol},
 type={same as category},
 sort-field={description},
 save-locations=false,
 set-widest
]

\renewcommand*{\glsxtrlongshortdescname}{%
  \protect\protect\glslongfont{\the\glslongtok}\space
  \glsxtrparen{\glsabbrvfont{\the\glsshorttok}}%
}

% requires glossaries-extra v1.25:
\renewcommand*{\glsxtronlyname}{%
  \protect\glsabbrvonlyfont{\the\glslongtok}%
}

\GlsXtrLoadResources[
  src={miscsymbols},
  selection={recorded no deps},
  duplicate-label-suffix={.copy},
  entry-type-aliases={icon=index},
  field-aliases={
   identifier=category,
   icondescription=symbol,
   icon=name
  },
  type=index,
  sort-field={symbol},
  group={glssymbols}
]

\GlsXtrLoadResources[
 src={terms,bacteria,markuplanguages,vegetables,minerals,
  animals,chemicalformula,baseunits,derivedunits,people,
  films,books,miscsymbols},
 selection={recorded and deps and see},
 field-aliases={
   identifier=category,
   formula=symbol,
   chemicalname=name,
   unitname=description,
   unitsymbol=symbol,
   measurement=name,
   icon=symbol,
   icondescription=name
 },
 entry-type-aliases={
  chemical=index,
  measurement=entry,
  unit=dualentry,
  icon=index
 },
 label-prefix={idx.},
 record-label-prefix={idx.},
 type=index,
 dual-type=index,
 combine-dual-locations=both,
 abbreviation-sort-fallback={long},
 replicate-override,
 replicate-fields={first=name},
 identical-sort-action={category}
]

\newcommand*{\swaptwo}[2]{#2, #1}
\newcommand*{\swapthree}[3]{#2 #3, #1}

\newcommand{\bacteriafont}[1]{\emph{#1}}
\glssetcategoryattribute{bacteria}{textformat}{bacteriafont}
\glssetcategoryattribute{bacteria}{glossnamefont}{bacteriafont}
\glssetcategoryattribute{bacteria}{glossdescfont}{bacteriafont}

\newcommand{\bookfont}[1]{\emph{#1}}
\glssetcategoryattribute{book}{textformat}{bookfont}
\glssetcategoryattribute{book}{glossnamefont}{bookfont}

\newcommand{\filmfont}[1]{\emph{#1}}
\glssetcategoryattribute{film}{textformat}{filmfont}
\glssetcategoryattribute{film}{glossnamefont}{filmfont}
\glssetcategoryattribute{film}{glossdesc}{firstuc}

\glssetcategoryattribute{markuplanguage}{glossdesc}{firstuc}

\newcommand*{\glsxtrpostlinkmediacontrol}{%
 \glsxtrpostlinkAddDescOnFirstUse
}

\newcommand*{\glsxtrpostlinkinformation}{%
 \glsxtrpostlinkAddDescOnFirstUse
}

\newcommand*{\glsxtrpostlinkweather}{%
 \glsxtrpostlinkAddDescOnFirstUse
}

\newcommand*{\glsxtrpostlinkperson}{%
 \glsxtrifwasfirstuse
 {%
   \ifglshasfield{user3}{\glslabel}%
   {\space(\glscurrentfieldvalue)}%
   {}%
 }%
 {}%
}

\newcommand*{\glsxtrpostnameperson}{%
 \ifglshasfield{user3}{\glscurrententrylabel}%
 {\space(\glscurrentfieldvalue)}%
 {}%
}

\newcommand*{\glsxtrpostdescperson}{%
 \ifglshasfield{user1}{\glscurrententrylabel}
 {% born
   \space(\glscurrentfieldvalue\,--\,%
     \ifglshasfield{user2}{\glscurrententrylabel}
     {% died
        \glscurrentfieldvalue
     }%
     {}%
   )%
 }%
 {}%
}

\newcommand*{\glsxtrpostdescfilm}{%
 \ifglshasfield{user1}{\glscurrententrylabel}%
 {%
   \glsxtrrestorepostpunc % requires glossaries-extra v1.23+
   \ (released \glscurrentfieldvalue)}%
 {}%
 \ifglshasfield{user2}{\glscurrententrylabel}%
 {%
    \glsxtrrestorepostpunc
    \ featuring \glscurrentfieldvalue
 }%
 {}%
}

\renewcommand*{\glsxtrbookindexname}[1]{%
  \glossentryname{#1}%
  \ifglshassymbol{#1}%
  {%
    \glsifcategory{#1}{chemical}%
    {, \glossentrysymbol{#1}}%
    {\space(\glossentrynameother{#1}{symbol})}%
  }%
  {%
    \glsifcategory{#1}{film}%
    {\ (film)}%
    {}%
  }%
}

% requires glossaries-extra v1.25+:
\renewcommand*{\glsextrapostnamehook}[1]{%
  \glsadd[format=hyperbf]{#1}%
}

\newcommand{\Unit}{\glssymbol}
\newcommand{\measurement}{\gls}
\glsxtrnewgls{film.}{\film}
\glsxtrnewglslike{idx.}{\idx}{\idxpl}{\Idx}{\Idxpl}

\begin{document}
\chapter{Sample}
\section{Bacteria}
This section is about \idxpl{bacteria}.
\subsection{First Use}
\gls{cbotulinum}, \gls{pputida}, \gls{cperfringens},
\gls{bsubtilis}, \gls{ctetani}, \gls{pcomposti},
\gls{pfimeticola}, \gls{cburnetii}, \gls{raustralis},
\gls{rrickettsii}.

\subsection{Next Use}
\gls{cbotulinum}, \gls{pputida}, \gls{cperfringens},
\gls{bsubtilis}, \gls{ctetani}, \gls{pcomposti},
\gls{pfimeticola}, \gls{cburnetii}, \gls{raustralis},
\gls{rrickettsii}.

\section{Markup Languages}
This section is about \idxpl{markuplanguage}.
\subsection{First Use}
\gls{LaTeX}, \gls{markdown}, \gls{xhtml}, \gls{mathml}, \gls{svg}.

\subsection{Next Use}
\gls{LaTeX}, \gls{markdown}, \gls{xhtml}, \gls{mathml}, \gls{svg}.

\section{Vegetables}
This section is about \idxpl{vegetable}.
\Gls{cabbage}, \gls{brussels-sprout}, \gls{artichoke}, 
\gls{cauliflower}, \gls{courgette}, \gls{spinach}.

\section{Minerals}
This section is about \idxpl{mineral}.
\Gls{beryl}, \gls{amethyst}, \gls{chalcedony}, \gls{aquamarine},
\gls{aragonite}, \gls{calcite}, \gls{bilinite}, 
\gls{cyanotrichite}, \gls{biotite}, \gls{dolomite}, 
\gls{quetzalcoatlite}, \gls{vulcanite}.

\section{Animals}
This section is about \idxpl{animal}.
\Gls{duck}, \gls{parrot}, \gls{hedgehog}, \gls{sealion}, 
\gls{zander}, \gls{aardvark}, \gls{zebra}, \gls{swan},
\gls{armadillo}.

\section{Chemicals}
This section is about \idxpl{chemical}.
\gls{Al2SO43} (\glsdesc{Al2SO43}), \gls{H2O} (\glsdesc{H2O}), 
\gls{C6H12O6} (\glsdesc{C6H12O6}), \gls{CH3CH2OH} 
(\glsdesc{CH3CH2OH}), \gls{CH2O} (\glsdesc{CH2O}), \gls{OF2}
(\glsdesc{OF2}), \gls{O2F2} (\glsdesc{O2F2}), \gls{SO42-}
(\glsdesc{SO42-}), \gls{H3O+} (\glsdesc{H3O+}), \gls{OH-}
(\glsdesc{OH-}), \gls{O2} (\glsdesc{O2}), \gls{AlF3} 
(\glsdesc{AlF3}), \gls{O} (\glsdesc{O}), \gls{Al2CoO4}
(\glsdesc{Al2CoO4}), \gls{As4S4} (\glsdesc{As4S4}), 
\gls{C10H10O4} (\glsdesc{C10H10O4}), \gls{C5H4NCOOH} 
(\glsdesc{C5H4NCOOH}), \gls{C8H10N4O2} (\gls{C8H10N4O2}), 
\gls{SO2} (\glsdesc{SO2}), \gls{S2O72-} (\gls{S2O72-}), 
\gls{SbBr3} (\glsdesc{SbBr3}), \gls{Sc2O3} (\glsdesc{Sc2O3}), 
\gls{Zr3PO44} (\glsdesc{Zr3PO44}), \gls{ZnF2} (\glsdesc{ZnF2}).

\section{SI Units}
\Idxpl{baseunit}: \Unit{ampere} (measures \measurement{ampere}), 
\Unit{kilogram} (measures \measurement{kilogram}), \Unit{metre}, 
\Unit{second}, \Unit{kelvin}, \Unit{mole}, \Unit{candela}.

\Idxpl{derivedunit}: \Unit{area}, \Unit{volume},
\Unit{velocity},
\Unit{acceleration}, \Unit{density}, \Unit{luminance},
\Unit{specificvolume}, \Unit{concentration}, \Unit{wavenumber}.

\section{Books and Films}
\Idxpl{book}: \gls{ataleoftwocities} (by \gls{dickens}),
\gls{thebigsleep} (by \gls{chandler}, \idx{film} adaptation:
\film{thebigsleep}), \gls{icecoldinalex} (by
\gls{landon}, \idx{film} adaptation: \film{icecoldinalex}),
\gls{whydidnttheyaskevans} (by \gls{christie}, 
\idx{film} adaptation: \film{whydidnttheyaskevans}),
\gls{doandroidsdreamofelectricsheep} (by \gls{dick},
inspired the \idx{film} \film{bladerunner}).

\Idxpl{film}: \film{anunexpectedjourney}, \film{desolationofsmaug}
and \film{thebattleoffivearmies} (adapted from the
\idx{book} \gls{thehobbit} by \gls{tolkien}), 
\film{thefellowshipofthering}, \film{thetwotowers}
and \film{thereturnoftheking} (adapted from the
\idx{book} \gls{thelordoftherings} also by \gls{tolkien}).

\section{Miscellaneous Symbols}

\subsection{First Use}

\Idxpl{mediacontrol}: \gls{forward}, \gls{forwardtoindex},
\gls{rewindtoindex}, \gls{rewind}.

\Idx{information}: \gls{bicycle}, \gls{coffeecup}, \gls{info},
\gls{gentsroom}, \gls{ladiesroom}, \gls{wheelchair}, \gls{football},
\gls{recycling}.

\Idx{weather}: \gls{cloud}, \gls{fog}, \gls{hail}, \gls{sun},
\gls{lightning}.

\subsection{Next Use}

\Idxpl{mediacontrol}: \gls{forward}, \gls{forwardtoindex},
\gls{rewindtoindex}, \gls{rewind}.

\Idx{information}: \gls{bicycle}, \gls{coffeecup}, \gls{info},
\gls{gentsroom}, \gls{ladiesroom}, \gls{wheelchair}, \gls{football}.

\Idx{weather}: \gls{cloud}, \gls{fog}, \gls{hail}, \gls{sun},
\gls{lightning}.

\section{Measurements}

\Idxpl{measurement}:
\measurement{ampere}, \measurement{area}, \measurement{metre}.

\chapter{Glossaries}
\printunsrtglossary[type=bacteria,style=mcoltree]
\printunsrtglossary[type=markuplanguage,style=altlist]
\printunsrtglossary[type=vegetable,style=tree,nogroupskip]
\printunsrtglossary[type=mineral,style=treegroup]
\printunsrtglossary[type=animal,style=tree]
\printunsrtglossary[type=person,style=tree,nogroupskip]
\printunsrtglossary[type=book,style=tree,nogroupskip]
\printunsrtglossary[type=film,style=tree,nogroupskip]
\printunsrtglossary*[type=chemical,style=mcolalttreegroup]
{%
  \renewcommand*{\glstreenamefmt}[1]{#1}%
  \renewcommand*{\glstreegroupheaderfmt}[1]{\textbf{#1}}%
}
\printunsrtglossary*[type=measurement,style=alttree,nogroupskip]
{%
  \renewcommand{\glsxtralttreeSymbolDescLocation}[2]{%
    \glossentrydesc{#1}%
    \ifglshassymbol{#1}{\space(\glossentrysymbol{#1})}{}%
    \glspostdescription
    \glsxtrAltTreePar
  }%
}

\printunsrtglossary*[type=baseunit,style=alttree,nogroupskip]
{%
  \glsFindWidestTopLevelName[baseunit]%
}
\printunsrtglossary[type=information,style=alttree,nogroupskip]
\printunsrtglossary[type=mediacontrol,style=alttree,nogroupskip]
\printunsrtglossary[type=weather,style=alttree,nogroupskip]

\printunsrtglossary*[type=index,style=bookindex]
{%
  \setupglossaries{section=chapter}%
  \let\sortname\swaptwo
  \let\sortvonname\swapthree
  \renewcommand*{\glsextrapostnamehook}[1]{}%
}
\end{document}}
