'''
Created on 16-Apr-2021

@author: rishi_priyansh

https://www.youtube.com/watch?v=r40h66qiF5I

'''
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

# _atomic_structure_manager
# AtomicStructureManager

class AtomicStructureManager(BaseManager):
    '''
    AtomicStructureManager
    Example:
        ke = AtomicStructureManager()
        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(AtomicStructureManager, cls).__new__(cls, *args, **kwargs)
        for base_class in AtomicStructureManager.__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/nuclear_physics_short_intro.txt"   
        glossary_type = 'physics' 
        glossary_file_path = "physics/physics_dict_terms.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_fill_in_the_blanks_question',
            '_problem_interchanged_words',
            '_problem_put_letters_in_order',
            '_problem_put_sentence_in_order',
            '_problem_put_paragraph_in_order',
            '_problem_insert_a_sentence',
            
            ])
        pass

    def _problem_fill_in_the_blanks_question(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        result = self.rtq_builder.get_question_fill_in_the_blanks(**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_interchanged_words(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        result = self.rtq_builder.get_question_interchanged_words(**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_put_letters_in_order(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        result = self.rtq_builder.get_question_put_letters_in_order(**kwargs)
        if result is None:
            problemTemplate.add_problem(f"""
            Error while getting a word
            """.strip(), "text/html")
            return problemTemplate


        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_put_sentence_in_order(self, *args, **kwargs):
        '''
        text = None
        sentence_delimiter = "\n"
        '''
        result = self.rtq_builder.get_question_put_sentence_in_order(**kwargs)
            
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        if result is None:
            problemTemplate.add_problem(f"""
            Error while getting a sentence
            """.strip(), "text/html")
            return problemTemplate

        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_put_paragraph_in_order(self, *args, **kwargs):
        result = self.rtq_builder.get_question_put_paragraph_in_order(**kwargs)

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        if result is None:
            problemTemplate.add_problem(f"""
            Error while getting a paragraph
            """.strip(), "text/html")
            return problemTemplate

        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_insert_a_sentence(self, *args, **kwargs):

        result = self.rtq_builder.get_question_insert_a_sentence(**kwargs)

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        if result is None:
            problemTemplate.add_problem(f"""
            Error while getting a paragraph
            """.strip(), "text/html")
            return problemTemplate

        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 _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

    