'''
Created on 01-Apr-2021
@author: bthakur
'''
import os 
#import decimal
from functools import reduce
from xv.km.ui import SvgHelper 

import numpy as np
import random
import inspect
import traceback
from sympy import (simplify, sympify, log, Symbol, symbols, Eq, Matrix, latex, 
                   solve, Rational, diag, pi, nsimplify, Poly)

from sympy.abc import theta


#from xv.math.formulae import FormulaHelper, FormulaSet
from xv.math.formulae import FormulaHelper, FormulaSet


from xv.km.util import latex_fraction
        
from xv.util import toIntOrFloat, list_to_str

from sympy.core.expr import UnevaluatedExpr

from xv.km.util import latex_fraction

from ..helper import BaseHelper
from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate

# _polygon_manager
# PolygonManager
        
class PolygonManager(BaseManager):
    '''
    PolygonManager
    Example:
        ke = PolygonManager()
        ke.getRandomProblem()
        ke.getRandomProblem(problem_type = 0)
        ...
        
        ke.printProblem()
        ke.printAnswer()
        ke.printSolution()
        
        ke.printProblemTypes()
        
    '''    
    
    def __new__(cls, *args, **kwargs):
        instance = super(PolygonManager, cls).__new__(cls, *args, **kwargs)
        for base_class in PolygonManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.base_helper = BaseHelper(**kwargs)
        self.fh = FormulaHelper()
        pass

    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
            '_problem_area_triangle',
            ])
        pass

    def _problem_area_triangle(self, *args, **kwargs):
        
        n1, n2  = random.sample(list(range(1, 10)), 2)
        theta = random.choice([pi/3, pi/4, pi/6])
        
        polygon_helper = self.base_helper.get_geometry_helper().get_polygon_helper()
        polygon = polygon_helper.get_random_triangle(n1, n2, theta)
        
        self.polygon = polygon
        
        svg_str = polygon_helper.plot_polygon(polygon, use_cache = False)
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = f"""
        Triangle {polygon}.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        The area of a triangle is half of the parallelogram with same base and height. Therefore, 
        
        Area of triangle = ${latex_fraction(1, 2)}$ * base * height $= {latex_fraction(1, 2)} * {n1} * {n2} = {toIntOrFloat(n1 * n2 / 2)}$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        problemTemplate.add_solution(svg_str, 'text/svg')
        
        
        answer_s = f"""
        ${toIntOrFloat(n1 * n2 / 2)}$
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    
    def _sample(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = f"""
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
        