'''
Created on 02-Mar-2021

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

@author: bthakur
'''
import numpy as np
import random
import inspect
import traceback

from sympy import (simplify, sympify, log, latex, Symbol, symbols, 
                   UnevaluatedExpr as ueval)

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

        
class LinearEquationsAdvancedManager(BaseManager):
    '''
    Linear Equations
    Example:
        ke = LinearEquationsAdvancedManager()
        ke.getRandomProblem()
        ke.getRandomProblem(problem_type = 0)
        ke.getRandomProblem(problem_type = 1)
        ...
        ke.printProblem()
        ke.printAnswer()
        ke.printSolution()

        ke.printProblemTypes()
    '''    
    def __new__(cls, *args, **kwargs):
        instance = super(LinearEquationsAdvancedManager, cls).__new__(cls, *args, **kwargs)
        for base_class in LinearEquationsAdvancedManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.base_helper = BaseHelper(**kwargs)
        pass

    def _TEMPLATE_CUSTOM_QUESTIONS(self, keywords, **kwargs):
        
        from ...storeddata import MatrixStoredQuestions
        
        lsqs = MatrixStoredQuestions(verbose = self.verbose)
        result = lsqs.get_random_question(keywords)

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        if result is None:
            prob_s = f"""
            Failed to get questions
            keywords = {keywords}
            """
            problemTemplate.add_problem(prob_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
        pass


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

    def _problem_foundation_to_concept_of_matrix(self, *args, **kwargs):
        
        le_helper = self.base_helper.get_algebra_helper().get_linear_equations_helper()
        result = le_helper.get_foundation_to_concept_of_matrix(restraurent_count = 1)

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        if result is None:
            prob_s = f"""
            Failed to get questions
            """
            problemTemplate.add_problem(prob_s.strip(), 'text/html')
            return problemTemplate

        human_count = result['human_count']
        friend_names_str = result['friend_names_str']
        food_count = result['food_count']
        food_names_str = result['food_names_str']
        A = result['A']
        B = result['B']
        C = result['C']
        Adf = result['Adf']
        Bdf = result['Bdf']
        Cdf = result['Cdf']
        Cdf_latex = result['Cdf_latex']
        line_break = result['line_break']
    
        prob_s = f"""
        {human_count} friends, {friend_names_str} are having a party. 
        They order {food_count} items: {food_names_str}.
        <br />
        {Adf.to_html().strip()}
        <br />
        They prices of items they ordered are as follows:
        <br />
        {Bdf.to_html().strip()}
        <br />
        Calculate the cost for each friend.
        """
        prob_s = prob_s.replace("\n", "").replace(line_break, "<br/>")
        problemTemplate.add_problem(prob_s.strip(), 'text/html')
        
        soln_s = []
        
        soln_item = f"""
        Ordered quantities:
        <br />
        {Adf.to_html().strip()}
        <br />
        Prices of ordered items:
        <br />
        {Bdf.to_html().strip()}
        Let us calculate the cost:
        <br />
        {Cdf_latex.to_html().strip()}
        <br />
        <br />
        So, the cost is:
        <br />
        {Cdf.to_html().strip()}
        
        <br />
        <br />
        In short form, we write:
        <br />
        <br />
        $\\displaystyle 
        {latex(A)} {latex(B)} = {latex(C)}
        $

        <br />
        <br />
        In mathematical format:
        <br />
        $\\displaystyle 
        AB = C
        $
        """
        soln_item = soln_item.replace("\n", "").replace(line_break, "<br/>")
        soln_s.append(soln_item.strip())
        
        soln_s = "\n\n".join(soln_s)
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        The cost for each friend.
        <br/>
        {Cdf.to_html().strip()}
        """
        answer_s = answer_s.replace("\n", "").replace(line_break, "<br/>")
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
    
        return problemTemplate
        pass

