'''
Created on 10-Jun-2021

@author: bthakur
'''
import numpy as np
from xv.util import toIntOrFloat, locatePath
from sympy import (Symbol, 
                   symbols, Rational, sympify, pi, E, latex, solve, Eq, simplify)
import random
import matplotlib.pyplot as plt
import io
from xv.km.util import latex_fraction
import pathlib
import csv
import operator
import inspect
import traceback

# temperature_conv_helper
# TemperatureConvHelper

class TemperatureConvHelper:
    
    def __init__(self, **kwargs):
        self.verbose = kwargs.get('verbose', False)
        
        self.C_name = symbols('Celcuis')
        self.F_name = symbols('Fahrenheit')
        self.K_name = symbols('Kelvin')
        
        self.C, self.F, self.K = symbols('C, F, K')
        self.C_unit = f'^{{\\circ}} {{C}}'
        self.F_unit = f'^{{\\circ}} {{F}}'
        self.K_unit = 'K'

        
        self.C_range = [0, 100]
        self.F_range = [32, 212]
        self.K_range = [273.14, 373.14]
        
        self.symbs = [self.C, self.F, self.K]
        self.names = [self.C_name, self.F_name, self.K_name]
        self.units = [self.C_unit, self.F_unit, self.K_unit]
        self.ranges = [self.C_range, self.F_range, self.K_range]
        pass

    def get_names(self, name_idxs):
        names = [x for idx, x in enumerate(self.names) 
                 if idx in name_idxs]
        return names
        
    def get_units(self, name_idxs):
        units = [x for idx, x in enumerate(self.units) 
                 if idx in name_idxs]
        return units
        
    def get_symbs(self, name_idxs):
        symbs = [x for idx, x in enumerate(self.symbs) 
                 if idx in name_idxs]
        return symbs
        
    
    def get_conversion_eq(self, name_idxs):
        idx1, idx2 = random.sample(name_idxs, 2)

        C = self.symbs[idx1]
        F = self.symbs[idx2]
        C_mp, C_bp = self.ranges[idx1]
        F_mp, F_bp = self.ranges[idx2]
        
        delta_C_mp_eval = C - C_mp
        delta_C_bp_eval = C_bp - C_mp
        
        delta_F_mp_eval = F - F_mp
        delta_F_bp_eval = F_bp - F_mp
        
        eq_C_F = Eq(delta_F_mp_eval / delta_C_mp_eval , 
                    Rational(delta_F_bp_eval, delta_C_bp_eval) )
        return eq_C_F
        pass

    def get_conversion_solution(self, name_idxs):
        idx1, idx2 = random.sample(name_idxs, 2)

        C = self.symbs[idx1]
        F = self.symbs[idx2]
        C_mp, C_bp = self.ranges[idx1]
        F_mp, F_bp = self.ranges[idx2]
        
        delta_C_mp = Symbol(f""" {latex(C)} - {latex(C_mp)} """)
        delta_C_bp = Symbol(f""" {latex(C_bp)} - {latex(C_mp)} """)
        
        delta_F_mp = Symbol(f""" {latex(F)} - {latex(F_mp)} """)
        delta_F_bp = Symbol(f""" {latex(F_bp)} - {latex(F_mp)} """)
        
        delta_C_mp_eval = C - C_mp
        delta_C_bp_eval = C_bp - C_mp
        
        delta_F_mp_eval = F - F_mp
        delta_F_bp_eval = F_bp - F_mp
        
        constant_symb = Symbol(f"Constant")
        delta_C_symb = Symbol(f"\\Delta {C}")
        delta_F_symb = Symbol(f"\\Delta {F}")
        
        eq_C_F0 = Eq(delta_F_symb / delta_C_symb , constant_symb )

        eq_C_F1 = Eq(delta_F_mp / delta_C_mp , delta_F_bp / delta_C_bp )

        eq_C_F = Eq(delta_F_mp_eval / delta_C_mp_eval , 
                    Rational(delta_F_bp_eval, delta_C_bp_eval) )

        
        soln_s = f"""
        Equation to convert between {self.names[idx1]} and  {self.names[idx2]}:

        $\\displaystyle {latex(eq_C_F0)}$

        $\\displaystyle \\implies {latex(eq_C_F1)}$

        $\\displaystyle \\implies {latex(eq_C_F)}$

        """
        
        return soln_s
        pass



    pass
#class TemperatureConvHelper