'''
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
from os.path import os
        
#_agtp_mechanics

class   AgtpMechanics(BaseManager):
    '''
    AgtpMechanics
    Example:
        ke = AgtpMechanics()
        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(AgtpMechanics, cls).__new__(cls, *args, **kwargs)
        for base_class in AgtpMechanics.__bases__:
            base_class.__init__(instance)
        return instance
    
    def __init__(self, *args, **kwargs):
        pass
    
    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
            '_problem_1_1',
            '_problem_2_1',
            
            ])
        pass
            
    def _problem_1_1(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        A chain of mass M and length L is suspended vertically with its lower end
        touching a scale. The chain is released and falls onto the scale. What is
        the reading of the scale when a length of the chain has fallen? Neglect
        the size of the individual links.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = r"""
        The reading of the scale consists of two parts: the weight of the chain accumulated and the impulse per unit time imparted by the chain colliding with the scale. The first part is clearly $W_{1}=M g x / L$. The velocity of the links at the instant of hitting the scale is found from $v^{2}=2 g x$. The second component of the force exerted on the scale equals
        $$
        \frac{d p}{d t}=\frac{d(m v)}{d t}=v \frac{d m}{d t}=v \frac{M}{L} v=2 M g \frac{x}{L}
        $$
        The total force on the scale during the fall of the chain is therefore
        $$
        M g \frac{x}{L}+2 M g \frac{x}{L}=3 M g \frac{x}{L}
        $$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = fr"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    
    def _init_file_path(self, file_name):
        import pathlib
        from xv.util import locatePath
        rootFolder = pathlib.Path(__file__).parent.absolute()
        file_path = locatePath(file_name, 
                               parent_depth = 6,
                               rootFolder = rootFolder,
                               part_match = False,
                               search_type = 'file',
                               verbose = self.verbose,
                               )
        return os.path.join(rootFolder, file_path)
    
            
    def _problem_2_1(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        file_name = 'agtp_1_2.jpg'
        file_path = self._init_file_path(file_name)
        
        problemTemplate.add_answer(file_path, 'image/jpg')
        
        problem_s = fr"""
        
        {file_path}
        A rope is wrapped around a fixed cylinder as shown in Figure P. 1.2. There
        is friction between the rope and the cylinder, with a coefficient of friction
        $\mu ;$ the angle $\theta_{0}=\pi / 3$ defines the arc of the cylinder covered by the rope. The rope is much thinner than the cylinder. A cat is pulling on one end of the rope with a force $F$ while 10 mice can just barely prevent it from sliding by applying a total force $f=F / 10$.
        a) Does the minimum force necessary to prevent the rope from sliding depend on the diameter of the cylinder?
        b) Through what minimum angle $\theta_{1}$ about the cylinder should one mouse wrap the rope in order to prevent the cat from winning the game of tug of war?
        
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = r"""
        
a) The straightforward solution is given in (b). However, to answer the first part, we can use dimensional analysis. The force $f$ applied by the mice may depend only on the dimensionless coefficient of friction $\mu$, the cylinder diameter $d$, and the force $F$ :
$$
f \propto G(\mu) d^{\alpha} F^{\beta}
$$
where $G(\mu)$ is some function of $\mu$. It is obvious that it is impossible to satisfy this equation unless $\alpha=0$, so the force $f$ does not depend on the diameter of the cylinder.



b) Consider an element of rope between angles $\theta$ and $\theta+d \theta$. The difference in tension $d T$ between its ends equals $d T=\mu N=\mu T d \theta$, where $N$ is the normal force (see Figure S.1.2). This equation has a solution: $T=$ const $\cdot e^{\mu \theta}$. At $\theta=0, T=f$, and at $\theta_{0}=\pi / 3, T=F$, so
$$
F=f e^{\mu \theta_{0}}
$$
From equation (S.1.2.1) we obtain the same result as determined in part (a), i.e., the force does not depend on the diameter of the cylinder. Similarly, if we have just one mouse, the minimum angle $\theta_{1}$ of wrapping is found from
$$
F=f^{\prime} e^{\mu \theta_{1}}
$$
where $f^{\prime}=f / 10=F / 100$ is the force necessary for one mouse to keep the rope from slipping. From equation (S.1.2.1), we can also find the coefficient of friction $\mu$ :
$$
\mu=\frac{1}{\theta_{0}} \ln \left(\frac{F}{f}\right)
$$
Then, from equation (S.1.2.2),
$$
\theta_{1}=\frac{1}{\mu} \ln \left(\frac{F}{f^{\prime}}\right)=\theta_{0} \frac{\ln \left(F / f^{\prime}\right)}{\ln (F / f)}=\theta_{0} \log _{F / f}\left(\frac{F}{f^{\prime}}\right)=\theta_{0} \log _{10} 100=2 \theta_{0}=\frac{2 \pi}{3}
$$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = fr"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    
    