% --------------------------------------------------------------------------
%
% the CHEMMACROS package -- module: `isotopes'
%
% --------------------------------------------------------------------------
% Clemens Niederberger
% --------------------------------------------------------------------------
% https://github.org/cgnieder/chemmacros/
% contact@mychemistry.eu
% --------------------------------------------------------------------------
% If you have any ideas, questions, suggestions or bugs to report, please
% feel free to contact me.
% --------------------------------------------------------------------------
% Copyright 2011-2016 Clemens Niederberger
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Clemens Niederberger.
% --------------------------------------------------------------------------
\ChemModule{isotopes}{2015/07/22 a macro to easily print element isotopes}

\RequirePackage{elements}

\msg_new:nnn {chemmacros} {no-isotope}
  { The~ isotope~ #2~ is~ not~ defined~ for~ element~ #1. }

% --------------------------------------------------------------------------
\tl_new:N  \l__chemmacros_isotope_tl
\tl_new:N  \l__chemmacros_isotope_list_tl
\seq_new:N \l__chemmacros_isotope_seq
\seq_new:N \l__chemmacros_check_isotopes_seq

% #1: atomic number|element symbol|element name
% #2: tokenlist variable
\cs_new_protected:Npn \chemmacros_get_atom_number:nN #1#2
  {
    \chemmacros_if_is_int:nTF {#1}
      { \tl_set:Nn #2 {#1} }
      {
        \cs_if_exist:cTF { @elements@atom@number@ \str_lower_case:n {#1} }
          { \tl_set:Nx #2 { \use:c { @elements@atom@number@ \str_lower_case:n {#1} } } }
          { \tl_set:Nx #2 { \use:c { @elements@atom@name@num@ \str_lower_case:n {#1} } } }
      }
  }
\cs_generate_variant:Nn \chemmacros_get_atom_number:nN { V }

% #1: atomic number|element symbol|element name
% #2: tl to store the result in
\cs_new_protected:Npn \chemmacros_get_isotope_list:nN #1#2
  {
    \chemmacros_get_atom_number:nN {#1} \l__chemmacros_isotope_tl
    \tl_set:Nx #2
      {
        \use:c
          {
            @elements@atom@isotopes@
            \int_to_roman:n {\l__chemmacros_isotope_tl}
          }
      }
  }

% #1: atomic number|element symbol|element name
% #2: tokenlist variable
\cs_new_protected:Npn \chemmacros_get_element_symbol:nN #1#2
  {
    \chemmacros_get_atom_number:nN {#1} \l__chemmacros_isotope_tl
    \tl_set:Nx #2
      {
        \use:c
          {
            @elements@atom@symbol@num@
            \int_to_roman:n {\l__chemmacros_isotope_tl}
          }
      }
  }

% #1: atomic number|element symbol|element name
% #2: isotope number
\prg_new_protected_conditional:Npnn \chemmacros_check_isotope:nn #1#2 {T,F,TF}
  {
    \chemmacros_get_isotopes:nN {#1} \l__chemmacros_check_isotopes_seq
    \seq_if_in:NnTF \l__chemmacros_check_isotopes_seq {#2}
      { \prg_return_true: }
      { \prg_return_false: }
  }
\cs_generate_variant:Nn \chemmacros_check_isotope:nnF { V }

% #1: atomic number|element symbol|element name
% #2: tl to store the result in
\cs_new_protected:Npn \chemmacros_get_main_isotope:nN #1#2
  {
    \chemmacros_get_isotope_list:nN {#1} \l__chemmacros_isotope_list_tl
    \seq_set_split:NnV \l__chemmacros_isotope_seq {,} \l__chemmacros_isotope_list_tl
    \seq_map_inline:Nn \l__chemmacros_isotope_seq
      {
        \chemmacros_read_isotope:nNN {##1}
          \l__chemmacros_tmpa_bool
          #2
        \bool_if:NT \l__chemmacros_tmpa_bool
          { \seq_map_break: }
      }
  }
\cs_generate_variant:Nn \chemmacros_get_main_isotope:nN { V }

% #1: atomic number|element symbol|element name
% #2: seq to store the results in
\cs_new_protected:Npn \chemmacros_get_isotopes:nN #1#2
  {
    \seq_clear:N #2
    \chemmacros_get_isotope_list:nN {#1} \l__chemmacros_tmpa_tl
    \seq_set_split:NnV \l__chemmacros_isotope_seq {,} \l__chemmacros_tmpa_tl
    \seq_map_inline:Nn \l__chemmacros_isotope_seq
      {
        \chemmacros_read_isotope:nNN {##1}
          \l__chemmacros_tmpa_bool
          \l__chemmacros_tmpa_tl
        \seq_put_right:NV #2 \l__chemmacros_tmpa_tl
      }
  }

% #1: boolean variable
% #2: tokenlist variable
% #3: isotope list entry
\cs_new_protected:Npn \chemmacros_read_isotope:nNN #1#2#3
  { \__chemmacros_read_isotope:NNw #2#3 !#1!! \q_stop }

% \__chemmacros_read_isotope:NNw N N !!1!! \q_stop
% \__chemmacros_read_isotope:NNw N N !1!! \q_stop
\cs_new_protected:Npn \__chemmacros_read_isotope:NNw #1#2#3!#4!#5!#6 \q_stop
  {
    \tl_if_blank:nTF {#5}
      {
        \tl_set:Nn #2 {#4}
        \bool_set_false:N #1
      }
      {
        \tl_set:Nn #2 {#5}
        \bool_set_true:N #1
      }
  }

% --------------------------------------------------------------------------
% #1: boolean: hide atomic number?
% #2: nucleons, element symbol
\cs_new_protected:Npn \chemmacros_isotope:nn #1#2
  { \__chemmacros_isotope:nww {#1} #2,, \q_stop }

\cs_new_protected:Npn \__chemmacros_isotope:nww #1#2,#3,#4 \q_stop
  {
    \tl_if_blank:nTF {#3}
      {
        \chemmacros_get_element_symbol:nN {#2} \l__chemmacros_tmpa_tl
        \chemmacros_get_main_isotope:nN {#2} \l__chemmacros_tmpb_tl
        \chemmacros_get_atom_number:nN {#2} \l__chemmacros_tmpc_tl
        \bool_if:nTF {#1}
          {
            \chemmacros_isotope_write:VnV
              \l__chemmacros_tmpb_tl
              { }
              \l__chemmacros_tmpa_tl
          }
          {
            \chemmacros_isotope_write:VVV
              \l__chemmacros_tmpb_tl
              \l__chemmacros_tmpc_tl
              \l__chemmacros_tmpa_tl
          }
      }
      {
        \chemmacros_get_element_symbol:nN {#3} \l__chemmacros_tmpa_tl
        \chemmacros_get_atom_number:nN {#3} \l__chemmacros_tmpb_tl
        \chemmacros_check_isotope:VnF
          \l__chemmacros_tmpa_tl
          {#2}
          {
            \chemmacros_get_element_symbol:nN {#3} \l__chemmacros_tmpa_tl
            \msg_warning:nnVn {chemmacros} {no-isotope}
              \l__chemmacros_tmpa_tl
              {#2}
          }
        \chemmacros_get_element_symbol:nN {#3} \l__chemmacros_tmpa_tl
        \bool_if:nTF {#1}
          { \chemmacros_isotope_write:nnV {#2} { } \l__chemmacros_tmpa_tl }
          {
            \chemmacros_isotope_write:nVV {#2}
              \l__chemmacros_tmpb_tl
              \l__chemmacros_tmpa_tl
          }
      }
  }

\bool_new:N \l__chemmacros_isotope_super_bool
\tl_new:N \l__chemmacros_isotope_side_connect_tl

\chemmacros_define_keys:nn {isotopes}
  {
    format .choice: ,
    format / super .code:n  =
      \bool_set_true:N \l__chemmacros_isotope_super_bool ,
    format / side .code:n   =
      \bool_set_false:N \l__chemmacros_isotope_super_bool ,
    format .initial:n       = super ,
    side-connect .tl_set:N  = \l__chemmacros_isotope_side_connect_tl ,
    side-connect .initial:n = -
  }

% #1: nucleons
% #2: atomic number
% #3: element symbol
\cs_new_protected:Npn \chemmacros_isotope_write:nnn #1#2#3
  {
    \bool_if:NTF \l__chemmacros_isotope_super_bool
      { \chemmacros_chemformula:n { ^{#1}_{#2}#3 } }
      {
        \chemmacros_chemformula:n {#3}
        \tl_use:N \l__chemmacros_isotope_side_connect_tl
        #1
      }
  }
\cs_generate_variant:Nn \chemmacros_isotope_write:nnn { VnV , VVV , nnV , nVV }

% TODO: option for the layout of the written isotope: ^{12}_6C vs C-12

\NewDocumentCommand \isotope { sm }
  { \chemmacros_isotope:nn {#1} {#2} }
  
% --------------------------------------------------------------------------
