'''
Created on 16-Apr-2021

@author: rishi_priyansh
'''
from sympy import symbols, Eq, solve, Symbol, latex, sqrt, UnevaluatedExpr
from xv.km.ui import SvgHelper  
import matplotlib.pyplot as plt

import random
import inspect
import traceback

from ...helper import BaseHelper

from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate
from xv.util import list_to_str
        

   
class LightMechanicsManager(BaseManager):
    '''
    LightMechanicsManager
    Example:
        ke = LightMechanicsManager()
        ke.getRandomProblem()
        ke.getRandomProblem(problem_type = 0)
        ...
        
        ke.printProblem()
        ke.printAnswer()
        ke.printSolution()
        
        ke.printProblemTypes()
        
    '''    
    _OPR_TYPE_COUNT = 10
    
    def __new__(cls, *args, **kwargs):
        instance = super(LightMechanicsManager, cls).__new__(cls, *args, **kwargs)
        for base_class in LightMechanicsManager.__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_polarization',
            '_problem_mirror_formula_1',
            '_problem_mirror_formula_2',
            #'_problem_lens_equation_1',
            # '_problem_lens_equation_2',
            ])
        pass
    
    def _problem_polarization(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        wm_helper = self.base_helper.get_light_mechanics_helper()
        
        random_problem_method = random.choice([
            wm_helper.get_relation,
            wm_helper.get_computation,
            wm_helper.get_proportional_computation,
            ])
        result = random_problem_method(
                            'get_polarization',
                            as_strings = True)

        if not result:
            problemTemplate.add_problem("""
            Failed to get data
            """.strip(), 
            "text/html")
            return problemTemplate
        
        # problem_s = 'TODO'
        # problemTemplate.add_problem(problem_s, "text/html")
        
        problemTemplate.add_problem(result['problem_s'].strip(), "text/html")
        problemTemplate.add_solution(result['soln_s'], 'text/html')
        problemTemplate.add_answer(result['answer_s'].strip(), 'text/html')
        return problemTemplate
  
    def _problem_mirror_formula_2(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        wm_helper = self.base_helper.get_light_mechanics_helper()
        
        random_problem_method = random.choice([
            wm_helper.get_relation,
            wm_helper.get_computation,
            wm_helper.get_proportional_computation,
            ])
        result = random_problem_method(
                            'get_mirror_formula_2',
                            as_strings = True)

        if not result:
            problemTemplate.add_problem("""
            Failed to get data
            """.strip(), 
            "text/html")
            return problemTemplate
        
        # problem_s = 'TODO'
        # problemTemplate.add_problem(problem_s, "text/html")
        
        problemTemplate.add_problem(result['problem_s'].strip(), "text/html")
        problemTemplate.add_solution(result['soln_s'], 'text/html')
        problemTemplate.add_answer(result['answer_s'].strip(), 'text/html')
        return problemTemplate
  
    def _problem_mirror_formula_1(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        wm_helper = self.base_helper.get_light_mechanics_helper()
        
        random_problem_method = random.choice([
            wm_helper.get_relation,
            wm_helper.get_computation,
            wm_helper.get_proportional_computation,
            ])
        result = random_problem_method(
                            'get_mirror_formula_1',
                            as_strings = True)

        if not result:
            problemTemplate.add_problem("""
            Failed to get data
            """.strip(), 
            "text/html")
            return problemTemplate
        
        # problem_s = 'TODO'
        # problemTemplate.add_problem(problem_s, "text/html")
        
        problemTemplate.add_problem(result['problem_s'].strip(), "text/html")
        problemTemplate.add_solution(result['soln_s'], 'text/html')
        problemTemplate.add_answer(result['answer_s'].strip(), 'text/html')
        return problemTemplate
  
    def _problem_lens_equation_2(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        wm_helper = self.base_helper.get_light_mechanics_helper()
        
        random_problem_method = random.choice([
            wm_helper.get_relation,
            wm_helper.get_computation,
            wm_helper.get_proportional_computation,
            ])
        result = random_problem_method(
                            'get_lens_equation_2',
                            as_strings = True)

        if not result:
            problemTemplate.add_problem("""
            Failed to get data
            """.strip(), 
            "text/html")
            return problemTemplate
        
        problem_s = 'TODO'
        problemTemplate.add_problem(problem_s, "text/html")
        
        problemTemplate.add_problem(result['problem_s'].strip(), "text/html")
        problemTemplate.add_solution(result['soln_s'], 'text/html')
        problemTemplate.add_answer(result['answer_s'].strip(), 'text/html')
        return problemTemplate
  
    def _problem_lens_equation_1(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        wm_helper = self.base_helper.get_light_mechanics_helper()
        
        random_problem_method = random.choice([
            wm_helper.get_relation,
            wm_helper.get_computation,
            wm_helper.get_proportional_computation,
            ])
        result = random_problem_method(
                            'get_lens_equation_1',
                            as_strings = True)

        if not result:
            problemTemplate.add_problem("""
            Failed to get data
            """.strip(), 
            "text/html")
            return problemTemplate
        
        problem_s = 'TODO'
        problemTemplate.add_problem(problem_s, "text/html")
        
        problemTemplate.add_problem(result['problem_s'].strip(), "text/html")
        problemTemplate.add_solution(result['soln_s'], 'text/html')
        problemTemplate.add_answer(result['answer_s'].strip(), 'text/html')
        return problemTemplate

    def _sample(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

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


    
    