'''
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 RadiationManager(BaseManager):
    '''
    RadiationManager
    Example:
        ke = RadiationManager()
        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(RadiationManager, cls).__new__(cls, *args, **kwargs)
        for base_class in RadiationManager.__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_4_189',
            '_problem_4_190',
            '_problem_4_191',

            ])
        pass

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

        problem_s = f"""
        4.189. An electromagnetic wave of frequency $\\nu $ = 3.0 MHz passes
        from vacuum into a non-magnetic medium with permittivity a
        = 4.0.
        (a) what is the speed of light in the medium?
        (b) what is the wavelength of light in the medium?
        (c) what is the frequency of light in the medium?
        (d) Find the increment of its wavelength.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')

        soln_s = r"""
        $E$ = energy of light
        $h$ = planck's constant
        $\nu$ = frequency of light
        $\lambda_m$ = wavelength of light in the medium
        $c_m$ = velocity of light in the medium
        $\varepsilon_m$ = relative permittivity of the medium

        (a) the speed of light in the medium
        $c_m \propto \frac{1}{\sqrt{\varepsilon_m}}$
        $\Rightarrow c_m = \frac{1}{\sqrt{\varepsilon_m}} * c$

        (b)
        $c_m = \frac{1}{\sqrt{\varepsilon_m}} * c$
        $\Rightarrow \frac{c_m}{\nu} = \frac{1}{\sqrt{\varepsilon_m}} \frac{c}{\nu}$
        $\Rightarrow \lambda_m = \frac{1}{\sqrt{\varepsilon_m}} \frac{c}{\nu}$

        (c)
        As the photon goes from one medium to another medium, the energy of the photon remains constant.
        $E = h \nu$
        $\Rightarrow \nu \propto constant$
        as $E$ and $h$ are constants.
        $\Rightarrow \nu = \frac{c}{\lambda}$

        (d)
        $c_m \propto \frac{1}{\sqrt{\varepsilon_m}}$
        $\Rightarrow c_m = \frac{1}{\sqrt{\varepsilon_m}} * c$
        $\Rightarrow \Delta c_m = \frac{1}{\sqrt{\varepsilon_m}} * c - c = c(\frac{1}{\sqrt{\varepsilon_m}} - 1)$
        $\Rightarrow \frac{\Delta c_m}{\nu} = \frac{c}{\nu} (\frac{1}{\sqrt{\varepsilon_m}} - 1)$
        $\Rightarrow \Delta \lambda_m = \frac{c}{\nu} (\frac{1}{\sqrt{\varepsilon_m}} - 1)$
        Thus the change in wavelength of light (from its value in vaccum to its value in the medium) is
        $
        \Delta \lambda=\frac{c / \sqrt{\varepsilon}}{v}-\frac{c}{v}=\frac{c}{v}\left(\frac{1}{\sqrt{\varepsilon}}-1\right)=-50 \mathrm{~m}
        $
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')

        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')

        return problemTemplate


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

        problem_s = r"""
        4.190. A plane electromagnetic wave falls at right angles to the
        surface of a plane-parallel plate of thickness $l$. The plate is made
        of non-magnetic substance whose permittivity decreases exponentially from
        a value $\varepsilon_1$ at the front surface down to a value $\varepsilon_2$, at the
        rear one. How long does it take a given wave phase to travel across
        this plate?
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')

        soln_s = r"""
        From the data of the problem the relative permittivity of the medium varies as
        $
        \varepsilon(x)=\varepsilon_{1} e^{-(x / l) \ln \frac{\varepsilon_{1}}{\varepsilon_{2}}}
        $
        Hence the local velocity of light
        $
        v(x)=\frac{c}{\sqrt{\varepsilon(x)}}=\frac{c}{\sqrt{\varepsilon_{1}}} e^{\frac{x}{2 l} \ln \frac{\varepsilon_{1}}{\varepsilon_{2}}}
        $
        Thus the required time $t=\int_{0}^{l} \frac{d x}{v(x)}=\frac{\sqrt{\varepsilon_{1}}}{c} \int_{0}^{l} e^{-\frac{x}{2 l} \ln \frac{\varepsilon_{1}}{\varepsilon_{2}}}$
        $
        d x=\frac{\sqrt{\varepsilon_{1}}}{c} \frac{-e^{-\frac{1}{2} \ln \frac{\varepsilon_{1}}{\varepsilon_{2}}}+1}{\frac{1}{2 l} \ln \frac{\varepsilon_{1}}{\varepsilon_{2}}}=\frac{2 l}{c} \frac{\sqrt{\varepsilon_{1}}-\sqrt{\varepsilon_{2}}}{\ln \frac{\varepsilon_{1}}{\varepsilon_{2}}}
        $
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')

        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')

        return problemTemplate


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

        problem_s = r"""
        4.191. A plane electromagnetic wave of frequency v = 10 MHz
        propagates in a poorly conducting medium with conductivity
        a = 10 mS/m and permittivity $\varepsilon$ = 9. Find the ratio of amplitudes
        of conduction and displacement current densities.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')

        soln_s = fr"""
        Conduction current density $\vec{{J}} = \frac{{\vec{{I}}}}{{A}}$ <div style = "display: block; margin-left: 30%;">
        $\vec{{I}}$ = electric current
        $A$ = cross-section area of the wire
        </div>

        $= \frac{{\vec{{V}}}}{{R}} \frac{{1}}{{A}}$ <div style = "display: block; margin-left: 30%;">
        as $\vec{{V}} = \vec{{I}} R$
        $\vec{{V}}$ = potential difference across the wire
        $R$ = resistance
        </div>

        $= \frac{{\vec{{V}}}}{{\rho \frac{{l}}{{A}}}} \frac{{1}}{{A}}$ <div style = "display: block; margin-left: 30%;">
        as $R = \rho \frac{{l}}{{A}}$
        $\rho$ = resistivity
        $l$ = length of the wire
        </div>

        $= \frac{{\vec{{V}}}}{{\rho l}} $

        $= \frac{{\vec{{E}} l}}{{\rho l}} $ <div style = "display: block; margin-left: 30%;">
        as $\vec{{V}} = \vec{{E}} l$
        $\vec{{E}}$ = electric field
        </div>

        $= \frac{{\vec{{E}}}}{{\rho}} $

        $= \sigma \vec{{E}} $ <div style = "display: block; margin-left: 30%;">
        as $\rho = \frac{{1}}{{\sigma}}$
        $\sigma$ = conductivity
        </div>


        Conduction current density $\vec{{J}} =\sigma \vec{{E}}$

        Displacement current density $=\frac{{\partial \vec{{D}}}}{{\partial t}}=\varepsilon \varepsilon_{{0}} \frac{{\partial \vec{{E}}}}{{\partial t}}=i \omega \varepsilon \varepsilon_{{0}} \vec{{E}}$
        Ratio of magnitudes $=\frac{{\sigma}}{{\omega \varepsilon \varepsilon_{{0}}}}=\frac{{j_{{c}}}}{{j_{{d i s}}}}=2$, on puting the values.
        """
        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/

