'''
Created on 01-Apr-2021
@author: rishi_priyansh
'''

from sympy import ( Eq,
                    Limit,
                    Pow, Mul, Add, exp,
                    Function, latex, Integral, solve, 
                    UnevaluatedExpr, S, oo, log, logcombine)


import numpy as np
import random
import inspect
import traceback
from sympy import (symbols, Symbol)

from xv.km.util import latex_fraction, latex_atom
from ...helper import BaseHelper

from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate

# state_change_manager
# StateChangeManager

        
class StateChangeManager(BaseManager):
    '''
    State change of matter when heat is passed
    Example:
        ke = StateChangeManager()
        ke
        
        ke.printProblemTypes()
        
        ke.getRandomProblem()
        ke.getRandomProblem(problem_type = 0)
        ke.getRandomProblem(problem_type = 1)
        ...
        
        ke.printProblem()
        ke.printAnswer()
        ke.printSolution()
        
        
    '''    
    def __new__(cls, *args, **kwargs):
        instance = super(StateChangeManager, cls).__new__(cls, *args, **kwargs)
        for base_class in StateChangeManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        pass

    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
                'problem_introduction_to_state_change',
             ])
        pass

    def problem_introduction_to_state_change(self, *args, **kwargs):
        
        degree_latex = "^{\circ}"
        degree_latex_s = "$\displaystyle ^{\circ}$"
            
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        prob_s = f"""
        Plot the graph of of ice heated from -20{degree_latex_s} to +120{degree_latex_s}
        
        What is the heat in Joules required to convert 25 grams of -10 {degree_latex_s}C ice into 150 {degree_latex_s}C steam?
        heat of fusion of water = 334 J/g
        heat of vaporization of water = 2257 J/g
        specific heat of ice = 2.09 J/g·{degree_latex_s}C
        specific heat of water = 4.18 J/g·{degree_latex_s}C
        specific heat of steam = 2.09 J/g·{degree_latex_s}C

        """
        problemTemplate.add_problem(prob_s.strip(), 'text/html')
        
        soln_s = f'''
        The total energy required is the sum of the energy to heat the -10 °C ice to 0 °C ice, melting the 0 °C ice into 0 °C water, heating the water to 100 °C, converting 100 °C water to 100 °C steam and heating the steam to 150 °C. To get the final value, first calculate the individual energy values and then add them up.
        
        Step 1:
        
        Find the heat required to raise the temperature of ice from -10 °C to 0 °C. Use the formula:
        
        q = mcΔT
        
        
        where
        
        q = heat energy
        m = mass
        c = specific heat
        ΔT = change in temperature
        In this problem:
        
        q = ?
        m = 25 g
        c = (2.09 J/g·°C
        ΔT = 0 °C - -10 °C (Remember, when you subtract a negative number, it is the same as adding a positive number.)
        
        Plug in the values and solve for q:
        
        
        q = (25 g)x(2.09 J/g·°C)[(0 °C - -10 °C)]
        q = (25 g)x(2.09 J/g·°C)x(10 °C)
        q = 522.5 J
        
        
        The heat required to raise the temperature of ice from -10 °C to 0 °C = 522.5 J
        
        
        Step 2:
        
        Find the heat required to convert 0 °C ice to 0 °C water.
        
        
        Use the formula for heat:
        
        
        q = m·ΔHf
        
        where
        
        q = heat energy
        m = mass
        ΔHf = heat of fusion
        For this problem:
        
        q = ?
        m = 25 g
        ΔHf = 334 J/g
        Plugging in the values gives the value for q:
        
        q = (25 g)x(334 J/g)
        q = 8350 J
        
        The heat required to convert 0 °C ice to 0 °C water = 8350 J
        
        
        Step 3:
        
        Find the heat required to raise the temperature of 0 °C water to 100 °C water.
        q = mcΔT
        q = (25 g)x(4.18 J/g·°C)[(100 °C - 0 °C)]
        q = (25 g)x(4.18 J/g·°C)x(100 °C)
        q = 10450 J
        The heat required to raise the temperature of 0 °C water to 100 °C water = 10450 J
        Step 4:
        
        Find the heat required to convert 100 °C water to 100 °C steam.
        q = m·ΔHv
        where
        q = heat energy
        m = mass
        ΔHv = heat of vaporization
        q = (25 g)x(2257 J/g)
        q = 56425 J
        The heat required to convert 100 °C water to 100 °C steam = 56425
        
        Step 5:
        
        Find the heat required to convert 100 °C steam to 150 °C steam
        q = mcΔT
        q = (25 g)x(2.09 J/g·°C)[(150 °C - 100 °C)]
        q = (25 g)x(2.09 J/g·°C)x(50 °C)
        q = 2612.5 J
        The heat required to convert 100 °C steam to 150 °C steam = 2612.5
        
        Step 6:
        
        Find total heat energy. In this final step, put together all of the answers from the previous calculations to cover the entire temperature range.
        
        
        HeatTotal = HeatStep 1 + HeatStep 2 + HeatStep 3 + HeatStep 4 + HeatStep 5
        HeatTotal = 522.5 J + 8350 J + 10450 J + 56425 J + 2612.5 J
        HeatTotal = 78360 J
        
        Answer:
        
        The heat required to convert 25 grams of -10 °C ice into 150 °C steam is 78360 J or 78.36 kJ.
        '''
        problemTemplate.add_solution(soln_s.strip(), 'text/html')

        answer_s = f'''
        The heat required to convert 25 grams of -10 °C ice into 150 °C steam is 78360 J or 78.36 kJ.
        '''
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        
        return problemTemplate
        pass

        