from datetime import datetime
from typing import Optional

from pydantic import BaseModel

from src.model.db.ultmarc_doctor_info import UltmarcDoctorInfoDBModel
from src.model.db.ultmarc_doctor_wrkplace import UltmarcDoctorWrkplaceDBModel
from src.model.db.ultmarc_doctor_wrkplace_his import \
    UltmarcDoctorWrkplaceHisDBModel
from src.model.db.ultmarc_sosiety import UltmarcSosietyDBModel
from src.model.db.ultmarc_specialist_license import \
    UltmarcSpecialistLicenseDBModel
from src.model.db.ultmarc_trt_course import UltmarcDrTrtCourseDBModel
from src.system_var import constants, environment


class UltmarcDoctorInfoViewModel(BaseModel):
    subtitle: str = '医師情報'
    is_batch_processing: bool = None
    doctor_info_data: UltmarcDoctorInfoDBModel = None
    trt_coursed_data: Optional[list[UltmarcDrTrtCourseDBModel]] = None
    sosiety_data: Optional[list[UltmarcSosietyDBModel]] = None
    specialist_license_data: Optional[list[UltmarcSpecialistLicenseDBModel]] = None
    doctor_wrkplace_data: Optional[list[UltmarcDoctorWrkplaceDBModel]] = None
    doctor_wrkplace_his_data: Optional[list[UltmarcDoctorWrkplaceHisDBModel]] = None
    doc_id: str = None
    post_cnt: int = None
    page_num: int = None

    # 現在のページ（表示用）
    def is_page_num_view(self):
        return self.page_num + 1

    # 前ボタン
    def is_disabled_prev(self):
        return 'disabled' if self.page_num == 0 else ''

    # 次ボタン
    def is_disabled_next(self):
        if self.page_num == self.post_cnt - 1:
            return 'disabled'
        return ''

    # 生年月日
    def is_input_birthday_format(self):
        if self.doctor_info_data.birthday is None or len(self.doctor_info_data.birthday) == 0:
            return ''
        birthday = self.doctor_info_data.birthday.replace(" ", "")
        if len(birthday) == 8:
            # ８桁の場合、yyyy/mm/ddとする
            return f'{birthday[0:4]}/{birthday[4:6]}/{birthday[6:8]}'
        if len(birthday) == 4:
            # ４桁の場合、mm/ddとする
            return f'{birthday[0:2]}/{birthday[2:4]}'
        # 規格外の場合は、空表示とする
        return ''

    # 利用停止区分

    def is_input_use_stop_div_category_name(self):
        if not self.doctor_info_data.use_stop_div:
            return ''
        return constants.DISPLAY_USER_STOP_DIV.get(self.doctor_info_data.use_stop_div, '')

    # 開始年月日
    def is_input_aply_start_ymd_format(self, aply_start_date: datetime):
        if not aply_start_date:
            return ''
        return self._format_date(aply_start_date)

    # 医師勤務先履歴_開始年月日
    def is_input_his_aply_start_ymd_format(self, aply_start_date_string: str):
        if not aply_start_date_string:
            return ''
        try:
            # 医師勤務先履歴の適用開始年月日は文字列型なので、日付に変換してから渡す
            aply_start_date = datetime.strptime(aply_start_date_string, '%Y%m%d')
        except Exception:
            return aply_start_date_string
        return self._format_date(aply_start_date)

    # 医師勤務先履歴_終了年月日
    def is_input_his_aply_end_ymd_format(self, aply_end_date_string: str):
        if not aply_end_date_string:
            return ''
        try:
            # 医師勤務先履歴の適用開始年月日は文字列型なので、日付に変換してから渡す
            aply_end_date = datetime.strptime(aply_end_date_string, '%Y%m%d')
        except Exception:
            return aply_end_date_string
        return self._format_date(aply_end_date)

    # DM不可フラグ
    def is_input_notdm_flg_name(self, notdm_flg: str):
        if not notdm_flg:
            return ''
        if notdm_flg == '1':
            return '不可'

    def is_input_trt_course_data_size(self):
        return len(self.trt_coursed_data)

    def disabled_button(self):
        return 'disabled' if self.is_data_empty() or self.is_data_overflow_max_length() else ''

    def is_form_submitted(self):
        return self.form_data is not None

    def is_data_empty(self):
        return len(self.doctor_data) == 0

    def is_data_overflow_max_length(self):
        return len(self.doctor_data) > environment.ULTMARC_SEARCH_RESULT_MAX_COUNT

    def _format_date_string(self, date_string: str) -> str:
        if date_string is None or date_string == '':
            return ''
        date_str = datetime.strptime(date_string, '%Y%m%d')
        return date_str.strftime('%Y/%m/%d')

    def _format_date(self, date_time: datetime) -> str:
        if date_time is None:
            return ''
        return date_time.strftime('%Y/%m/%d')
