% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{statementsp}[2026/03/28 v1.0 A package for theorem and proposition boxes]

%Packages for statementsp
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{ifthen} %Using \ifthenelse
\usepackage{etoolbox} %Using \ifstrequal etc.
\usepackage{amsthm} %Proof environment
\usepackage{amssymb} %Mathematical symbols
\usepackage[many]{tcolorbox} %Insert tcolorbox package and its option


%Refference
% \mathtoolsset{showonlyrefs=true} %Give numbers of equation in equation and align environment only to refered them.
\usepackage[
    setpagesize=false,
    bookmarks=true,
    bookmarksdepth=tocdepth,
    bookmarksnumbered=true,
    colorlinks=true,
    linkcolor=blue,
    pdftitle={},
    pdfsubject={},
    pdfauthor={},
    pdfkeywords={}
]{hyperref} %Insert hyperlink into a document
\usepackage{prettyref}
\newcommand{\refsp}[1]{\hyperref[#1]{\prettyref{#1}}}
\newcommand{\refnamesp}[1]{
  \ifcsname usevalue@name#1\endcsname 
    \hyperref[#1]{\prettyref{#1}~:\usevalue{name#1}}
  \else
    \hyperref[#1]{\prettyref{#1}~:\usevalue{name#1}}
    \PackageWarning{statementsp}{No name of statement #1}{}
  \fi
}


%Define counter
\newcounter{statement@num}[section]
\renewcommand{\thestatement@num}{\arabic{section}.\arabic{statement@num}}
\newcounter{all@num} %Recall and advance notice counter
\setcounter{all@num}{0}


%Foundamental design of tcolorbox.
\tcbset{
    statement/.style 2 args={
            enhanced,
            coltitle=#1,
            colframe=#2,
            boxrule=2pt,
            colback=white,
            breakable,
            sharp corners,
            attach boxed title to top left={xshift=3mm, yshift*=-\tcboxedtitleheight/2},
            boxed title style={
                    colframe=#2,
                    boxrule=2pt,
                    colback=#2,
                    sharp corners
                },
            after upper=\hfill $\square$
        }
}


%For previewing and recalling box
%moving
\newcommand{\usevalue}[1]{%
  \ifcsname usevalue@#1\endcsname
    \csname usevalue@#1\endcsname
  \else
    ??%
  \fi
}
\newcommand{\definevalue}[2]{%
  \immediate\write\@auxout{%
    \unexpanded{\global\@namedef{usevalue@#1}{#2}}%
  }
}

\newcommand{\allusevalue}[1]{
    \ifcsname allusevalue@#1\endcsname
        \csname allusevalue@#1\endcsname
    \else
        0
    \fi
}

\newcommand{\statementnameusevalue}[1]{
    \ifcsname statementname@#1\endcsname
        \csname statementname@#1\endcsname
    \else
        ??
    \fi
}


%Giving a command of making a box
\NewDocumentCommand{\newstatementsp}{m m m m}{
    %#1=statement name, #2=statement display name, #3=box color, #4=title color
    \makeatletter
    \definecolor{#1tcol}{HTML}{#3}
    \definecolor{#1bcol}{HTML}{#4}
    \newrefformat{#1}{#2~\ref*{##1}}
    \write\@auxout{\unexpanded{\global\@namedef{statementname@#1}{#2}}}
    \makeatother
}

%statementsp environment
\NewDocumentEnvironment{statementsp}{r<> d[] d() +!b}{
    \ifstrequal{#2}{}{
        \stepcounter{statement@num}
        \stepcounter{all@num}
    }{
        \stepcounter{all@num}
        \immediate\write\@auxout{\unexpanded{\global\@namedef{allusevalue@all#1:#2}}{\theall@num}}
        \refstepcounter{statement@num}\label{#1:#2}
        
        \ifstrequal{#3}{}{
            \definevalue{pre#1:#2}{
                \begin{tcolorbox}[statement={#1tcol}{#1bcol}, title={\underline{\hyperref[#1:#2]{\statementnameusevalue{#1}~\ref*{#1:#2}}}~ (Preview)}]
                    #4
                \end{tcolorbox}
            }
            \definevalue{re#1:#2}{
                \begin{tcolorbox}[statement={#1tcol}{#1bcol}, title={\underline{\hyperref[#1:#2]{\statementnameusevalue{#1}~\ref*{#1:#2}}}~ (Recall)}]
                    #4
                \end{tcolorbox}
            }
        }{
            \definevalue{name#1:#2}{(#3)}
            \definevalue{pre#1:#2}{
                \begin{tcolorbox}[statement={#1tcol}{#1bcol}, title={\underline{\hyperref[#1:#2]{\statementnameusevalue{#1}~\ref*{#1:#2}}}:~(#3)~ (Preview)}]
                    #4
                \end{tcolorbox}
            }
            \definevalue{re#1:#2}{
                \begin{tcolorbox}[statement={#1tcol}{#1bcol}, title={\underline{\hyperref[#1:#2]{\statementnameusevalue{#1}~\ref*{#1:#2}}}:~(#3)~ (Recall)}]
                    #4
                \end{tcolorbox}
            }
        }
    }
    \ifstrequal{#3}{}{
        \tcbset{
            mybox/.style={title={\underline{\hspace{-1.5mm}\statementnameusevalue{#1}~\thestatement@num}}}
        }
    }{
        \tcbset{
            mybox/.style={title={\underline{\hspace{-1.5mm}\statementnameusevalue{#1}~\thestatement@num}:~(#3)}}
        }
    }
    \begin{tcolorbox}[statement={#1tcol}{#1bcol}, mybox]
        #4
    \end{tcolorbox}
}{}


%statementsp* environment
\NewDocumentEnvironment{statementsp*}{r<> d[] d() +!b}{
    \ifstrequal{#2}{}{
        \stepcounter{all@num}
    }{
        \label{#1:#2}
        \stepcounter{all@num}
        \immediate\write\@auxout{\unexpanded{\global\@namedef{allusevalue@all#1:#2}}{\theall@num}}
        \ifstrequal{#3}{}{
            \definevalue{pre#1:#2}{
                \begin{tcolorbox}[statement={#1tcol}{#1bcol}, title={\underline{\hspace{-1.5mm}\statementnameusevalue{#1}}~ (Preview)}]
                    #4
                \end{tcolorbox}
            }
            \definevalue{re#1:#2}{
                \begin{tcolorbox}[statement={#1tcol}{#1bcol}, title={\underline{\hspace{-1.5mm}\statementnameusevalue{#1}}~ (Recall)}]
                    #4
                \end{tcolorbox}
            }
        }{
            \definevalue{name#1:#2}{(#3)}
            \definevalue{pre#1:#2}{
                \begin{tcolorbox}[statement={#1tcol}{#1bcol}, title={\underline{\hspace{-1.5mm}\statementnameusevalue{#1}}~(#3)~ (Preview)}]
                    #4
                \end{tcolorbox}
            }
            \definevalue{re#1:#2}{
                \begin{tcolorbox}[statement={#1tcol}{#1bcol}, title={\underline{\hspace{-1.5mm}\statementnameusevalue{#1}}~(#3)~ (Recall)}]
                    #4
                \end{tcolorbox}
            }
        }
    }
    \ifstrequal{#3}{}{
        \tcbset{
            mybox/.style={title={\underline{\hspace{-1.5mm}\statementnameusevalue{#1}}}}
        }
    }{
        \tcbset{
            mybox/.style={title={\underline{\hspace{-1.5mm}\statementnameusevalue{#1}}~(#3)}}
        }
    }
    \begin{tcolorbox}[statement={#1tcol}{#1bcol}, mybox]
        #4
    \end{tcolorbox}
}{}

%Preview and Recall command
\NewDocumentCommand{\refcallsp}{m}{
    \ifnumcomp{\value{all@num}}{<}{\allusevalue{all#1}}{
        \usevalue{pre#1}
    }{
        \usevalue{re#1}
    }
}


% %Proof environment
% \renewcommand{\proofname}{\textit{pf}.)} %Change first style of proof
% \renewcommand{\qedsymbol}{$\blacksquare$} %Change end style of proof
% \makeatletter
% \renewenvironment{proof}[1][\proofname]{\par
%     \pushQED{\qed}%
%     \normalfont \topsep6\p@\@plus6\p@\relax
%     \trivlist
%     \item\relax
%     {
%         #1\@addpunct{}}\hspace\labelsep\ignorespaces
% }{%
%     \popQED\endtrivlist\@endpefalse
% }
% \makeatother

% \newenvironment{pfsp}{\begin{proof}}{\end{proof} \vspace{5mm}}