'''
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 ElectricCurrentManager(BaseManager):
    '''
    ElectricCurrentManager
    Example:
        ke = ElectricCurrentManager()
        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(ElectricCurrentManager, cls).__new__(cls, *args, **kwargs)
        for base_class in ElectricCurrentManager.__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_3_170',
            '_problem_3_171',
            '_problem_3_172',
            '_problem_3_173',
            '_problem_3_174',
            '_problem_3_175',
            '_problem_3_176',
            '_problem_3_177',
            '_problem_3_178',
            '_problem_3_179',
            '_problem_stored_data',
            
            ])
        pass
    
    def _problem_3_170(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        3.170. A capacitor with capacitance C = 400 pF is connected
        via a resistance R = 650 $\Omega$ to a source of constant voltage $V_0$.
        How soon will the voltage developed across the capacitor reach a
        value V = 0.90 $V_0$?

        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        3.170 The formula is,
        $$
        q=C V_{{0}}\left(1-e^{{-t / R C}}\right)
        $$
        or, 
        $$
        V=\frac{{q}}{{C}}=V_{{0}}\left(1-e^{{-t / R C}}\right) \text {{ or, }} \quad \frac{{V}}{{V_{{0}}}}=1-e^{{-t / R C}} 
        $$
        or, 
        $$
        e^{{-t / R C}}=1-\frac{{V}}{{V_{{0}}}}=\frac{{V_{{0}}-V}}{{V_{{0}}}}
        $$
        Hence, $t=R C \ln \frac{{V_{{0}}}}{{V_{{0}}-V}}=R C \ln 10$, if $V=0 \cdot 9 V_{{0}}$
        Thus $t=0.6 \mu \mathrm{{S}}$.
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_3_171(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        3.171. A capacitor filled with dielectric of permittivity $\varepsilon$ = 2.1
        loses half the charge acquired during a time interval $\tau$ = 3.0 min.
        Assuming the charge to leak only through the dielectric filler, calculate its resistivity. 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        3.171 The charge decays according to the foumula
        $$
        q=q_{{0}} e^{{-t / R C}}
        $$
        Here, $$R C= \text{{mean life}} = \text{{Half-life}} / \ln 2
        $$
        So, half life $=T=R C \ln 2$
        But,
        $$
        C=\frac{{\varepsilon \varepsilon_{{0}} A}}{{d}}, R=\frac{{\rho d}}{{A}}
        $$
        Hence,
        $$
        \rho=\frac{{T}}{{\varepsilon \varepsilon_{{0}} \ln 2}}=1.4 \times 10^{{13}} \Omega \cdot \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_3_172(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        3.172. A circuit consists of a source of a constant emf $\xi$ and a resist
        ante R and a capacitor with capacitance C connected in series. The
        internal resistance of the source is negligible. At a moment t = 0
        the capacitance of the capacitor is abruptly decreased $\eta$-fold. Find
        the current flowing through the circuit as a function of time t.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        3.172 Suppose $q$ is the charge at time $t$. Initially $q=C \xi$, at $t=0$.
        Then at time $t$,
        $$
        \begin{{gathered}}
        \quad \frac{{\eta q}}{{C}}-i R-\xi=0 \\
        \text {{ But }}, i=-\frac{{d q}}{{d t}}(-\text {{ sign because charge decreases) }}
        \end{{gathered}}
        $$
        So,
        $$
        \begin{{gathered}}
        \quad \frac{{\eta q}}{{C}}+R \frac{{d q}}{{d t}}=\xi \\
        \frac{{d q}}{{d t}}+\frac{{\eta}}{{R C}} q=\frac{{\xi}}{{R}}
        \end{{gathered}}
        $$
        or,
        $$
        \frac{{d}}{{d t}} q e^{{t \eta / R C}}=\frac{{\xi}}{{R}} e^{{t \eta / R C}} 
        $$
        or,
        $$
        \begin{{gathered}}
        q=\frac{{C \xi}}{{\eta}}+A e^{{-t \eta / R C}} \\
        A=C \xi\left(1-\frac{{1}}{{\eta}}\right), \text {{ from }} q=C \xi \text {{ at }} t=0 \\
        \end{{gathered}}
        $$
        Hence,
        $$
        q=C \xi\left(\frac{{1}}{{\eta}}+\left(1-\frac{{1}}{{\eta}}\right) e^{{-\eta t / R C}}\right) 
        $$
        Finally, 
        $$
        i=-\frac{{d q}}{{d t}}=\frac{{\xi(\eta-1)}}{{R}} e^{{-t \eta / R C}}
        $$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_3_173(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        3.173. An ammeter and a voltmeter are connected in series to a battery
        with an emf $\xi$ = 6.0 V. When a certain resistance is connected 
        in parallel with the voltmeter, the readings of the latter decrease
        $\eta$ = 2.0 times, whereas the readings of the ammeter increase the
        same number of times. Find the voltmeter readings after the connection of the resistance.

        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        3.173 Let $r=$ internal resistance of the battery. We shall take the resistance of the ammeter to $\mathrm{{be}}=0$ and that of voltmeter to be $\mathrm{{G}}$.
        Initially, $V=\xi-I r, I=\frac{{\xi}}{{r+G}}$

        So,$$
        V=\xi \frac{{G}}{{r+G}} \quad (1)
        $$
        After the voltmeter is shunted $$
        \frac{{V}}{{\eta}}=\xi-\frac{{\xi r}}{{r+\frac{{R G}}{{R+G}}}} \text {{ (Voltmeter) }} \quad (2)
        $$
        and$$
        \frac{{\xi}}{{r+\frac{{R G}}{{R+G}}}}=\eta \frac{{\xi}}{{r+G}} \text{{(Ammeter)}} \quad (3)
        $$

        From $(2)$ and $(3)$ we have
        $$
        \frac{{V}}{{\eta}}=\xi-\frac{{\eta r \xi}}{{r+G}} \quad (4)
        $$
        From (1) and (4)
        $$
        \frac{{G}}{{\eta}}=r+G-\eta r \text {{ or }} G=\eta r
        $$
        Then (1) gives the required reading
        $$
        \frac{{V}}{{\eta}}=\frac{{\xi}}{{\eta+1}}
        $$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_3_174(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        3.174. Find a potential difference $\varphi_1 - \varphi_2$ between points 1 and 2
        of the circuit shown in Fig. 3.39 if $R_1 = 10 \Omega$, $R_2 = 20 \Omega$, 
        $\xi_1$= 5.0 V, and $\xi_2$ = 2.0 V. The internal resistances of the current sources are negligible.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        3.174 Assume the current flow, as shown. Then potentials are as shown. Thus,
        $$
        \varphi_{{1}}=\varphi_{{1}}-I R_{{1}}+\xi_{{1}}-I R_{{2}}-\xi_{{2}}
        $$
        or, 
        $$
        \quad I=\frac{{\xi_{{1}}-\xi_{{2}}}}{{R_{{1}}+R_{{2}}}}
        $$
        And $$
        \quad \varphi_{{2}}=\varphi_{{1}}-I R_{{1}}+\xi_{{1}}
        $$
        So, $$
        \begin{{aligned}}
        \varphi_{{1}}-\varphi_{{2}}=-\xi_{{1}}+\frac{{\xi_{{1}}-\xi_{{2}}}}{{R_{{1}}+R_{{2}}}} R_{{1}} \\
        =-\left(\xi_{{1}} R_{{2}}+\xi_{{2}} R_{{1}}\right) /\left(R_{{1}}+R_{{2}}\right)=-4 \mathrm{{~V}}
        \end{{aligned}}
        $$

        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_3_175(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        3.175. Two sources of current of equal emf
        are connected in series and have different
        internal resistances $R_1$ and $R_2$. ($R_2$ > $R_1$).
        Find the external resistance R at which the 
        potential difference across the terminals of one
        of the sources (which one in particular?) becomes equal to zero.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        3.175 Let, us consider the current $i$, flowing through the circuit, as shown in the figure. Applying loop rule for the circuit, $-\Delta \varphi=0$
        $$
        -2 \xi+i R_{{1}}+i R_{{2}}+i R=0
        $$
        or, 
        $$
        \quad i\left(R_{{1}}+R_{{2}}^{{\prime}}+R\right)=2 \xi
        $$
        or,
        $$
        \quad i=\frac{{2 \xi}}{{R+R_{{1}}+R_{{2}}}}
        $$
        Now, if $$
        \begin{{aligned}}
        \varphi_{{1}}-\varphi_{{2}}=0\\
        -\xi+i R_{{1}}=0
        \end{{aligned}}
        $$
        or, 
        $$\frac{{2 \xi R_{{1}}}}{{R+R_{{1}}+R_{{2}}}}=\xi \text{{ and }} 2 R_{{1}}=R_{{2}}+R+R_{{1}}
        $$
        or, $R=R_{{1}}-R_{{2}}$, which is not possible as $R_{{2}}>R_{{1}}$
        Thus,
        $$
        \varphi_{{2}}-\varphi_{{3}}=-\xi+i R_{{2}}=0
        $$
        or,
        $$
        \frac{{2 \xi R_{{2}}}}{{R+R_{{1}}+R_{{2}}}}=\xi
        $$
        So, $R=R_{{2}}-R_{{1}}$, which is the required resistance.
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_3_176(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        3.176. N sources of current with different emf's are connected
        as shown in Fig. 3.40. The emf's of the sources are proportional to
        their internal resistances, i.e. $\xi = \alpha R$, where $\alpha$ is an assigned constant. The lead wire resistance is negligible. Find:
        (a) the current in the circuit;
        (b) the potential difference between points A and B dividing
        the circuit in n and N - n links. 
 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        3.176 (a) Current, $i=\frac{{N \xi}}{{N R}}=\frac{{N \alpha R}}{{N R}}=\alpha$, as $\xi=\alpha R$ (given)
        (b) $\varphi_{{A}}-\varphi_{{B}}=n \xi-n i R=n \alpha R-n \alpha R=0$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_3_177(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        3.177. In the circuit shown in Fig. 3.41 the sources have emf's
        $\xi_1$ = 1.0 V and $\xi_2$ = 2.5 V and the resistances have the values
        $R_1 = 10 \Omega$ and $R_2 = 20 \Omega$. The internal resistances of the sources
        are negligible. Find a potential difference $\varphi_A - \varphi_B$ between the
        plates A and B of the capacitor C. 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        3.177 As the capacitor is fully charged, no current flows through it. So, current
        $$
        i=\frac{{\xi_{{2}}-\xi_{{1}}}}{{R_{{1}}+R_{{2}}}}\left(\text {{ as }} \xi_{{2}}>\xi_{{1}}\right)
        $$
        And hence, $\varphi_{{A}}-\varphi_{{B}}=\xi_{{1}}-\xi_{{2}}+i R_{{2}}$
        $$
        \begin{{aligned}}
        &=\xi_{{1}}-\xi_{{2}}+\frac{{\xi_{{2}}-\xi_{{1}}}}{{R_{{1}}+R_{{2}}}} R_{{2}} \\
        &=\frac{{\left(\xi_{{1}}-\xi_{{2}}\right) R_{{1}}}}{{R_{{1}}+R_{{2}}}}=-0 \cdot 5 \mathrm{{~V}}
        \end{{aligned}}
        $$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

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

        problem_s = fr"""
        3.178. In the circuit shown in Fig. 3.42 the emf of the source is
        equal to $\xi$ = 5.0 V and the resistances are equal to $R_1$ = 4.0 $\Omega$
        and $R_2 = 6.0 \Omega$. The internal resistance of the source equals 
        R = 0.10 $\Omega$. Find the currents flowing through the resistances $R_1$
        and $R_2$."""
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        3.178 Let us make the current distribution, as shown in the figure.
        $$
        \text {{ Current }} i=\frac{{\xi}}{{R+\frac{{R_{{1}} R_{{2}}}}{{R_{{1}}+R_{{2}}}}}} \text {{ (using loop rule) }}
        $$
        So, current through the resistor $R_{{1}}$
        $$
        \begin{{aligned}}
        i_{{1}} &=\frac{{\xi}}{{R+\frac{{R_{{1}} R_{{2}}}}{{R_{{1}}+R_{{2}}}}}} \frac{{R_{{2}}}}{{R_{{1}}+R_{{2}}}} \\
        &=\frac{{\xi R_{{2}}}}{{R R_{{1}}+R R_{{2}}+R_{{1}} R_{{2}}}}=1.2 \mathrm{{~A}}
        \end{{aligned}}
        $$
        and similary, current through the resistor $R_{{2}}$
        $$
        i_{{2}}=\frac{{\xi}}{{R+\frac{{R_{{1}} R_{{2}}}}{{R_{{1}}+R_{{2}}}}}} \frac{{R_{{1}}}}{{R_{{1}}+R_{{2}}}}=\frac{{\xi R_{{1}}}}{{R R_{{1}}+R_{{1}} R_{{2}}+R R_{{2}}}}=0.8 \mathrm{{~A}}
        $$"""
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_3_179(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        3.179. Fig. 3.43 illustrates a potentiometric circuit by means of
        which we can vary a voltage V applied to a certain device possessing
        a resistance R. The potentiometer has a length l and a resistance
        $R_0$ , and voltage $V_0$ is applied to its terminals. Find the voltage V
        fed to the device as a function of distance x. Analyse separately the
        case $R >> R_0$.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        3.179 Total resistance =
        $$
        \begin{{gathered}}
        =\frac{{l-x}}{{l}} R_{{0}}+\frac{{R \cdot \frac{{x R_{{0}}}}{{l}}}}{{R+\frac{{x R_{{0}}}}{{l}}}} \\
        =\frac{{l-x}}{{l}} R_{{0}}+\frac{{x R R_{{0}}}}{{l R+x R_{{0}}}} \\
        =R_{{0}}\left[\frac{{l-x}}{{l}}+\frac{{x R}}{{l R+x R_{{0}}}}\right]
        \end{{gathered}}
        $$
        Then 
        $$
        V=\frac{{V_{{0}} \frac{{x R}}{{l R+x R_{{0}}}}}}{{ \left(1-\frac{{x}}{{l}}+\frac{{x R}}{{x R_{{0}}+l R}}\right)}} = \frac{{V_{{0}} R x}}{{\left\{{I R+R_{{0}} x\left(1-\frac{{x}}{{l}}\right)\right\}}}} 
        $$
        For 
        $$
        R \gg R_{{0}}, V \cong V_{{0}} \frac{{x}}{{l}}
        $$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_stored_data(self, *args, **kwargs):
        
        from .....storeddata import ElectricCurrentStoredQuestions
        keywords = kwargs.get('keywords', 'electric currents')
        
        isqs = ElectricCurrentStoredQuestions(verbose = self.verbose)
        result = isqs.get_random_question(keywords)
        # result = isqs.get_question_q1_15()

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        if result is None:
            prob_s = f"""
            Failed to get questions
            keywords = {keywords}
            """
            problemTemplate.add_problem(prob_s.strip(), 'text/html')
            return problemTemplate

        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s.strip(), 'text/html')

        soln_s = f"""
        {result['soln_s']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')

        answer_s = f"""
        {result['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    

