import importlib
import matplotlib.pyplot as plt
from datetime import datetime
from threading import Thread, Lock

from loader import dmt

printnoln = lambda text: print(text, end='', flush=True)
rprint = lambda text: print('\r' + text)

DAYS_NEW_USER = 7
IMAGE_MAGICK = "magick"
CHANGE_DATE = datetime.fromisoformat("2018-08-21T21:00:00")
FIG_SIZE = (8,6)
# FIG_LAYOUT =


def calc_intervals(posts, months=3):
    firstpost = dmt(posts).reduce(lambda acc, e: acc if acc < e['CreationDate'] else e['CreationDate'], lambda acc, e: acc if acc < e else e, lambda: posts[0]['CreationDate'], "firstpost").getresults()
    lastpost = dmt(posts).reduce(lambda acc, e: acc if acc > e['CreationDate'] else e['CreationDate'], lambda acc, e: acc if acc > e else e, lambda: posts[0]['CreationDate'], "lastpost").getresults()

    # calc in months intervals from change date
    f = CHANGE_DATE.replace(month=CHANGE_DATE.month)
    while firstpost < f:
        f = f.replace(year=f.year - (1 if f.month - months < 1 else 0))
        f = f.replace(month=(f.month - months + 12 - 1) % 12 + 1)
    firstpost = f

    # calc in months intervals from change date
    l = CHANGE_DATE.replace(month=CHANGE_DATE.month)
    while lastpost > l:
        l = l.replace(year=l.year + (1 if l.month + months > 12 else 0))
        l = l.replace(month=(l.month + months - 1) % 12 + 1)
    lastpost = l

    # firstpost = firstpost.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
    # if (firstpost.month - 1) % months != 0:
    #     firstpost = firstpost.replace(month=firstpost.month - ((firstpost.month - 1) % months))
    # lastpost = lastpost.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
    # if (lastpost.month - 1) % months != 0:
    #     lastpost = lastpost.replace(month=lastpost.month - ((lastpost.month - 1) % months))
    # # add 3 months to last post
    # if lastpost.month + months > 12:
    #     lastpost = lastpost.replace(month=lastpost.month + months - 12, year=lastpost.year + 1)
    # else:
    #     lastpost = lastpost.replace(month=lastpost.month + months)

    cdate = firstpost
    intervals = []
    while cdate < lastpost:
        nextmon = cdate.month + months
        nextquarter = cdate.replace(month=nextmon if nextmon <= 12 else nextmon - 12, year=cdate.year + (0 if nextmon <= 12 else 1))
        if cdate > firstpost and nextquarter < lastpost: # ignore first and last intervals as there is only partial data
            print("adding interval: " + cdate.strftime("%d-%m-%Y") + " - " + nextquarter.strftime("%d-%m-%Y"))
            intervals.append((cdate, nextquarter))
        cdate = nextquarter
    return intervals


def difftime(date):
    diff = (date.year - CHANGE_DATE.year) * 12
    diff += ((date.month - CHANGE_DATE.month) % 12)
    if date.month - CHANGE_DATE.month < 0:
        diff -= 12
    return diff

# print(str(difftime(datetime.fromisoformat("2018-11-21T21:00:00"))) + ", 3")
# print(str(difftime(datetime.fromisoformat("2018-05-21T21:00:00"))) + ", -3")
# print(str(difftime(datetime.fromisoformat("2019-11-21T21:00:00"))) + ", 15")
# print(str(difftime(datetime.fromisoformat("2017-05-21T21:00:00"))) + ", -15")
# print(str(difftime(datetime.fromisoformat("2020-05-21T21:00:00"))) + ", 21")
# print(str(difftime(datetime.fromisoformat("2016-11-21T21:00:00"))) + ", -21")


def imprt(file):
    spec = importlib.util.spec_from_file_location("module.name", file)
    foo = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(foo)
    return foo


# class FigSaver():
#     def __init__(self):
#         self.__lock = Lock()
#         self.__threads = []
#
#     def save(self, fig, path, **kwargs):
#         thread = Thread(target=self.__dosave, args=(fig, path, kwargs))
#         with self.__lock:
#             self.__threads.append(thread)
#         thread.start()
#
#     def __dosave(self, fig, path, kwargs):
#         fig.savefig(path, **kwargs)
#         plt.close(fig)
#
#     def join(self):
#         with self.__lock:
#             for thread in self.__threads:
#                 thread.join()
