'''
Created on 17-Jun-2021

@author: bthakur
'''
from xv.util import listAttr, locatePath
import pathlib
import csv
from latex2sympy2 import latex2sympy, latex2latex
from sympy import sympify, Eq, latex
from xv.util.local import FileSystemManager
import random

from ._formula_helper import FormulaHelper

from xv.util import UtilClass

class FormulaSet(UtilClass):
    MAX_TRIES = 10
    
    def __init__(self, count = 4,  category = None, formula_helper = None, verbose = False):
        super().__init__(verbose = verbose)
        self.count = count
        self.fh = formula_helper
        if self.fh is None:
            self.fh = FormulaHelper()
        self.formulae = None
        self.category = None
        
        self._init_set(category = category)
        pass
    
    def _init_set(self, category = None):
        if not category is None:
            formulae_found = self.fh.search_formulae(category, has_equation = True)
            pass
        else:
            categories = self.fh.get_categories()
            
            max_tries = FormulaSet.MAX_TRIES
            formulae_found = None
            while max_tries > 0: 
                max_tries = max_tries - 1
                category = random.choice(categories)
                formulae_found = self.fh.search_formulae(category, has_equation = True)
                if len(formulae_found) > 1:
                    break
                pass
            
            if len(formulae_found) < 1:
                raise Exception("No category with formula found")
            pass
        
        
        formulae_found = random.sample(formulae_found, min(self.count, 
                                                           len(formulae_found)))
        self.category = category
        self.formulae = formulae_found
        pass
    
    def get_latex_set(self, 
                      formulae = None,
                      expr_type = 'rhs', 
                      include_index = False,
                      include_description = False,
                      include_links = False,
                      shuffle = False,
                      line_separator = "\n\n"
                      ):
        '''
        expr_type = 'lhs' or 'rhs', 'eq', 'latex_expr'
            'latex_expr' is default value
        '''
        if formulae is None:
            formulae = self.formulae.copy()
            
        if shuffle:
            random.shuffle(formulae)
            
        latex_set = []
        for formula in formulae:
            expr = formula.get_latex_expr(expr_type)
            if expr_type == 'lhs':
                expr_s = f"{formula.eq.lhs}"
            if expr_type == 'rhs':
                expr_s = f"{formula.eq.rhs}"
            else:
                expr_s = f"{formula.eq.lhs} = {formula.eq.rhs}"
            
            if include_description:
                if len(expr_s) < 10:
                    formula_latex = f"{formula.description}, ${expr}$"
                else:
                    formula_latex = f"{formula.description}{line_separator}\t${expr}$"
            else:
                formula_latex = f"${expr}$"
                pass

            if include_links:
                formula_latex = f"""{formula_latex}
                
                {formula.get_links_html()}
                """
            
            latex_set.append(formula_latex.strip())
            pass

                
            
        #latex_set = list(set(latex_set))
        
        if include_index:
            latex_set = [ f"{idx + 1}. " + expr
                for idx, expr in enumerate(latex_set)
                ]
        
        
        return latex_set
        pass
    
    pass
#class _FormulaSet:

