'''
Created on 16-Apr-2021

@author: rishi_priyansh

'''
from sympy import symbols, Eq, solve, Symbol, latex, sqrt, UnevaluatedExpr
from xv.km.ui import SvgHelper  
import matplotlib.pyplot as plt

import random
import inspect
import traceback

from ..helper import BaseHelper

from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate


# StringTheoryManager
# string_theory

class StringTheoryManager(BaseManager):
    '''
    StringTheoryManager
    Example:
        ke = StringTheoryManager()
        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(StringTheoryManager, cls).__new__(cls, *args, **kwargs)
        for base_class in StringTheoryManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.base_helper = BaseHelper(**kwargs)
        self.rtq_builder = self._init_rich_text_question_builder()
        pass
    
    def _init_rich_text_question_builder(self):
        from xv.km import RichTextQuestionBuilder
        rich_text_file_path = "physics/string-theory.txt"   
        glossary_type = 'physics' 
        glossary_file_path = "physics/glossary_string_theory.csv"
        rtq_builder = RichTextQuestionBuilder(rich_text_file_path = rich_text_file_path,
                                              glossary_type = glossary_type,
                                              glossary_file_path = glossary_file_path,
                                              verbose = self.verbose)

        if self.verbose:
            print(f""" rtq_builder: \n{rtq_builder}
            """)
        return rtq_builder
        pass


    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
            '_problem_introduction',
            '_problem_random_question_from_rich_text',
            '_problem_random_saq_question',
            '_problem_random_mcq_question',
            ])
        pass
    
    def _problem_introduction(self, *args, **kwargs):
        """

        doc_style: xv_doc
        """
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        problem_s = f"""
        Introduction to Planck Length
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        video_links = []
        video_links.append([
            "https://www.youtube.com/watch?v=DpFlqcMwh2U", 
            "What is reality? String theory & multiverse visualized"
            ])

        video_links_str = []
        for video_link, video_label in video_links:
            if not video_link:
                continue
            anchor = rf"""<li><a target = "_blank" href = "{video_link}">{video_label}</a></li>"""
            anchor = anchor.replace("\n", "")
            video_links_str.append(anchor)
            pass
        
        video_links_str = "".join(video_links_str)
        video_links_str = f"<ol>{video_links_str}</ol>"
        
        soln_s = f"""
        Watch videos: 
        {video_links_str}
        """
        
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        Watch videos: 
        {video_links_str}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

    def _problem_random_question_from_rich_text(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        result = self.rtq_builder.get_random_question(*args, **kwargs)

        problem_s = f"""
        {result['problem_s']}
        """
        problemTemplate.add_problem(problem_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



    def _problem_random_saq_question(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        ar_helper = self.base_helper.get_string_theory_helper()
        result = ar_helper.random_saq_question(**kwargs)

        problem_s = f"""
        {result['question']}
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        <b>Question.</b> {result['question']}

        <b>Answer.</b> {result['answer']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {result['answer']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate


    def _problem_random_mcq_question(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        ar_helper = self.base_helper.get_string_theory_helper()
        result = ar_helper.random_mcq_question(**kwargs)

        problem_s = f"""
        {result['question']}
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        <b>Question.</b> {result['question']}

        <b>Answer.</b> {result['answer']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {result['answer']}
        """
        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

        