'''
Created on 07-Jan-2021
@author: bthakur

TODO: implement proof of all identities
'''

import random

from sympy import (symbols, latex, simplify, Eq,
                   pi,
                   sin, cos, tan,
                   csc, sec, cot
                   )

from xv.km.util import latex_fraction

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

# _trig_identity_manager
# TrigIdentityManager
        
class TrigIdentityManager(BaseManager):
    '''
    Trigonometric Identities


    Minimum Grade: 6
    Maximum Grade: 12


    Examples
    --------
    ke = TrigIdentityManager()
    ke 
    
    ke.printProblemTypes()
    ke.helpProblemType(0)
    
    ke.getRandomProblem()
    ke.getRandomProblem(problem_type = 0)


    ke.printAnswer()
    ke.printSolution()
    

    doc_style: xv_doc
    ''' 

    
    _OPR_TYPE_COUNT = 10
    
    def __new__(cls, *args, **kwargs):
        instance = super(TrigIdentityManager, cls).__new__(cls, *args, **kwargs)
        for base_class in TrigIdentityManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.base_helper = BaseHelper(**kwargs)
        pass
    

    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
            '_problem_trig_identities_summary',
            '_problem_triangle_identities_summary',
            '_problem_proof_of_identity_sets',
            '_problem_identities',
            '_problem_advanced_identities',
            ])
        pass


    def _problem_trig_identities_summary(self, *args, **kwargs):
        """

        doc_style: xv_doc
        """
        ti_helper = self.base_helper.get_trigonometry_helper().get_trig_identity_helper()
        identities_summary = ti_helper.get_trig_identities_summary()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        problem_s = f"""
        Summary
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        {identities_summary}
        """
        
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {soln_s}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

    def _problem_triangle_identities_summary(self, *args, **kwargs):
        """

        doc_style: xv_doc
        """
        ti_helper = self.base_helper.get_trigonometry_helper().get_trig_identity_helper()
        identities_summary = ti_helper.get_triangle_identities_summary()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        problem_s = f"""
        Summary
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        {identities_summary}
        """
        
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {soln_s}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

    def _problem_proof_of_identity_sets(self, *args, **kwargs):
        """

        doc_style: xv_doc
        """
        ti_helper = self.base_helper.get_trigonometry_helper().get_trig_identity_helper()
        result = ti_helper.get_problem_random_identities_set()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s.strip(), 'text/html')
        
        soln_s = f"""
        {result['soln_s']}
        """
        
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {result['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

    def _problem_identities(self, *args, **kwargs):
        """
        Basic Trigonometric Identities


        Minimum Grade: 6
        Maximum Grade: 12

    
        Examples
        --------
        from xv.math.trigonometry import TrigIdentityManager
        ke = TrigIdentityManager()


        ke.getRandomProblem(problem_type = '_problem_identities')


        doc_style: xv_doc
        """
        ti_helper = self.base_helper.get_trigonometry_helper().get_trig_identity_helper()

        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        A, B, C = ti_helper.get_angles()
        
        index = kwargs.get('index', None)
        
        result = ti_helper.get_random_identity(index = index)
        #self.eqs = eqs
        #result = random.choice(eqs)
        
        problem_s = f"""
        If A + B +  C = ${latex(pi)}$, prove 
        
        ${latex(result.get('eq'))}$
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        {result.get('proof')}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {result.get('proof')}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

    def _problem_advanced_identities(self, *args, **kwargs):
        """
        Advanced Trigonometric Identities


        Minimum Grade: 6
        Maximum Grade: 12

    
        Examples
        --------
        from xv.math.trigonometry import TrigIdentityManager
        ke = TrigIdentityManager()


        ke.getRandomProblem(problem_type = '_problem_advanced_identities')


        doc_style: xv_doc
        """

        ti_helper = self.base_helper.get_trigonometry_helper().get_trig_identity_helper()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        A, B, C = ti_helper.get_angles()
        
        index = kwargs.get('index', None)
        
        result = ti_helper.get_advanced_random_identity(index = index)
        
        problem_s = f"""
        If A + B +  C = ${latex(pi)}$, prove 
        
        ${latex(result.get('eq'))}$
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        {result.get('proof')}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {result.get('proof')}
        """
        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

