from icalendar import Calendar, Event
from datetime import datetime
import os

for_confluence = False


def create_event(summary, reduced, date, uid):
    event = Event()
    event["uid"] = str(uid)
    if reduced.lower() == "office closed":
        event["dtstart;value=date"] = "".join(date.split("-"))
    elif reduced.lower() == "reduced 3 hours":
        if for_confluence:
            t1 = 12
        else:
            t1 = 14
        event["dtstart"] = "".join(date.split("-")) + "T{}0000Z".format(t1)
        event["dtend"] = "".join(date.split("-")) + "T{}0000Z".format(t1 + 3)
    else:
        event["dtstart"] = "".join(date.split("-"))
    event["dtstamp"] = datetime.now().strftime("%Y%m%dT%H%M%SZ")
    event["description"] = ", ".join([summary, reduced])
    event["summary"] = summary
    event["X-MICROSOFT-CDO-BUSYSTATUS"] = "OOF"
    return event


# tz=Timezone()
# tz.add('tzid', 'Europe/Stockholm')

calendar_swe_all = Calendar()
calendar_swe_all.add("version", "2.0")
calendar_swe_all.add("prodid", "Yngve's Superscript")
calendar_eng_all = calendar_swe_all.copy()

# calendar_swe_all.add_component(tz)
# calendar_eng_all.add_component(tz)

i_swe_all = 1
i_eng_all = 1

for f in os.listdir("."):
    if f.split("_")[0] == "list":
        year = int(f.split(".")[0].split("_")[1])
        print(f, year)

        calendar_swe = Calendar()
        calendar_swe.add("version", "2.0")
        calendar_swe.add("prodid", "Yngve's Superscript")
        calendar_swe.add("X-WR-TIMEZONE", "Europe/Stockholm")
        calendar_eng = calendar_swe.copy()

        # calendar_swe.add_component(tz)
        # calendar_eng.add_component(tz)

        i_swe = 1
        i_eng = 1

        for l in open(f, "r", encoding="utf8"):
            if l.strip()[0] == "#":
                continue
            # encoding issues with swedish:
            lsp = l.split("  ")

            date = lsp[0].strip()
            desc_eng = lsp[1].split("/")[0].strip()
            desc_swe = lsp[1].split("/")[1].strip()
            reduction = lsp[-1].strip()

            event_swe = create_event(desc_swe, reduction, date, i_swe)
            i_swe += 1
            calendar_swe.add_component(event_swe)

            event_eng = create_event(desc_eng, reduction, date, i_eng)
            i_eng += 1
            calendar_eng.add_component(event_eng)

            if year > 2016:
                event_swe_all = create_event(desc_swe, reduction, date, i_swe_all)
                i_swe_all += 1
                calendar_swe_all.add_component(event_swe_all)

                event_eng_all = create_event(desc_eng, reduction, date, i_eng_all)
                i_eng_all += 1
                calendar_eng_all.add_component(event_eng_all)

        open("vacations_swe_{}.ics".format(year), "wb").write(calendar_swe.to_ical())
        open("vacations_eng_{}.ics".format(year), "wb").write(calendar_eng.to_ical())
open("vacations_swe.ics", "wb").write(calendar_swe_all.to_ical())
open("vacations_eng.ics", "wb").write(calendar_eng_all.to_ical())