'''
Created on 12-Jul-2021

@author: bthakur
'''
import random
import inspect
import traceback   #        {inspect.stack()[0][3]}

import re
from sympy import (Eq, symbols, sqrt, latex,
                   Rational, solve, log, nsimplify, E, exp,  
                   sin, cos, I, series, pi,
                   UnevaluatedExpr as ueval)
from sympy.abc import theta
import sympy

from ....util import LatexEquation, MathUtil

from xv.km.util import latex_fraction
from xv.util import UtilClass

# _stmt_to_expr_arithmetic_opreration
# StmtToExprArithmeticOpreration

class StmtToExprArithmeticOpreration(UtilClass):
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        
        pass

    def get_problem_arithmetic_opreration(self, *args, **kwargs):
        '''
        '''
        
        methods = [
            self._statement_to_expr_type_a,
            self._statement_to_expr_inverse_properties,
            ]
        
        method = random.choice(methods)
        return method(*args, **kwargs)
        pass

    def _statement_to_expr_type_a(self, *args, **kwargs):
        '''
        '''
        x, y, z = symbols("x, y, z")
        nx = random.randint(-9, 9)
        ny = random.randint(-9, 9)
        nz = random.randint(-9, 9)
        
        p_tuples = []
        
        eq = Eq(nx + ueval(x), nx + ny)
        soln_eq = Eq(x, ny)
        
        prob_s = f"""
        The sum of two integers is ${latex(eq.rhs)}$ . 
        If one of them is ${latex(eq.rhs - soln_eq.rhs)}$ , then what is the other?
        """.replace("\n", " ").strip()

        soln_s = f"""
        The sum of ${latex(eq.rhs - soln_eq.rhs)}$ and another integer is ${latex(eq.rhs)}$ . 
        
        $\\displaystyle \\implies
        {latex(nx)} + {latex(x)} = {latex(nx + ny)}
        $
        $\\displaystyle \\implies
        {latex(soln_eq)}
        $
        """
        
        answer_s = f"""
        $\\displaystyle {latex(soln_eq.rhs)}
        $
        """
        p_tuples.append([prob_s, soln_s, answer_s])

        eq = Eq(ueval(x) - ueval(nx), ny)
        soln_eq = Eq(x, nx + ny)
        
        prob_s = f"""
        The difference of ${latex(x)}$ 
        and ${latex(nx)}$ is ${latex(ny)}$ . What is the value of ${latex(x)}$ ?
        """.replace("\n", " ").strip()

        soln_s = f"""
        The difference of ${latex(x)}$ and ${latex(nx)}$ is ${latex(ny)}$
        
        $\\displaystyle  \\implies
        {latex(x)} - {latex(nx)} = {latex(ny)}
        $
        $\\displaystyle \\implies
        {latex(soln_eq)}
        $
        """
        
        answer_s = f"""
        $\\displaystyle {latex(soln_eq.rhs)}
        $
        """
        p_tuples.append([prob_s, soln_s, answer_s])

        p_tuple = random.choice(p_tuples)
        
        result = {
            'prob_s': p_tuple[0].strip(),
            'soln_s': p_tuple[1].strip(),
            'answer_s': p_tuple[2].strip(),
            
            }
        return result
        pass

    def _statement_to_expr_inverse_properties(self, *args, **kwargs):
        '''
        '''
        x, y, z = symbols("x, y, z")
        nx = random.randint(-9, 9)
        ny = random.randint(-9, 9)
        nz = random.randint(-9, 9)
        
        
        prob_s = f"""
        What is the product of additive inverse of ${latex(nx)}$ 
        and multiplicative inverse of ${latex(ny)}$ ?
        """.replace("\n", " ").strip()

        soln_s = f"""
        The product of additive inverse of ${latex(nx)}$ and multiplicative inverse of ${latex(ny)}$
        
        $\\displaystyle 
        = \\text {{ (additive inverse of {latex(nx)}) }} 
        \\cdot  \\text {{ multiplicative inverse of {latex(ny)} }}
        $
        
        $\\displaystyle 
        = - {latex(nx)}
        \\cdot  \\frac {{ 1 }} {{ {latex(ny)} }}
        $
        
        $\\displaystyle \
        = {latex(Rational( -nx, ny ))}
        $
        """
        
        answer_s = f"""
        $\\displaystyle 
        {latex(Rational( -nx, ny ))}
        $
        """
        
        
        result = {
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            
            }
        return result
        pass
    
    
    pass
