from datetime import datetime, date


class TzDateTime:
    """
    The format of date - 'dd.mm'
    The format of time - 'hh:mm'
    """

    def __init__(self, day, month, hours=None, minutes=None):
        self.day = day
        self.month = month
        self.year = None  # need to be resolved using resolve method.
        self.hours = hours
        self.minutes = minutes

    @staticmethod
    def read_from_str(date_str, time_str):
        returned_tz_time = TzDateTime(day=date_str[0:2],
                                      month=date_str[3:5],
                                      hours=time_str[0:2],
                                      minutes=time_str[3:5])

        returned_tz_time.validate_params()

    def validate_params(self):
        # year is currently unknown, using 2020 as placeholder, to cover leap years too.
        # If no exception, datetime is valid.
        dt = datetime(2020, int(self.month), int(self.day), int(self.hours), int(self.minutes))

    @staticmethod
    def resolve_year_for_dates(from_date, to_date):
        """
        The DST information in TimeZone.dat contains only month and day.
        The json contains also the year.
        This method calculates the year based on from_date, to_date and today.
        """
        today = date.today()

        # To use date capabilities for month and day comparison, we use current year for object creation.
        # Real year is calculated later.
        from_d = date(today.year, int(from_date.month), int(from_date.day))
        to_d = date(today.year, int(to_date.month), int(to_date.day))

        if from_d < to_d:
            # It is in the same year. Example 03/06/2023 - 15/11/2023
            if today < to_d:
                # The DST didn't end for this year. Using current year.
                from_date.year = str(today.year)
                to_date.year = str(today.year)
            else:
                # DST ended for current year. User might have been configured it for next year.
                from_date.year = str(today.year + 1)
                to_date.year = str(today.year + 1)
        else:
            # from_d is after to_d. It means there is DST during year change. Example  15/11/2022 - 15/06/2023
            if today < to_d:
                # We are still in DST. from_date is in previous year, to_date is in current year.
                from_date.year = str(today.year - 1)
                to_date.year = str(today.year)
            else:
                # DST has started this year or going to start. to_date will be next year.
                from_date.year = str(today.year)
                to_date.year = str(today.year + 1)
