from typing import Optional

from pydantic import BaseModel

from src.model.db.ultmarc_inst_info import UltmarcInstInfoDBModel
from src.model.db.ultmarc_inst_trt_course import UltmarcInstTrtCourseDBModel
from src.system_var import environment


class UltmarcInstInfoViewModel(BaseModel):
    subtitle: str = '施設情報'
    is_batch_processing: Optional[bool]
    inst_info_data: Optional[UltmarcInstInfoDBModel]
    inst_trt_coursed_data: Optional[list[UltmarcInstTrtCourseDBModel]]
    doctor_wrkplace_count: Optional[int]
    inst_id: Optional[str]
    post_cnt: Optional[int]
    page_num: Optional[int]

    # 未確認
    def is_checked_unconf_flg(self):
        return 'checked' if self.inst_info_data.unconf_flg == '1' else ''

    # 休院店
    def is_checked_close_flg(self):
        return 'checked' if self.inst_info_data.close_flg == '1' else ''

    # 開業
    def is_checked_estab_sche_flg(self):
        return 'checked' if self.inst_info_data.estab_sche_flg == '1' else ''

    # 住所不明
    def is_checked_addr_unknown_reason_cd(self):
        return 'checked' if self.inst_info_data.addr_unknown_reason_cd else ''

    # 開業
    def is_checked_phone_number_non_flg(self):
        return 'checked' if self.inst_info_data.phone_number_non_flg == '1' else ''

    # 再審査区分
    def is_checked_re_exam_cd(self):
        return 'checked' if self.inst_info_data.re_exam_cd else ''

    # 病棟閉鎖　　
    def is_checked_ward_abolish_flg(self):
        return 'checked' if self.inst_info_data.ward_abolish_flg == '1' else ''

    # 一部病棟閉鎖
    def is_checked_ward_abolish_flg_part(self):
        return 'checked' if self.inst_info_data.ward_abolish_flg == '2' else ''

    # 修正年月日
    def is_input_sys_update_date(self):
        sys_update_date = str(self.inst_info_data.sys_update_date)
        return sys_update_date[:10]

    # 勤務医師ボタン表示
    def is_disabled_doctor_wrkplace(self):
        return 'disabled' if self.doctor_wrkplace_count == 0 else ''

    # 現在のページ（表示用）
    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_inst_trt_course_data_size(self):
        if self.inst_trt_coursed_data is None:
            return 0
        return len(self.inst_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
