'''
Created on 01-Apr-2021
@author: bthakur
'''

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


from ..helper import BaseHelper
from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate
        
class ArithmeticGridManager(BaseManager):

    '''
    ArithmeticGridManager
    Example:
        ke = ArithmeticGridManager()
        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(ArithmeticGridManager, cls).__new__(cls, *args, **kwargs)
        for base_class in ArithmeticGridManager.__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_simple_arithmetic_grid',
            ])
        pass
    
    def _problem_simple_arithmetic_grid(self, *args, **kwargs):
        '''
        '''
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        size = (random.randint(2, 5), random.randint(2, 5))

        aghelper = self.base_helper.get_arithmetic_helper().get_arithmetic_grid_helper()
        qty_grid = aghelper.get_qty_grid(size = size, 
                                         grid_type = "qty",
                                         show_price = True,
                                         #show_item_cost = True,
                                         #show_person_cost = True,
                                         )
        
        cost_grid = aghelper.get_qty_grid(size = size, 
                                          cost_data = qty_grid['cost_data'],
                                          grid_type = "cost",
                                          show_price = True,
                                          show_item_cost = True,
                                          show_person_cost = True,
                                          )
        
        problem_max = aghelper.get_problem_max_cost(qty_grid['cost_data'])
        problem_min = aghelper.get_problem_min_cost(qty_grid['cost_data'])
        
        sub_problems = [problem_max, problem_min]
        random.shuffle(sub_problems)
        
        sub_statements = [x['statement'] for x in sub_problems]
        sub_answers = [f"<b>{x['answer_statement']}</b>: {x['answer']}" 
                       for x in sub_problems]
        
        sub_statements = "\n\n".join(sub_statements)
        sub_answers = "\n\n".join(sub_answers)
        
        problem_s = f"""
        Some friends buy a few items and the quantity is given in the table below: 
        {qty_grid['grid_html']}
        <b>Answer the followings:</b>
        {sub_statements}
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        
        soln_s = f"""
        <b>Cost Grid:</b>
        {cost_grid['grid_html']}
        {sub_answers}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        Plese note than there are other possible solutions.
        """
        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

