'''
Created on 13-Sep-2021

@author: Rishi Priyansh
'''
import random
import inspect
import traceback

from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate
        
class ElectrostaticFieldManager(BaseManager):
    '''
    ElectrostaticFieldManager
    Example:
        ke = ElectrostaticFieldManager()
        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(ElectrostaticFieldManager, cls).__new__(cls, *args, **kwargs)
        for base_class in ElectrostaticFieldManager.__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([
            '_find_electric_field_at_center_of_non_conducting_ring',
            '_find_electric_field_at_a_point_of_charged_filament',
            ])
        pass
        
    
    def _find_electric_field_at_center_of_non_conducting_ring(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        1.2. A thin nonconducting ring of radius R is charged with a
        linear density $\lambda = \lambda_{{0}} \cos \theta$, where $\lambda_{{0}}$ is a positive constant and $\theta$,
        is the azimuth angle. Find the electric field intensity E at the centre
        of the ring. 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        THe electric field at centre of a small part of the ring
        $\displaystyle \vec{{dE}} = \frac{{1}}{{4 \pi \epsilon_{{0}}}} \frac{{dq}}{{r^{{3}}}} \vec{{r}}$
         
        As raduis vector $\vec{{r}} = r \hat{{r}} = r \left ( \cos \theta \hat{{i}} + \sin \theta \hat{{j}} \right )$,
        $\displaystyle \Rightarrow \vec{{dE}} = \frac{{1}}{{4 \pi \epsilon_{{0}}}} \frac{{dq}}{{r^{{3}}}} r \left (\cos \theta \hat{{i}} + \sin \theta \hat{{j}} \right )$
        $\displaystyle \Rightarrow \vec{{dE}} = \frac{{1}}{{4 \pi \epsilon_{{0}}}} \frac{{dq}}{{r^{{2}}}} \left (\cos \theta \hat{{i}} + \sin \theta \hat{{j}}\right )$

        As $dq = \lambda dl = \lambda r d \theta = \lambda_{{0}} \cos \theta r d \theta$,
        $\displaystyle \Rightarrow \vec{{dE}} = \frac{{1}}{{4 \pi \epsilon_{{0}}}} \frac{{\lambda_{{0}} \cos \theta r d \theta}}{{r^{{2}}}}\left (\cos \theta \hat{{i}} + \sin \theta \hat{{j}} \right )$
        $\displaystyle \Rightarrow \vec{{dE}} = \frac{{1}}{{4 \pi \epsilon_{{0}}}} \frac{{\lambda_{{0}} \cos \theta d \theta}}{{r}}\left (\cos \theta \hat{{i}} + \sin \theta \hat{{j}} \right )$

        Grouping all the constants for the Integration,
        $\displaystyle \Rightarrow \vec{{dE}} = \frac{{\lambda_{{0}}}}{{4 \pi \epsilon_{{0}} r}} \cos \theta d \theta \left (\cos \theta \hat{{i}} + \sin \theta \hat{{j}} \right )$
        $\displaystyle \Rightarrow \int_{{0}}^{{\vec{{E}}}}\vec{{dE}} = \frac{{\lambda_{{0}}}}{{4 \pi \epsilon_{{0}} r}} \int_{{0}}^{{2\pi}} \cos \theta \left(\cos \theta \hat{{i}} + \sin \theta \hat{{j}} \right )d \theta$
        $\displaystyle \Rightarrow \vec{{E}} = \frac{{\lambda_{{0}}}}{{4 \pi \epsilon_{{0}} r}} \int_{{0}}^{{2\pi}} \cos \theta \left (\cos \theta \hat{{i}} + \sin \theta \hat{{j}} \right )d \theta$
        $\displaystyle \Rightarrow \vec{{E}} = \frac{{\lambda_{{0}}}}{{4 \pi \epsilon_{{0}} r}} \left ( \int_{{0}}^{{2\pi}} \cos^{{2}} \theta d \theta \hat{{i}} +  \int_{{0}}^{{2\pi}} \sin \theta \cos \theta d \theta \hat{{j}} \right )$

        $\displaystyle \int_{{0}}^{{2\pi}} \cos^{{2}} \theta d \theta = \left. \frac{{\sin 2\theta}}{{4}} + \frac{{\theta}}{{2}} \right]_{{0}}^{{2\pi}} = \pi$
        $\displaystyle \int_{{0}}^{{2\pi}} \sin \theta \cos \theta d \theta = \left. -\frac{{1}}{{4}} \cos 2\theta \right]_{{0}}^{{2\pi}} = 0 $
        $\displaystyle \Rightarrow \vec{{E}} = \frac{{\lambda_{{0}}}}{{4 \pi \epsilon_{{0}} r}} \left ( \pi \hat{{i}} + 0 \hat{{j}} \right )$
        $\displaystyle \Rightarrow \vec{{E}} = \frac{{\lambda_{{0}}}}{{4 \pi \epsilon_{{0}} r}} \pi \hat{{i}}$

        This is the electric field at the center of non-conducting ring,
        $\displaystyle \Rightarrow \vec{{E}} = \frac{{\lambda_{{0}}}}{{4\epsilon_{{0}} r}} \hat{{i}}$
        
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = fr"""
        $\Rightarrow \vec{{E}} = \frac{{\lambda_{{0}}}}{{4\epsilon_{{0}} r}} \hat{{i}}$
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _find_electric_field_at_a_point_of_charged_filament(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        1.3. A semi-infinite straight uniformly charged filament has a
        charge k per unit length. Find the magnitude and the direction of the
        field intensity at the point P(a, b).
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = fr"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    