'''
Created on 20-Jan-2022

@author: bthakur
'''

import inspect
import traceback
import random
import sympy
from sympy import (symbols, Symbol, Pow, latex, Rational,
                   nsimplify,
                   UnevaluatedExpr as ueval)

from xv.km.util import latex_fraction
from xv.util import toIntOrFloat, list_to_str


# _long_arithmetic_process_helper
# LongArithmeticProcessHelper

from xv.util import UtilClass
from xv.util.functions._util_functions import html_table

class LongArithmeticProcessHelper(UtilClass):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        pass




    def get_problem_add_integers(self, **kwargs):
        digit_sep = " " * 4
        nums_size = random.randint(2, 4)

        nums = []
        nums_digits = []
        for i in range(nums_size):
            num = random.randint(1, 6 ** random.randint(1, 10))
            num_digits = [x for x in list(str(num))]
            nums.append(num)
            nums_digits.append(num_digits)
            pass

        nums_str = list_to_str(nums, last_sep = ' and ')
        nums_sum = sum(nums)

        max_len = max([len(str(num)) for num in nums])

        prob_s = f"""
        Find sum of {nums_str}.
        """

        soln_stmts = []

        nums_as_str = [list(str(num)) for num in nums]
        nums_resized = self.make_lists_equal_length(*nums_as_str)

        nums_resized_decorated = self.make_lists_equal_length(*nums_as_str, decorate_sep = True)
        nums_resized_html = html_table(nums_resized_decorated)

        soln_stmt = f"""
        Write each number in a separate line.
        {nums_resized_html}

        Let us start adding digits from the right side.
        """
        soln_stmts.append(soln_stmt.strip())


        for idx in range(max_len):
            temp_resized = nums_resized.copy()

            temp_resized = self.make_lists_equal_length(*temp_resized, sep = " ")
            temp_resized_html = html_table(temp_resized)

            soln_stmt = f"""
            idx = {idx}
            {temp_resized_html}
            """
            soln_stmts.append(soln_stmt.strip())
            pass

        soln_stmt = f"""
        """
        soln_stmts.append(soln_stmt.strip())



        soln_s = "\n\n".join(soln_stmts)

        answer_s = f"""
        {nums_sum}
        """
        result = {
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }

        return result
        pass


    def make_lists_equal_length(self, *lists, sep = '0', color = 'red', decorate_sep = False):

        if decorate_sep:
            sep = f"""<span style="color: {color};">{sep}</span>"""

        max_digit_count = max([len(x) for x in lists])

        mod_lists = []
        for temp_list in lists:
            if len(temp_list) == max_digit_count:
                mod_lists.append(temp_list)
                continue

            addl_list = []
            for _ in range(max_digit_count - len(temp_list)):
                addl_list.append(sep)

            addl_list.extend(temp_list)
            mod_lists.append(addl_list)
            pass
        return mod_lists
        pass

    pass
#class LongArithmeticProcessHelper:
