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 environment


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

    # 現在のページ（表示用）
    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):
        return self._format_date_string(self.doctor_info_data.birthday)

    # 開始年月日
    def is_input_aply_start_ymd_format(self, date_string):
        if date_string:
            return self._format_date(date_string)
        else:
            return ''

    def is_input_his_aply_start_ymd_format(self, date_string):
        if date_string:
            return self._format_date(date_string)
        else:
            return ''

    # 終了年月日
    def is_input_his_aply_end_ymd_format(self, date_string):
        if date_string:
            return self._format_date(date_string)
        else:
            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):
        if date_string is None:
            return ''
        date_str = datetime.strptime(date_string, '%Y%m%d')
        return date_str.strftime('%Y/%m/%d')

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