'''
Created on 19.05.2022

@author: wf
'''
import datetime
class Figure():
    '''
    a figure 
    '''
    def __init__(self,title,caption,figLabel,sqlQuery,fileNames,px=600,scale=None):
        '''
        constructore
        '''
        self.title=title
        self.caption=caption
        self.sqlQuery=sqlQuery
        self.figLabel=figLabel
        self.fileNames=fileNames
        self.px=px
        self.scale=scale
        self.scalemarkup=f"[scale={scale}]" if scale else ""
        
    def asWikiMarkup(self):
        '''
        create mediawiki markup to show the given outputFilename and sqlQuery
        '''
        markup=f"""== {self.title} =="""
        if self.sqlQuery:
            markup+=f"""
=== sql query ===
<source lang='sql'>
{self.sqlQuery}
</source>"""
        markup+=f"""
=== {self.caption} ===
"""
        for fileName in self.fileNames:
            markup+=f"""[[File:{fileName}|{self.px}px]]"""
        return markup 
    
    def asLatex(self,index,cols):
        '''
        get the latex Code for the given figure
        
        Args:
            index(int): the index of this figure
            cols(int): total number of cols
        '''
        latex=""
        if cols==1 or index==0:
            latex="\\begin{figure}"
            latex+="\n"
        fileCount=len(self.fileNames)
        for fileName in self.fileNames:
            latex+=f"""    %figure {self.figLabel}:{self.caption}
    {{\\includegraphics{self.scalemarkup}[width=\\linewidth/{fileCount}/{cols}]{{{self.latexFolder}{fileName}}}}}%
    \hfill%"""
        if cols==1:
            latex+=Figure.footer(self.caption,self.figLabel)
        else:
            if (index+1)%cols==0:
                latex+="\n    \\\\[\smallskipamount]"
        return latex 
    
    @classmethod
    def footer(cls,caption,figLabel):
        latex="""
    \caption{%s}
    \label{fig:%s}
\end{figure}""" % (caption,figLabel)
        return latex
    
class FigureList():
    '''
    Figure List
    '''
    maxFigureListId=0
    
    def __init__(self,caption,latexFolder="results/",figureListLabel=None,cols=1):
        '''
        constructor
        '''
        self.caption=caption
        self.clear()
        self.latexFolder=latexFolder
        self.isodate=datetime.datetime.now().isoformat()
        if figureListLabel is None:
            FigureList.maxFigureListId+=1
            figureListLabel=f"figureList{FigureList.maxFigureListId}"
        self.figureListLabel=figureListLabel
        self.cols=cols
    
    def clear(self):
        '''
        clear my figures list
        '''
        self.figures=[]      
        
    def add(self,figure:Figure):
        '''
        add the given figure
        
        figure(Figure): the figure to add
        '''
        figure.latexFolder=self.latexFolder
        self.figures.append(figure)
        
    def latexComment(self,preamble:bool=True):
        '''
        create a latex comment
        
        Args:
            preamble(bool): if True generate a preamble comment otherwise a footer comment
        '''
        if preamble:
            text=f"""% >>>figureList {self.figureListLabel} generated by {__file__} on {self.isodate}"""
        else:
            text=f"%<<<figureList {self.figureListLabel}: end of generated latex code"
        return text
    
    def printAllMarkups(self):
        '''
        print all Markus
        '''
        for figure in self.figures:
            print(figure.asWikiMarkup())
        # wrap 
        print(self.latexComment(True))
        for index,figure in enumerate(self.figures):
            print(figure.asLatex(index,self.cols))
        if self.cols>=1:
            print(Figure.footer(self.caption, self.figureListLabel))
        print(self.latexComment(False))