'''
Created on 12-Jul-2021

@author: bthakur
'''
import random
import inspect
import traceback   #        {inspect.stack()[0][3]}

import re
from sympy import (Eq, Symbol, symbols, sqrt, latex,
                   Rational, solve, log, nsimplify, E, exp,  
                   sin, cos, I, series, pi)
from sympy.abc import theta
import sympy

from ....util import LatexEquation

from xv.km.util import latex_fraction
from sympy.core.expr import UnevaluatedExpr as ueval

from xv.util import UtilClass, list_to_str

class ModellingSingleVariableHelper(UtilClass):
    def __init__(self, *args, **kwargs):
        super().__init__(**kwargs)
        pass

    def get_random_model(self, *args, **kwargs):
        methods = [self.model_price_dicount_tax,
                   ]
        
        method = random.choice(methods)
        random_model = method()
    
        var_names = random_model['var_names']
        var_vals = random_model['var_vals']
        
        
        solutions = random_model['solutions']
        
        var_names_str = [f"${latex(k)}: {latex(v)}$" for k,v in var_names.items()]
        var_names_str = "\n".join(var_names_str)
        
        solutions_str = [f"${latex(k)}: {latex(v)}$" for k,v in solutions.items()]
        solutions_str = "\n".join(solutions_str)
        
        prob_s = f"""
        {random_model['statement_header']}
        {random_model['statement']}
        {random_model['statement_footer']}
        """
                
        soln_s = f"""
        <b>Let:</b>
        {var_names_str}
        
        <b>Equations:</b>
        {random_model['eqs_str']}
        
        <b>Solving the equations:</b>
        {solutions_str}
        """
        
        answer_s = f"""
        {solutions_str}
        """
        
        result = {
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        
        return result
        pass

    def model_price_dicount_tax(self):
        '''
        model_price_dicount_tax
        '''
        
        x  = Symbol('x')
        denm = random.randint(2, 20)
        x_frac = Rational(random.randint(1, 9), denm) * random.choice([-1, 1])
        x_val = random.randint(1, 20) * random.choice([1, denm])
        
        eq_ueval = Eq(ueval(x) + x_frac * x , (1 + x_frac) * x_val )
        eq = Eq((1 + x_frac) * x , (1 + x_frac) * x_val )
        
        if x_frac < 0:
            statement = f"""
            If $\\displaystyle {latex(abs(x_frac))}
            $ times of number is subtracted from itself, it is equal to $\\displaystyle 
            {latex(eq.rhs)}
            $. Write the equation and solve it.   
            """
        else:
            statement = f"""
            If $\\displaystyle {latex(x_frac)}
            $ times of number is added to itself, it is equal to $\\displaystyle 
            {latex(eq.rhs)}
            $. Write the equation and solve it.   
            """
            
        
        var_names = {x: 'number',
                     }
        vals_dict = {x: x_val,
                    }
        
        var_vals = {}
        solutions = vals_dict
        
        statement_header = ''
        
        statement_footer = ""


        latex_eqs = [latex(eq_ueval)]
        eqs_str = self._get_eqs_str(latex_eqs)
        

        random_model = {
            'statement_header': statement_header,
            'statement': statement,
            'statement_footer': statement_footer,
            'var_names': var_names,
            'latex_eqs': latex_eqs,
            'eqs_str': eqs_str,
            'var_names': var_names,
            'var_vals': var_vals,
            'solutions': solutions,
            }
        
        return random_model
        pass
    
    
    
    
    def _get_eqs_str(self, latex_eqs):
        eqs_str = [f'$\\displaystyle {latex_eq}$' for latex_eq in latex_eqs]
        eqs_str = "\n\n".join(eqs_str)
        return eqs_str
        pass
    
    def _get_filtered_dict(self, dct, keys):
        return {k:dct[k] for k in keys}
        pass
    
    
    pass
#class ModellingSingleVariableHelper:
