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

# _arithmetic_series_helper
# ArithmeticSeriesHelper

from ...core import ArithmeticSeries

from xv.util import UtilClass

class ArithmeticSeriesHelper(UtilClass):
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        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 ArithmeticSeries(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 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 ArithmeticSeries(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):
        s = f"""
        $\\text {{next term = (previous term) + (common difference) }}$
        $t_{{n}} = t_{{0}} + n * \\text {{ common difference}}$
        
        Please note that we start count of terms from 0.
        """.strip()
        return s
        pass
    
    
    
    def get_problem_find_series_props(self):
        
        choice = random.randint(0, 1)
        if choice == 0:
            return self.get_series_props_from_two_sums()
                
        return self.get_series_props_from_two_sums()
        pass
    
    
    def get_series_props_from_two_sums(self):
        
        a, d = symbols('a, d')
        
        t0 = random.randint(1, 9) * random.choice([1, -1])
        cd = random.randint(1, 9) * random.choice([1, -1])
        
        n1 = random.randint(5, 10)
        n2 = random.randint(5, 10)
        n3 = random.randint(5, 10)
        
        tn1 = t0 + (n1 - 1) (cd)
        tn1n2 = t0 + (n1 + n2 - 1) (cd)
        tn1n2n3 = t0 + (n1 + n2 + n3 - 1) (cd)
        
        soln_stmts = []
        
        
        prob_s = f"""
        Find the sum of first ${latex(n1)}$ terms of an arithmetic progression 
        if the sum of next ${latex(n2)}$ terms is 10 
        and the sum of next ${latex(n3)}$ terms is 17.
        """.replace("\n", " ").strip()
        
        soln_stmt = f"""
        $$\\displaystyle
        \\begin{{aligned}}
        
        \\text {{Let }}
        
        \\text {{ first term }} 
        &= {latex(a)}
        \\\\
        
        \\text {{ common difference }} 
        &= {latex(d)}
        \\\\
        \\end{{aligned}}
        $$
        """
        soln_stmts.append(soln_stmt.strip())

        answer_s = f"""
        
        """
        
        soln_s = "\n\n".join(soln_stmts)
        
        result = {
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        
        return result
        pass
    
    
    
    
    pass
#class ArithmeticSeriesHelper:
