import os
from posixpath import dirname
import pandas as pd
from datetime import datetime
import calendar
from os.path import join
import time
from enum import Enum


class Klinika(Enum):
    KIRURGJI = 'kirurgji'
    OBS = 'OBS-GYN'
    URGJENCA = 'urgjenca'


klinika = Klinika.OBS.value

xlsx_path = './Excel/'
excel_path = join(xlsx_path, f"raw_{klinika}_2016-2019.xlsx")

df = pd.read_excel(excel_path, sheet_name='Sheet1')
df = df.reset_index()
df['MOSHA'] = [None] * df.index.stop
df['ERROR'] = [None] * df.index.stop
i = 0
datat_pattern = "%d/%m/%y"
data_shtri_korigj = []
datelindja_korigj = []


def llogarit_moshen(datelindja: datetime, data_shtri: datetime):
    return data_shtri.year - datelindja.year - ((data_shtri.month, data_shtri.day) < (datelindja.month, datelindja.day))


def konverto_data(datat: str, i: int):
    # i +=1
    datat_pattern = "%d/%m/%y"
    try:
        if "2572/81" in datat or datat == "?" or datat == "nan" or datat == "Pensionist" or datat == "15/13/92" or datat == "29/2/94" or datat == "20/0/81" or "Colicaabdpd" in datat or "2379/11" in datat or "2710/44" in datat or "31/4/56" in datat or "11/1/199" in datat or "27/7/889" in datat or "31/9/84" in datat:
            df.iat[i, df.columns.get_loc('MOSHA')] = "ERROR"
            df.iat[i, df.columns.get_loc('ERROR')] = datat
            return ""
        if "vj" in datat:
            datat = datat.replace("vjec", "")
            datat = datat.replace("vj", "")
            df.iat[i, df.columns.get_loc('MOSHA')] = int(datat)
            return "DEKLARUAR"
        if "muaj" in datat:
            datat = datat.replace("muaj", "")
            datat = int(datat) / 12
            df.iat[i, df.columns.get_loc('MOSHA')] = float(datat)
            return "DEKLARUAR"
        if datat.endswith('/'):
            datat = datat[:-1]
        datat = datat.split('/')
        if len(datat) == 1:
            datat.insert(0, '1')
            datat.insert(1, '1')
        if datat[0] == "0":
            datat[0] = "1"
        if len(datat) < 3 and len(datat) > 1:
            datat.insert(0, '1')
        if (len(datat)) > 2:
            if datat[2] in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
                datat[2] = f"0{datat[2]}"
            try:
                if int(datat[2]) > 99:
                    datat_pattern = "%d/%m/%Y"
                # Detyrohem te bej nje supozim qe nuk ka datelindje 1919 e poshte, bazuar mbi datat e shtrimit qe jane max 2019
                elif int(datat[2]) > 19 and int(datat[2]) < 100:
                    datat[2] = f"19{datat[2]}"
                    datat_pattern = "%d/%m/%Y"
                else:
                    datat_pattern = "%d/%m/%y"
            except:
                print(i, datat)
        datat = "/".join(datat)
        try:
            datat = datetime.strptime(datat, datat_pattern)
            return datat
        except ValueError as e:
            print(f"{i}: {e} -- {datat}")
            return None

    except TypeError as e:
        print(f"{i}: {e} -- {datat}")
        return None


def pastro_string(datat: str, i: int):
    datat = str(datat)
    datat = datat.strip()
    datat = datat.replace(" ", "")
    datat = datat.replace(".", "/")
    datat = datat.replace("'", "")

    datat = konverto_data(datat, i)
    return datat


for index, row in df.iterrows():
    try:
        data_shtri = row['DATA E SHTRIMIT']
    except KeyError as e:
        data_shtri = row['DATA']
    datelindja = row['DATELINDJA']
    emer = row['EMER']
    data_shtri = pastro_string(data_shtri, i)
    datelindja = pastro_string(datelindja, i)

    if data_shtri is None or datelindja is None:
        print(f"{i}: {data_shtri} --{emer}-- {datelindja}")
        break
    if type(data_shtri) is datetime and type(datelindja) is datetime:
        mosha = llogarit_moshen(datelindja, data_shtri)
        if mosha == 0:
            df.iat[i, df.columns.get_loc('MOSHA')] = "ERROR"
            df.iat[i, df.columns.get_loc('ERROR')] = mosha
        else:
            df.iat[i, df.columns.get_loc('MOSHA')] = mosha
    data_shtri_korigj.append(data_shtri)
    datelindja_korigj.append(datelindja)
    i += 1


df.insert(len(df.columns), "DT_SHTRIMI_KORIGJ", data_shtri_korigj)
df.insert(len(df.columns), "DATELINDJA_KORIGJ", datelindja_korigj)

df.drop(columns=df.columns[0], axis=1, inplace=True)
df.drop(columns=df.columns[0], axis=1, inplace=True)

cols = df.columns.tolist() 
index = cols.index('MOSHA')
del cols[index]
cols.insert(3,'MOSHA')
df = df[cols]
current_GMT = time.gmtime()
ts = calendar.timegm(current_GMT)

for dirpath, dirname, filename in os.walk(xlsx_path):
    for name in filename:
        if name.startswith(klinika):
            file_per_tu_fshire = join(xlsx_path, name)
            os.remove(file_per_tu_fshire)

excel_path = join(xlsx_path, f"{klinika}_{ts}.xlsx")
df.to_excel(excel_path, index=False)
