'''
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 InequalityGridHelper


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

    '''
    InequalityGridManager
    Example:
        ke = InequalityGridManager()
        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(InequalityGridManager, cls).__new__(cls, *args, **kwargs)
        for base_class in InequalityGridManager.__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_arithmetic_grid_missing_values',
            '_problem_arithmetic_grid_find_expr',
            '_problem_arithmetic_grid_messed_up',
            ])
        pass
    
    def _problem_arithmetic_grid_missing_values(self, *args, **kwargs):
        '''
        Optional params:
        include_log = False #Include log operations
        row_count = 4  #integer
        col_count = 4  #integer
        '''
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        include_log = kwargs.get('include_log', False)
        row_count = kwargs.get('row_count', random.choice([3, 4, 5, 6, 7]))
        col_count = kwargs.get('col_count', random.choice([3, 4, 5, 6, 7]))

        aghelper = InequalityGridHelper(include_log = include_log)
        aghelper.init_grid(row_count = row_count, col_count = col_count)
        grid = aghelper.get_grid()
        self.grid = grid
        self.aghelper = aghelper
        
        problem_s = f"""
        Fill the missing cells of the grid. The grid has been formed with the following rules:
        
        1. {aghelper.generator_statement}
         
        2. {aghelper.rule_top_right_highest()}
        
        {grid.html_grid(v_latex = False, missing_values = True)}
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html', 
                                    add_html_line_break =  True)
        
        
        soln_s = f"""
        Suggested Solution:
        Plese note than there are other possible solutions.
        
        {grid.html_grid(highlight_cells = True)}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {grid.html_grid(v_latex = False, highlight_cells = True)}

        Plese note than there are other possible solutions.
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

    def _problem_arithmetic_grid_find_expr(self, *args, **kwargs):
        '''
        Optional params:
        include_log = False #Include log operations
        row_count = 4  #integer
        col_count = 4  #integer
        '''
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')


        include_log = kwargs.get('include_log', False)
        row_count = kwargs.get('row_count', None)
        col_count = kwargs.get('col_count', None)
        
        aghelper = InequalityGridHelper(include_log = include_log,
                                        unique_cells = True, 
                                        shuffled = True)
        aghelper.init_grid(row_count = row_count, col_count = col_count)
        grid = aghelper.get_grid()
        self.grid = grid
        self.aghelper = aghelper
        
        problem_s = f"""
        Find the expression for values in cells of the grid of numbers formed with the following rules:
        {aghelper.generator_statement}
        
        {grid.html_grid(expr_latex = False)}
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        Suggested Solution:
        Plese note than there are other possible solutions.
        
        {grid.html_grid()}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {grid.html_grid(v_latex = False)}

        Plese note than there are other possible solutions.
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

    def _problem_arithmetic_grid_messed_up(self, *args, **kwargs):
        '''
        Optional params:
        include_log = False #Include log operations
        row_count = 4  #integer
        col_count = 4  #integer
        '''
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        include_log = kwargs.get('include_log', False)
        row_count = kwargs.get('row_count', None)
        col_count = kwargs.get('col_count', None)
        
        aghelper = InequalityGridHelper(include_log = include_log,
                                        unique_cells = True)
        aghelper.init_grid(row_count = row_count, col_count = col_count)
        grid = aghelper.get_grid()
        self.grid = grid
        self.aghelper = aghelper
        
        problem_s = f"""
        The arrangement cells of the grid has been messed up. Please fix them.
        The grid should follow the following rules:
        
        1. {aghelper.generator_statement}
         
        2. {aghelper.rule_top_right_highest()}
        
        {grid.html_grid(v_latex = False, shuffled = True)}
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html', 
                                    add_html_line_break =  True)
        
        soln_s = f"""
        Suggested Solution:
        Plese note than there are other possible solutions.
        
        {grid.html_grid()}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {grid.html_grid(v_latex = False)}

        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

