'''
Created on 01-Apr-2021
@author: rishi_priyansh
'''
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
        
class RadioActivityManager(BaseManager):
    '''
    RadioActivityManager
    Example:
        ke = RadioActivityManager()
        ke.getRandomProblem()
        ke.getRandomProblem(problem_type = 0)
        ...
        
        ke.printProblem()
        ke.printAnswer()
        ke.printSolution()
        
        ke.printProblemTypes()
        
    '''    
    _OPR_TYPE_COUNT = 10
    
    def __new__(cls, *args, **kwargs):
        instance = super(RadioActivityManager, cls).__new__(cls, *args, **kwargs)
        for base_class in RadioActivityManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.setContent()
        self.T_half = f'T_\\frac{{1}}{{2}}'
         
        pass
    
    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
            '_problem_6_233',
            '_problem_6_226',
            '_problem_6_224',
            '_problem_6_221',
            '_problem_6_220',
            ])
        pass

    def _problem_6_233(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        element_Ra = latex_atom('Ra', 88, 226)
        element_U = latex_atom('U', 92, 238)
        element_Pb = latex_atom('Pb', 82, 206)
        element_Pt = latex_atom('Pt', 82, 206)
        element_Os = latex_atom('Os', 76, 206)
        problem_s = f"""
        6.233. 
        (a) What isotope is produced from the alpha-radioactive
        ${element_Ra}$ as a result of five alpha-disintegrations and four beta-disintegrations?
        (b) How many alpha- and beta- decays does ${element_U}$ experience before
        turning finally into the stable ${element_Pb}$ isotope?
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        (a) ${element_Ra} 
        \\xrightarrow{{5 \\alpha - particle}}
        \\space {element_Pt}
        \\xrightarrow{{4 \\beta - particle}}
        \\space {element_Pb}
        $
         
        (b) ${element_U} 
        \\xrightarrow{{8 \\alpha - particle}}
        \\space {element_Os}
        \\xrightarrow{{6 \\beta - particle}}
        \\space {element_Pb}
        $
        
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        (a) ${element_Pb}$
        (b) alpha = 8, beta = 6
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

    def _problem_6_226(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        
        element_Co58 = latex_atom('Co', mass_number = 58)
        element_Co59 = latex_atom('Co', mass_number = 59)
        
        
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = f"""
        6.226. The specific activity of a preparation consisting of radioactive
        ${element_Co58}$ and nonradioactive ${element_Co59}$ is equal to $2.2 \cdot 10^{{12}}$ dis/(s*g).
        The half-life of ${element_Co58}$ is 71.3 days. Find the ratio of the mass of
        radioactive cobalt in that preparation to the total mass 
        of the preparation (in per cent).
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        Symbols used:
        $A$ = acitivity of a radionuclide
        $N$ = number of particals of ${element_Co58}$
        $\\lambda$ = decay constant of ${element_Co58}$
        ${self.T_half}$ = half life of ${element_Co58}$
        $m$ = mass of ${element_Co58}$ in the preparation
        $M_{{58}}$ = molar mass of ${element_Co58} = 58 g$
        $N_A$ = Avogadro constant
        $SA$ = specific activity of a radionuclide
        $M$ = total mass of the preparation
        $n$ = number of moles of ${element_Co58}$ = $\\dfrac{{m}}{{M_{{58}}}}$
        
        Formulae used: 
        $N = n \\cdot N_A = \\dfrac{{m}}{{M_{{58}}}} N_A$ \t\t $\\cdots$ form.(i)
        $\\lambda = \\frac{{ln \\space 2}}{{{self.T_half}}}$ \t\t $\\cdots$ form.(ii)
        $A = -\\dfrac{{dN}}{{dt}} = \\lambda N$ \t\t $\\cdots$ form.(iii)
        $SA = -\\dfrac{{dN}}{{dt}} \\dfrac{{1}}{{M}}$ \t\t $\\cdots$ form.(iv)
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        soln_s = f"""
        Solution:
        The activity of a nuclide is the number of disintegrations per second, or the number of unstable atomic nuclei that decay per second in a given sample.
        $A = -\\dfrac{{dN}}{{dt}} = \\lambda N$ \t\t $\\cdots$ (i)
        
        Substituting $N$ and $\\lambda$ from form.(i) and form.(ii) respectively,
        $\\implies -\\dfrac{{dN}}{{dt}} = \\dfrac{{ln \\space 2}}{{{self.T_half}}}\\dfrac{{m}}{{M_{{58}}}} N_A$ \t\t $\\cdots$ (ii)

        The specific activity is the activity per unit quantity(mass),
        $SA = -\\dfrac{{dN}}{{dt}} \\dfrac{{1}}{{M}} = \\dfrac{{ln \\space 2}}{{{self.T_half}}}\\dfrac{{m}}{{M_{{58}}}} N_A \\dfrac{{1}}{{M}}$ \t\t $\\cdots$ (iii)

        $\\implies \\dfrac{{m}}{{M}} = SA \\dfrac{{{self.T_half}}}{{ln \\space 2}} M_{{58}} \\dfrac{{1}}{{N_A}}$ \t\t $\\cdots$ (iv)
        
        $\\implies \\dfrac{{m}}{{M}} 
        = \\left ( 2.2 \\cdot 10^{{12}} \\right )
        \\dfrac{{71.3 \\space day}}{{ln \\space 2}}
        \\left ( 58 g \\right )
        \\dfrac{{1}}{{6.022 \\cdot 10^{{23}}}}
        $ \t\t $\\cdots$ (v)
        
        The ratio of the mass of ${element_Co58}$ in that preparation to the total mass of the preparation 
        $\\implies  \\dfrac{{m}}{{M}}= 1.88 \\cdot 10^{{-3}} = 0.188 \\%$
        
        
        
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        $\\dfrac{{m}}{{M}}= 1.88 \\cdot 10^{{-3}} = 0.188 \\%$
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_6_224(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        element_Na24 = latex_atom('Na', mass_number = 24)
        element_U235 = latex_atom('U', mass_number = 235)
        problem_s = f"""
        6.224. Calculate the specific activities of ${element_Na24}$ and ${element_U235}$
        nuclides whose half-lifes are $15$ hours and $7.1 \\cdot 10^8$ years respectively. 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html', add_html_line_break = False)
        
        soln_s = f"""
        Symbols used:
        $N$ = number of particals 
        $\\lambda$ = decay constant
        ${self.T_half}$ = half life
        $m$ = total mass
        $M$ = molar mass
        $N_A$ = Avogadro constant
        $n$ = number of moles 
        
        Formulae used: 
        $N = n \\cdot N_A = \\dfrac{{m}}{{M}} N_A$ \t\t $\\cdots$ form.(i)
        $\\lambda = \\frac{{ln \\space 2}}{{{self.T_half}}}$ \t\t $\\cdots$ form.(ii)
        $SA = -\\dfrac{{dN}}{{dt}} \\dfrac{{1}}{{M}}$ \t\t $\\cdots$ form.(iv)
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        soln_s = f"""
        Solution:
        The specific activity is defined as the activity per quantity of atoms of a particular radionuclide.
        $ SA = -\\dfrac{{dN}}{{dt}} \\dfrac{{1}}{{m}} = \\lambda N \\dfrac{{1}}{{m}}$ \t\t $\\cdots$ (i)
        
        Substituing $\\lambda $ from form.(ii),
        $\\implies SA = \\dfrac{{ln \\space 2}}{{{self.T_half}}} \\dfrac{{N}}{{m}}$ \t\t $\\cdots$ (ii)
        
        Simplifying form.(i) leads to $\\dfrac{{N}}{{m}} = \\dfrac{{N_A}}{{M}}$,
        
        $\\implies SA = \\dfrac{{ln \\space 2}}{{{self.T_half}}} \\dfrac{{N_A}}{{M}}$ \t\t $\\cdots$ (iii)
        
        This is the general formula for specific activity of any radionuclide,
        $\\implies SA = \\dfrac{{ln \\space 2}}{{{self.T_half}}} \\dfrac{{N_A}}{{M}}$
        
        For ${element_Na24}$,
        $\\implies SA = \\dfrac{{ln \\space 2}}{{15 \\space hour}} \\dfrac{{6.022 \\cdot 10^{{23}}}}{{24 g}}$
        $\\implies SA = 3.22 \\cdot 10^17 \\cdot s^{{-1}} g^{{-1}}$
        
        For ${element_U235}$,
        $\\implies SA = \\dfrac{{ln \\space 2}}{{7.1 \\cdot 10^8 \\space year}} \\dfrac{{6.022 \\cdot 10^{{23}}}}{{235 g}}$
        $\\implies SA = 0.793 \\cdot 10^5 \\cdot s^{{-1}} g^{{-1}}$
        
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        For ${element_Na24}$,
        $\\implies SA = 3.22 \\cdot 10^17 \\cdot s^{{-1}} g^{{-1}}$
        
        For ${element_U235}$,
        $\\implies SA = 0.793 \\cdot 10^5 \\cdot s^{{-1}} g^{{-1}}$
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_6_221(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        element_U238 = latex_atom('U', mass_number = 238)
        problem_s = f"""
        6.221. A ${element_U238}$ preparation of mass $1.0 g$ emits $1.24 \\cdot 10^4 $ alpha-particles
        per second. Find the half-life of this nuclide and the activity of the preparation. 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        Symbols used:
        $N$ = number of particals 
        $\\lambda$ = decay constant
        ${self.T_half}$ = half life
        $N_A$ = Avogadro constant
        $m$ = total mass
        $M$ = molar mass
        $n$ = number of moles
        
        Formulae used: 
        $N = n \\cdot N_A = \\dfrac{{m}}{{M}} N_A$ \t\t $\\cdots$ form.(i)
        ${self.T_half} = \\dfrac{{ln \\space 2}}{{\\lambda}}$ \t\t $\\cdots$ form.(ii)
        $A = -\\dfrac{{dN}}{{dt}} = \\lambda N$ \t\t $\\cdots$ form.(iii)
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        soln_s = f"""
        Solution:
        Half-life is constant over the lifetime of an exponentially decaying quantity,
        ${self.T_half} = \\dfrac{{ln \\space 2}}{{\\lambda}}$ \t\t $\\cdots$ (i)
        
        Solving for $\\lambda$,
        $-\\dfrac{{dN}}{{dt}} = \\lambda N = 1.24 \\cdot 10^4 s^{{-1}}$ \t\t $\\cdots$ (ii)
        
        $\\implies \\lambda  = 1.24 \\cdot 10^4 s^{{-1}} \\cdot \\dfrac{{1}}{{N}}$ \t\t $\\cdots$ (iii)
        
        Substituting $\\lambda$ from eq.(iii) in eq.(i) and form.(i) defines $1 g$ of ${element_U238}$ has $N = \\dfrac{{N_A}}{{238}}$ particles,
        $\\implies {self.T_half} = \\dfrac{{ln \\space 2}}{{1.24 \\cdot 10^4 s^{{-1}} \\cdot \\dfrac{{N_A}}{{238}}}}$ \t\t $\\cdots$ (iv)
    
        $\\implies {self.T_half} = \\dfrac{{ln \\space 2}}{{1.24 \\cdot 10^4 s^{{-1}} \\dfrac{{238}}{{6.022 \\cdot 10^{{23}}}}}}$ \t\t $\\cdots$ (v)
        
        ${self.T_half} = 4.49 \\cdot 10^9 year$
        
        
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        ${self.T_half} = 4.49 \\cdot 10^9 year$
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_6_220(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        element_Co55 = latex_atom('Co', mass_number = 55)
        
        problem_s = f"""
        6.220. Find the decay constant and the mean lifetime of ${element_Co55}$
        radionuclide if its activity is known to decrease $4.0%$ per hour.
        The decay product is nonradioactive.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        Symbols used:
        $A$ = acitivity of a radionuclide
        $N$ = number of particals of ${element_Co55}$
        $\\lambda$ = decay constant of ${element_Co55}$
        ${self.T_half}$ = half life of ${element_Co55}$
        
        Formulae used: 
        $A = -\\dfrac{{dN}}{{dt}} = \\lambda N$ \t\t $\\cdots$ form.(i)
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        soln_s = f"""
        Solution:
        The activity of a nuclide is the number of disintegrations per second, or the number of unstable atomic nuclei that decay per second in a given sample.
        $A = -\\dfrac{{dN}}{{dt}} = \\lambda N$ \t\t $\\cdots$ form.(i)
        
        roul$\\implies -\\dfrac{{dN}}{{N}} = \\lambda dt$ \t\t $\\cdots$ (i)
        
        let's assume that initally there were 100 atoms and after 1 hour 96 atoms would be left. Simplifying the form.(i),
        $\\implies \\int_{{N = 100}}^{{96}} \\dfrac{{dN}}{{N}} = \\int_{{t = 0}}^{{1 hour}} \\lambda dt$ \t\t $\\cdots$ (i)
        
        $\\implies \\left [ ln N \\right ]_{{100}}^{{96}} = \\lambda \\left [ t \\right ]_{{t = 0}}^{{1 hour}}$ \t\t $\\cdots$ (ii)
        
        $\\implies ln \\left (\\dfrac{{96}}{{100}} \\right ) = \\lambda 1 hour$ \t\t $\\cdots$ (iii)
        
        Switching the sides of eq.(iii)
        $\\implies \\lambda 1 hour = ln \\left (\\dfrac{{96}}{{100}} \\right )$ \t\t $\\cdots$ (iv)
        
        $\\implies \\lambda = ln \\left (\\dfrac{{96}}{{100}} \\right ) \\cdot \\dfrac{{1}}{{1 hour}}$ \t\t $\\cdots$ (v)
        
        $\\implies \\lambda = -0.0408 \\cdot hour^{{-1}}$ \t\t $\\cdots$ (vi)
        
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        $\\lambda = -0.0408 \\cdot hour^{{-1}}$
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

    def _TO_IMPLEMENT(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = f"""
        1.  What are atomic radii?
        2.  What happens to the atomic radius of an atom as you go down a group?
        3.  What happens to the atomic radius of an atom as you go across a period?
        4.  What is Ionization Energy?  Show an equation that demonstrates Ionization Energy.
        5.  What happens to the Ionization Energy of an atom as you go down a group?
        6.  What happens to the Ionization Energy of an atom as you go across a period?
        7. What is Electron Affinity?
        8.  What is Electronegativity?
        9.  What happens to the Electronegativity of an atom as you go down a group?
        10.  What happens to the Electronegativity of an atom as you go across a period?
        11.  Find the Ionization Energies of the following atoms from your reference table (chart S):
            a)  lithium
            b)  sodium
            c)  magnesium
            d)  fluorine
            e)  chlorine
            
            f)  oxygen
        12.  Find the Electronegativities of the following atoms from your reference table (chart S):
            a)  lithium
            b)  sodium
            c)  magnesium
            d)  fluorine
            e)  chlorine
            
            f)  oxygen
        13.  What atom is the most electronegative?
        1.  What are Valence Electrons?
        2.  What is a covalent bond?
        3.  a)  How many electrons does each atom want to have in its outer shell to be complete?
             b) What word is used to describe this number of electrons?
        4.  What is a diatomic molecule?  Show seven of them.
        5.  What is an Ionic bond?
        6.  How can you determine if a molecule is Ionic or covalent?
        7. What is a polar covalent bond?  Give an example.
        8.    What is a non-polar covalent bond?  Give an example.
        9.  Show the Ionic bonding between Potassium (K) and Fluorine (F).
        10.    a) Show the covalent bonding between  Hydrogen (H) and Bromine (Br).
                 b) Is this bond polar or non-polar?
        11.    a) Show the covalent bonding between  two Chlorine atoms.
            b) Is this bond polar or non-polar?
        12.    a) Show the covalent bonding of a water molecule.
                 b) Is this a polar or non-polar atom?
        13. What is a double bond?  What is a triple bond?
        14.  Show the bonding for CO2.
        15.  Show the bonding for N2.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _sample(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = f"""
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    

    #https://courses.lumenlearning.com/austincc-physics2/chapter/31-4-nuclear-decay-and-conservation-laws/
    
        