'''
Created on 12-Jul-2021

@author: bthakur
'''
import random
import inspect
import traceback
import re
from sympy import (Eq, symbols, Symbol, sqrt, latex, factor,
                   Rational, solve, log, simplify, E, exp,  
                   sin, cos, I, series, pi)
from sympy.abc import theta
import sympy

from xv.km import CustomLogarithm as Log
from xv.km.util import latex_fraction, latex_exponent

from ...core import GeometricSeries

class GeometricSeriesHelper:
    def __init__(self, **kwargs):
        self.verbose = kwargs.get('verbose', False)
        self.implies = symbols(u'\U000021D2')
        pass
    
    def get_standard_series(self, a = None, real = True):
        if a is None:
            a = Symbol('a', real = real)
        r, n = symbols('r n', real = real)
        
        return GeometricSeries(a, r, n)
        return 

    def get_random_series(self, 
                           a = None,
                           r = None, 
                           n = None,
                           standard_values = False,
                           numeric_values = False,
                           real = True
                          ):
        if a is None:
            a = self._get_random_value(Symbol('x', real = real, positive = True), 
                                       standard_value = standard_values, 
                                       numeric_value = numeric_values, 
                                       minv = 1,
                                       maxv = 9)
            
        
        if r is None:
            r = self._get_random_value(Symbol('y', real = real, positive = True), 
                                       standard_value = standard_values, 
                                       numeric_value = numeric_values, 
                                       minv = 1,
                                       maxv = 9)
            if isinstance(r, int):
                r = r / 100
                
        if n is None:
            n = self._get_random_value(Symbol('z', real = real, positive = True), 
                                       standard_value = standard_values, 
                                       numeric_value = numeric_values, 
                                       minv = 5,
                                       maxv = 9)
            try:
                if n < 0:
                    n = -n
                if n < 4:
                    n = random.randint(4, 9)
            except:
                pass
            
        return GeometricSeries(a, r, n)
        pass

    def _get_random_value(self, 
                          default_symbol,
                          standard_value = False,
                          numeric_value = False,
                          minv = 0,
                          maxv = 10
                          ):
        if numeric_value:
            return random.randint(minv, maxv)
        
        if standard_value:
            return default_symbol
        
        return random.choice(
            [default_symbol,
                1, 
                -1,
                random.randint(1, 9) * random.choice([1, -1]), 
                Rational(1, random.randint(1, 9)), 
                default_symbol * (random.randint(1, 9) * random.choice([1, -1])), 
                default_symbol ** random.randint(1, 9), 
                default_symbol ** -random.randint(1, 9), 
                default_symbol ** Rational(1, random.randint(1, 9)), 
                default_symbol ** -Rational(1, random.randint(1, 9)), 
            ])
        pass
    
    def get_term_formula_statement(self):
        prev_term = Symbol(f"\\text{{previous term}}")
        next_term = Symbol(f"\\text{{next term}}")
        cr = Symbol(f"\\text{{common ratio}}")
        n = Symbol('n')

        s = f"""
        $\\displaystyle 
        {latex(next_term)} = {latex(prev_term )} \\cdot {latex(cr)}
        $
        $t_{{n}} = t_0 * {latex(cr ** n)}$
        
        Please note that we start count of terms from 0.
        """.strip()
        return s
        pass
    pass
#class GeometricSeriesHelper:
