'''
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 MechchanicalOscillationsManager(BaseManager):
    '''
    MechchanicalOscillationsManager
    Example:
        ke = MechchanicalOscillationsManager()
        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(MechchanicalOscillationsManager, cls).__new__(cls, *args, **kwargs)
        for base_class in MechchanicalOscillationsManager.__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_21',
            '_problem_4_22',
            '_problem_4_23',
            '_problem_4_24',
            '_problem_4_25',            
            ])
        pass
            
    def _problem_4_21(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        4.21. A pendulum clock is mounted in an elevator car which starts
        going up with a constant acceleration w, with w < g. At a height h
        the acceleration of the car reverses, its magnitude remaining constant.
        How soon after the start of the motion will the clock show the right
        time again? 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        1.5 Let the particles collide at the point $A$ (Fig.), whose position vector is $\overrightarrow{{r_{{3}}}}$ (say). If $t$ be the time taken by each particle to reach at point $A$, from triangle law of vector addition :
        $\overrightarrow{{r_{{3}}}}=\overrightarrow{{r_{{1}}}}+\overrightarrow{{v_{{1}}}} t=\overrightarrow{{r_{{2}}}}+\overrightarrow{{v_{{2}}}} t$
        so, $\overrightarrow{{r_{{1}}}}-\overrightarrow{{r_{{2}}}}=\left(\overrightarrow{{v_{{2}}}}-\overrightarrow{{v_{{1}}}}\right) t$ (1)
        therefore, $t=\frac{{\left|\overrightarrow{{r_{{1}}}}-\overrightarrow{{r_{{2}}}}\right|}}{{\left|\overrightarrow{{v_{{2}}}}-\overrightarrow{{v_{{1}}}}\right|}}$ (2)
        From Eqs. (1) and (2) $\overrightarrow{{r_{{1}}}}=\overrightarrow{{r_{{2}}}}-\left(\overrightarrow{{v_{{2}}}}-\overrightarrow{{v_{{1}}}}\right) \frac{{\left|\overrightarrow{{r_{{1}}}}-\overrightarrow{{r_{{2}}}}\right|}}{{\left|\overrightarrow{{v_{{2}}}}-\overrightarrow{{v_{{1}}}}\right|}}$
        or, $\frac{{\overrightarrow{{r_{{1}}}}-\overrightarrow{{r_{{2}}}}}}{{\left|\overrightarrow{{r_{{1}}}}-\overrightarrow{{r_{{2}}}}\right|}}=\frac{{\overrightarrow{{v_{{2}}}}-\overrightarrow{{v_{{1}}}}}}{{\left|\overrightarrow{{v_{{2}}}}-\overrightarrow{{v_{{1}}}}\right|}}$, which is the sought relationship.
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_4_22(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        1.6. A ship moves along the equator to the east with velocity
        $v_o$ = 30 km/hour. The southeastern wind blows at an angle $\varphi$ = $60^{{\circ}}$
        to the equator with velocity v = 15 km/hour. Find the wind velocity
        $v'$ relative to the ship and the angle $\varphi'$ between the equator and the
        wind direction in the reference frame fixed to the ship. 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        1.6 We have $\vec{{v}}^{{\prime}}=\vec{{v}}-\overrightarrow{{v_{{0}}}}$ (1)
        From the vector diagram [of Eq. (1)] and using properties of triangle
        $v^{{\prime}}=\sqrt{{v_{{0}}^{{2}}+v^{{2}}+2 v_{{0}} v \cos \varphi}}=39.7 \mathrm{{~km}} / \mathrm{{hr}}$ (2)
        and $\frac{{v^{{\prime}}}}{{\sin (\pi-\varphi)}}=\frac{{v}}{{\sin \theta}} \quad$ or, $\sin \theta=\frac{{v \sin \varphi}}{{v^{{\prime}}}}$
        or $\theta=\sin ^{{-1}}\left(\frac{{v \sin \varphi}}{{v^{{\prime}}}}\right)$
        Using (2) and putting the values of $v$ and $d$
        $\theta=19.1^{{\circ}}$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_4_23(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        1.7. Two swimmers leave point A on one bank of the river to reach
        point B lying right across on the other bank. One of them crosses
        the river along the straight line AB while the other swims at right
        angles to the stream and then walks the distance that he has been
        carried away by the stream to get to point B. What was the velocity u
        of his walking if both swimmers reached the destination simultaneously? The stream velocity $v_0$ = 2.0 km/hour and the velocity $v'$
        of each swimmer with respect to water equals 2.5 km per hour. 

        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        1.7 Let one of the swimmer (say 1 ) cross the river along $A B$, which is obviously the shortest path. Time taken to cross the river by the swimmer $1 .$ 
        $t_{{1}}=\frac{{d}}{{\sqrt{{v^{{\prime 2}}-v_{{0}}^{{2}}}}}}$, (where $A B=d$ is the width of the river) (1)
        For the other swimmer (say 2), which follows the quickest path, the time taken to cross the river.
        $t_{{2}} = \frac{{d}}{{v^{{\prime}}}}$ (2)
        In the time $t_{{2}}$, drifting of the swimmer 2 , becomes
        $x=v_{{0}} t_{{2}}=\frac{{v_{{0}}}}{{v^{{\prime}}}} d$, (using Eq. 2) (3)
        If $t_{{3}}$ be the time for swimmer 2 to walk the distance $x$ to come from $C$ to $B$ (Fig.), then
        $$
        t_{{3}}=\frac{{x}}{{u}}=\frac{{v_{{0}} d}}{{v^{{\prime}} u}} \quad \text {{ (using Eq. 3) }}
        $$ (4)
        According to the problem $t_{{1}}=t_{{2}}+t_{{3}}$
        or,
        $$
        \frac{{d}}{{\sqrt{{v^{{\prime 2}}-v_{{0}}^{{2}}}}}}=\frac{{d}}{{v^{{\prime}}}}+\frac{{v_{{0}} d}}{{v^{{\prime}} u}}
        $$
        On solving we get
        $$
        u=\frac{{v_{{0}}}}{{\left(\frac{{1-v_{{0}}^{{2}}}}{{v^{{\prime 2}}}}\right)^{{-\frac{{1}}{{2}}}}-1}}=3 \mathrm{{~km}} / \mathrm{{hr}}
        $$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_4_24(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        1.8. Two boats, A and B, move away from a buoy anchored at the
        middle of a river along the mutually perpendicular straight lines:
        the boat A along the river, and the boat B across thg river. Having
        moved off an equal distance from the buoy the boats returned.
        Find the ratio of times of motion of boats $\tau_A / \tau_B$ if the velocity of
        each boat with respect to water is $\eta$ = 1.2 times greater than the
        stream velocity. 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        1.8 Let $l$ be the distance covered by the boat $A$ along the river as well as by the boat $B$ acr the river. Let $v_{{0}}$ be the stream velocity and $v^{{\prime}}$ the velocity of each boat with respect water. Therefore time taken by the boat $A$ in its journey
        $$
        t_{{A}}=\frac{{l}}{{v^{{\prime}}+v_{{0}}}}+\frac{{l}}{{v^{{\prime}}-v_{{0}}}}
        $$
        and for the boat $B$
        $$
        t_{{B}}=\frac{{l}}{{\sqrt{{v^{{\prime 2}}-v_{{0}}^{{2}}}}}}+\frac{{l}}{{\sqrt{{v^{{\prime 2}}-v_{{0}}^{{2}}}}}}=\frac{{2 l}}{{\sqrt{{v^{{\prime 2}}-v_{{0}}^{{2}}}}}}
        $$
        Hence,
        $$
        \frac{{t_{{A}}}}{{t_{{B}}}}=\frac{{v^{{\prime}}}}{{\sqrt{{v^{{\prime 2}}-v_{{0}}^{{2}}}}}}=\frac{{\eta}}{{\sqrt{{\eta^{{2}}-1}}}}\left(\text {{ where }} \eta=\frac{{v^{{\prime}}}}{{v}}\right)
        $$
        On substitution $\quad t_{{A}} / t_{{B}}=1 \cdot 8$

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

        problem_s = fr"""
        1.9. A boat moves relative to water with a velocity which is n
        = 2.0 times less than the river flow velocity. At what angle to the
        stream direction must the boat move to minimize drifting? 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        $1.9$ Let $v_{{0}}$ be the stream velocity and $v^{{\prime}}$ the velocity of boat with respect to water. $A$ $\frac{{v_{{0}}}}{{v^{{\prime}}}}=\eta=2>0$, some drifting of boat is inevitable. Let $\vec{{v}}$ make an angle $\theta$ with flow direction. (Fig.), then the time taken to cross the rive $t=\frac{{d}}{{v^{{\prime}} \sin \theta}}$ (where $d$ is the width of the river)
        In this time interval, the drifting of the boat $x=\left(v^{{\prime}} \cos \theta+v_{{0}}\right) t$
        $=\left(v^{{\prime}} \cos \theta+v_{{0}}\right) \frac{{d}}{{v^{{\prime}} \sin \theta}}=(\cot \theta+\eta \operatorname{{cosec}} \theta) d$
        For $x_{{\min }}$ (minimum drifting)
        $\begin{{aligned}} \frac{{d}}{{d \theta}}(\cot \theta+\eta \operatorname{{cosec}} \theta) &=0, \text {{ which yields }} \\ \cos \theta &=-\frac{{1}}{{\eta}}=-\frac{{1}}{{2}}  \end{{aligned}}$
        Hence, $\theta =120^{{\circ}}$
        """
        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/
    
    