from datetime import (
    datetime,
    timedelta,
)

from boss_api.shared.response import UserDto
from helper import remove_accents
from typing import Optional


class UserUpdateData(object):

    def __init__(
        self,               # type: UserUpdateData
        user_id,            # type: int
        long_name,          # type: str
        password,           # type: str
        nfc_id_code,        # type: str
        level,              # type: int
        start_date,         # type: str
        end_date,           # type: str
        status=None,        # type: Optional[int]
    ):
        # type: (...) -> None

        self.user_id = user_id
        self.long_name = long_name
        self.password = password
        self.nfc_id_code = nfc_id_code
        self.level = level
        self.start_date = start_date
        self.end_date = end_date
        self.status = status

    def fill_user_status(self):
        start = datetime.strptime(self.start_date, "%Y-%m-%d")
        now = datetime.now()
        if self.end_date is not None:
            end = datetime.strptime(self.end_date, "%Y-%m-%d")
        else:
            one_day = timedelta(days=1)
            end = now + one_day

        active = False
        if start <= now <= end or int(self.level) >= 30:
            active = True

        self.status = "0" if active else "1"

    def to_boss_user_dto(
        self,   # type: UserUpdateData
    ):
        # type: (...) -> UserDto

        return UserDto(
            user_id=self.user_id,
            user_name=str(self.user_id),
            long_name=remove_accents(text=self.long_name),
            password=self.password,
            level=self.level,
            status=self.status,
            admission_date=self.start_date,
            termination_date=self.end_date,
            nfc_uuid=self.nfc_id_code,
        )
