'''
Created on 12-Jul-2021

@author: bthakur
'''
import random
import inspect
import traceback
import re
import operator
import datetime
import calendar

from sympy import (symbols, latex, simplify, Eq,
                   pi,
                   sin, cos, tan,
                   csc, sec, cot
                   )
from sympy.abc import theta
import sympy

from xv.km.util import latex_fraction, latex_exponent, wrap_bracket

class CalendarHelper:
    def __init__(self, **kwargs):
        self.verbose = kwargs.get('verbose', False)
        self.implies = symbols(u'\U000021D2')
        self._months_in_non_leap_year = self._init_months_in_non_leap_year()
        pass

    def date_to_str(self, dt):
        return dt.strftime('%b %d, %Y')

    def get_recent_year(self, leap_year = False):
        year = datetime.datetime.now().year
        if not leap_year:
            return year
        
        while True:
            if calendar.isleap(year):
                return year
            year = year - 1
        pass

    def get_month_names(self):
        return list(self._months_in_non_leap_year.keys())

    def get_weekday_names(self):
        return list(calendar.day_name)

    def get_random_date(self, year_range = 20, month_range = 12):
        now = datetime.datetime.now()
        year1 = now.year + random.randint(1, year_range) * random.choice([-1, 1, 0])
        month1 = random.randint(1, month_range)
        day1 = random.choice(list(calendar.monthrange(year1, month1))) 
        d1 = datetime.date(year1, month1, day1)
        return d1
        pass
    
    

    def is_leap_year(self, year):
        return calendar.isleap(year)
        pass

    def is_leap_year_logic(self, year):
        if year % 100 == 0:
            if year % 400 == 0:
                return f"{year} is a century year and divisible by 400"
            return f"{year} is a century year and not divisible by 400"
        if year % 4 == 0:
            return f"{year} is a non-century year and divisible by 4"
        return f"{year} is a non-century year and not divisible by 4"
        pass

    def get_months(self, 
                   leap_year = False, 
                   days = None, 
                   op = operator.eq):
        
        months = self._months_in_non_leap_year.copy()
        if leap_year:
            months['February'] = 29
        
        if not days is None:
            months = {k:v for k, v in months.items() if op(v, days) }   
        
        return months
        pass
    
    def _init_months_in_non_leap_year(self):
        return {'January': 31,
                  'February': 28,
                  'March': 31,
                  'April': 30,
                  'May': 31,
                  'June': 30,
                  'July': 31,
                  'August': 31,
                  'September': 30,
                  'October': 31,
                  'November': 30,
                  'December': 31,
                  }
        pass
        
    pass
#class CalendarHelper:
