'''
Created on 26-Jan-2022
@author: bthakur
'''
import random
import inspect
import traceback
from sympy import latex

from ..helper import BaseHelper
from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate
        
# _profit_loss_manager
# ProfitLossManager

class ProfitLossManager(BaseManager):
    '''
    Example:
        ke = ProfitLossManager()
        ke
        
        ke.printProblemTypes()
        
        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(ProfitLossManager, cls).__new__(cls, *args, **kwargs)
        for base_class in ProfitLossManager.__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_gain_loss_percent',
                '_problem_purchase_cost',
                '_problem_units_bought_sold',
                '_problem_sales_price_profit_percent_relation',
                '_problem_percent_based_equations',
                '_problem_modelling_percentage',
                '_problem_modelling_layered_business',
                '_problem_discounts_taxes',
             ])
        pass
    
    def _problem_gain_loss_percent(self, *args, **kwargs):
        mp_helper = self.base_helper.get_modelling_percentage_helper()

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        result = mp_helper.get_model_type_gain_loss_percent(*args, **kwargs)
        
        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s, "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
        pass


    def _problem_purchase_cost(self, *args, **kwargs):
        mp_helper = self.base_helper.get_modelling_percentage_helper()

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        result = mp_helper.get_model_type_purchase_cost(*args, **kwargs)
        
        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s, "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
        pass


    def _problem_units_bought_sold(self, *args, **kwargs):
        mp_helper = self.base_helper.get_modelling_percentage_helper()

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        result = mp_helper.get_model_type_units_bought_sold(*args, **kwargs)
        
        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s, "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
        pass


    def _problem_sales_price_profit_percent_relation(self, *args, **kwargs):
        mp_helper = self.base_helper.get_modelling_percentage_helper()

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        result = mp_helper.get_model_sales_price_profit_percent_relation(*args, **kwargs)
        
        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s, "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
        pass

    def _problem_percent_based_equations(self, *args, **kwargs):
        mp_helper = self.base_helper.get_modelling_percentage_helper()

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        result = mp_helper.get_model_percent_based_equations(*args, **kwargs)
        
        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s, "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
        pass


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


        doc_style: xv_doc
        """
        soe_helper = self.base_helper.get_statement_to_expr_helper()
        result = soe_helper.get_problem_modelling_percentage()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        if result is None:
            problem_s = f"""
            Failed to get a problem.
            """
            problemTemplate.add_problem(problem_s.strip(), 'text/html')
            return problemTemplate
            
        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_modelling_layered_business(self, *args, **kwargs):
        """
        

        doc_style: xv_doc
        """
        soe_helper = self.base_helper.get_statement_to_expr_helper()
        result = soe_helper.get_problem_modelling_layered_business()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        if result is None:
            problem_s = f"""
            Failed to get a problem.
            """
            problemTemplate.add_problem(problem_s.strip(), 'text/html')
            return problemTemplate
            
        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_discounts_taxes(self, *args, **kwargs):
        """
        discounts and taxes
        
        doc_style: xv_doc
        """
        ste_helper = self.base_helper.get_statement_to_expr_helper()


        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        random_model  = ste_helper.get_discounts_taxes_prob()
        
        if random_model is None:
            problem_s = f"""
            Failed to get statement with units
            """
            problemTemplate.add_problem(problem_s.strip(), 'text/html')
            return problemTemplate

        problem_s = f"""
        {random_model['problem_s']}
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')

                
        soln_s = f"""
        {random_model['soln_s']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {random_model['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate



    def _problem_cost_price_profit_system(self, *args, **kwargs):
        """
        discounts and taxes
        
        doc_style: xv_doc
        """
        ste_helper = self.base_helper.get_statement_to_expr_helper()


        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        random_model  = ste_helper.get_discounts_taxes_prob()
        
        if random_model is None:
            problem_s = f"""
            Failed to get statement with units
            """
            problemTemplate.add_problem(problem_s.strip(), 'text/html')
            return problemTemplate

        problem_s = f"""
        {random_model['problem_s']}
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')

                
        soln_s = f"""
        {random_model['soln_s']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {random_model['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

