'''
Created on 20-Jan-2022

@author: bthakur
'''

import inspect
import traceback
import random
import numpy as np
from sympy import (latex, Symbol, symbols,
                   Eq, Rational,
                   UnevaluatedExpr as ueval,
                   )

from xv.util import toIntOrFloat, listAttr
from xv.km.storeddata import BaseStoredQuestions
from xv.km import CustomLogarithm as Log
from xv.km.util import latex_log, latex_fraction, latex_exponent

# _grade8_stored_questions
# Grade8StoredQuestions

class Grade8StoredQuestions(BaseStoredQuestions):
    _QUESTION_KEYWORDS = None

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


    def get_question_q_power(self):
        keywords = [
            'grade8',
            'grade 8',
            'power',
            'power to power',
            'power_to_power',
            ]

        ints = list(np.random.randint(-10, 10, 30))
        #ints_pos = [x for x in ints if x > 0]
        #ints_neg = [x for x in ints if x < 0]
        #ints_non_zero = [x for x in ints if x != 0]
        #ints_non_zero_one = [x for x in ints if abs(x) >= 1]
        ints_ge_2 = [x for x in ints if abs(x) >= 2]

        b, n1, n2, n3, n4 = random.sample(ints_ge_2, 5)
        x, n = symbols('x, n')

        expr_num = ueval(ueval(b) ** n1) ** n2
        expr_den = ueval(ueval(b) ** n3) ** n4

        expr1_num = ueval(b) ** (n1 * n2)
        expr1_den = ueval(b) ** (n3 * n4)

        expr2 = ueval(b) ** (ueval(n1 * n2) - ueval(n3 * n4))
        expr = ueval(b) ** (n1 * n2 - n3 * n4)

        expr_latex = latex_fraction(latex(expr_num), latex(expr_den))
        expr1_latex = latex_fraction(latex(expr1_num), latex(expr1_den))

        base_form = ueval(x) ** n

        hint_s = rf"""
        <b>See the following managers for more detailed explanation:</b>
        from xv.math.basicmaths import PowerManager
        from xv.math.basicmaths import AdvancedPowerManager


        """

        prob_s = rf"""
        Simplify the expression in the form ${latex(base_form)}$.
        $\displaystyle
        {expr_latex}
        $
        """

        soln_s = rf"""
        $\displaystyle
        {expr_latex}
        $

        $\displaystyle
        = {expr1_latex}
        $

        $\displaystyle
        = {latex(expr2)}
        $

        $\displaystyle
        = {latex(expr)}
        $


        {hint_s.strip()}
        """

        answer_s = rf"""
        $\displaystyle
        = {latex(expr)}
        $
        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass


    def get_question_linear_equations(self):
        keywords = [
            'grade8',
            'grade 8',
            'linear equations',
            'linear_equations',
            ]



        hint_s = rf"""
        <b>See the following managers for more various ways to solve linear equations:</b>
        from xv.math.algebra import LinearEquationManager

        """

        from ..algebra import LinearEquationManager
        le_manager = LinearEquationManager(verbose = self.verbose)
        problem_type = random.randint(0, 4)
        le_manager.getRandomProblem(problem_type = 4, dim = 2)

        prob_s = le_manager._problem

        soln_s = le_manager._solution

        answer_s = le_manager._answer

        result = {
            'keywords': keywords,
            'prob_s': prob_s,
            'soln_s': soln_s,
            'answer_s': answer_s,
            }
        return result
        pass


    def get_question_parallel_lines(self):
        keywords = [
            'grade8',
            'grade 8',
            'parallel lines',
            ]
        n1, n2 = np.random.randint(-10, 10, 2)
        a1, a2 = np.random.randint(-10, 10, 2)
        b1, b2 = np.random.randint(-10, 10, 2)
        x, y = symbols('x, y')

        eq1 = Eq(a1 * x + a2 * y, a1 * n1 + a2 * n2)
        eq2 = Eq(b1 * x + b2 * y, b1 * n1 + b2 * n2)

        hint_s = rf"""
        <b>See the following managers for plotting and dealing with parallel lines:</b>
        from xv.math ?????????????????????

        """

        prob_s = rf"""
        Draw parallel lines and find values of angles.
        Also angles of triangles

        """

        soln_s = rf"""

        {hint_s.strip()}
        """

        answer_s = rf"""
        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass


    def get_question_algebric_expressions(self):
        keywords = [
            'grade8',
            'grade 8',
            'algebric expressions',
            ]
        a1 = random.randint(50, 70)
        a2 = random.randint(1, 4)
        p = random.randint(2, 10)
        q = random.randint(2, 10) + p
        a3 = Rational(p, q)
        t0 = random.randint(1, 3)
        t1 = random.randint(1, 3) + t0
        w, t = symbols('w, t')

        eq1 = Eq(w, a1 + a2 * t + a3 * t **2)


        w0_latex = f"{latex(a1)} + {latex(a2)} * {latex(t0)} + {latex(a3)} * {latex(t0)}^{{{2}}}"
        w1_latex = f"{latex(a1)} + {latex(a2)} * {latex(t1)} + {latex(a3)} * {latex(t1)}^{{{2}}}"

        w0 = a1 + a2 * t0 + a3 * t0 ** 2
        w1 = a1 + a2 * t1 + a3 * t1 ** 2
        dw = w1 - w0

        hint_s = rf"""
        <b>For algebric expressions and their operations, see the followings:</b>
        from xv.math.algebra import SingleVariableManager
        from xv.math.algebra import AlgebricExpressionManager

        """

        prob_s = rf"""
        A young sumo wrestler goes on a special diet to gain weight.
        The variable ${w}$ models the wrestler's weight (in kilograms) after the wrestler has been on a special diet for ${t1}$ months.
        $\displaystyle
        {latex(eq1)}
        $
        How much weight does the wrestler gain between ${latex(t0)}$ and ${latex(t1)} $ months after being on diet?
        """

        soln_s = rf"""
        $\displaystyle
        {latex(eq1)}
        $

        Weight after ${t0}$ months

        $\displaystyle
        = {w0_latex}
        $

        $\displaystyle
        = {latex(w0)}
        $


        Weight after ${t1}$ months

        $\displaystyle
        = {w1_latex}
        $

        $\displaystyle
        = {latex(w1)}
        $

        Hence, the increase in weight
        $\displaystyle
        = {latex(w1)} - {latex(w0)}
        $

        $\displaystyle
        = {latex(dw)}
        $

        {hint_s.strip()}
        """

        answer_s = rf"""
        $\displaystyle
        {latex(dw)}
        $

        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass


    def get_question_statistical_interpretation(self):
        keywords = [
            'grade8',
            'grade 8',
            'statistical interpretation',
            ]

        prob_s = rf"""
        TODO statistical interpretation
        """

        soln_s = rf"""


        """

        answer_s = rf"""


        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass


    def get_question_equilateral_triangle(self):
        keywords = [
            'grade8',
            'grade 8',
            'congruency',
            'similarity',
            'equilateral triangle',
            ]

        prob_s = rf"""
        TODO equilateral_triangle
        An equilateral triangle is Sometimes/always/never similar to a scalene triangle,
        because we can Sometimes/always/never
        map one onto the other using only dilations and rigid transformations.

        """

        soln_s = rf"""


        """

        answer_s = rf"""


        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass

    def get_question_volume(self):
        keywords = [
            'grade8',
            'grade 8',
            'volume',
            ]

        hint_s = rf"""
        <b>To know more about volumes, please see:</b>
        from xv.math.geometry import AreaManager
        from xv.math.geometry import VolumeAreaManager

        """

        prob_s = rf"""
        TODO volumes

        """

        soln_s = rf"""


        {hint_s.strip()}

        """

        answer_s = rf"""


        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass

    def get_question_scatter_plot(self):
        keywords = [
            'grade8',
            'grade 8',
            'scatter plot',
            ]

        hint_s = rf"""
        <b>To know more about scatter plots, please see:</b>


        """

        prob_s = rf"""
        TODO scatter_plot

        """

        soln_s = rf"""


        {hint_s.strip()}

        """

        answer_s = rf"""


        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass


    def get_question_transform_a_polygon(self):
        keywords = [
            'grade8',
            'grade 8',
            'transform a polygon',
            ]

        hint_s = rf"""
        <b>:</b>

        """

        prob_s = rf"""
        Plot and
        """

        soln_s = rf"""


        {hint_s.strip()}

        """

        answer_s = rf"""


        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass

    def get_question_SAMPLE(self):
        keywords = [
            'grade8',
            'grade 8',
            ]

        hint_s = rf"""
        <b>:</b>

        """

        prob_s = rf"""

        """

        soln_s = rf"""


        {hint_s.strip()}

        """

        answer_s = rf"""


        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass


    pass
#class Grade8StoredQuestions:
