'''
Created on 01-Apr-2021
@author: bthakur
'''
from functools import reduce

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

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

        


from ..helper import BaseHelper
from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate
from sympy.core.expr import UnevaluatedExpr
        
# _geometrical_shape_manager
# GeometricalShapeManager
        
class GeometricalShapeManager(BaseManager):
    '''
    GeometricalShapeManager
    Example:
        ke = GeometricalShapeManager()
        ke.getRandomProblem()
        ke.getRandomProblem(problem_type = 0)
        ke.getRandomProblem(problem_type = 1)
        ...
        ke.printProblem()
        ke.printAnswer()
        ke.printSolution()
    '''    
    
    def __new__(cls, *args, **kwargs):
        instance = super(GeometricalShapeManager, cls).__new__(cls, *args, **kwargs)
        for base_class in GeometricalShapeManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.base_helper = BaseHelper(**kwargs)
        self._equation = None
        pass
    
    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
            '_problem_plot_regular_polygon',
            ]) 
        pass

    def _problem_plot_regular_polygon(self, *args, **kwargs):
        """
        
        doc_style: xv_doc
        """

        gs_helper = self.base_helper.get_geometry_helper().get_geometrical_shape_helper()


        side_counts = list(range(3, 11))
        side_counts_filtered = random.sample(side_counts, 3)

        
        result = gs_helper.plot_regular_polygon(side_counts_filtered, 
                                                use_cache = True)
        result_labelled = gs_helper.plot_regular_polygon(side_counts_filtered, 
                                                         labelled = True,
                                                         label_side_count = True,
                                                         draw_radii = True,
                                                         use_cache = True)
        
        result_all = gs_helper.plot_regular_polygon(side_counts, 
                                                         labelled = True,
                                                         label_side_count = True,
                                                         draw_radii = True,
                                                         use_cache = True)
        
        polygon_names_html = gs_helper.get_regular_polygon_names_html()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        problem_s = f"""
        Name the polygons:
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        problemTemplate.add_problem(result['svg_str'], 'text/svg')
        
        
        soln_s = f"""
        <b>Polygon</b>
        1. is made of a set of line segments (sides).
        1. is a closed curve, so it must have  at least 3 sides.
        3. no two line segments cross each other. 
        
        <b>Regular Polygon</b>
        A polygon whose all sides have same length. 
        
        
        <b>Polygon names:</b>
        {polygon_names_html}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        problemTemplate.add_solution(result_all['svg_str'], 'text/svg')

        
        problemTemplate.add_answer(result_labelled['svg_str'], 'text/svg')
        return problemTemplate



    def _problem_name_prefixes(self, *args, **kwargs):
        gs_helper = self.base_helper.get_geometrical_shape_helper()
        
        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
    

    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
    
    
                