import json
from typing import Optional

from pydantic import BaseModel

from src.model.db.prefc_master import PrefcMasterModel
from src.model.db.ultmarc_doctor import UltmarcDoctorDBModel
from src.model.request.ultmarc_doctor import UltmarcDoctorSearchModel
from src.system_var import environment


class UltmarcDoctorViewModel(BaseModel):
    subtitle: str = '医師検索一覧'
    is_batch_processing: Optional[bool]
    prefc_models: list[PrefcMasterModel]
    doctor_data: Optional[list[UltmarcDoctorDBModel]] = []
    form_data: Optional[UltmarcDoctorSearchModel]

    def ultmarc_data_json_str(self):
        def date_handler(obj):
            return obj.isoformat() if hasattr(obj, 'isoformat') else obj
        return json.dumps([model.dict() for model in self.doctor_data], ensure_ascii=False, default=date_handler)

    # ページネーションのページ番号
    # 検索時は最初のページを表示する
    # 詳細画面からの遷移時は、元のページを表示する
    def init_pagination_page_number(self):
        if not self.is_form_submitted():
            return 1
        return self.form_data.pagination_page_number or 1

    # 医師コード
    def is_input_dcf_pcf_dr_cd(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.dcf_pcf_dr_cd or ''

    # 氏名(漢字)
    def is_input_dr_name(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.dr_name or ''

    # 氏名(かな・カナ)
    def is_input_dr_name_kana(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.dr_name_kana or ''

    # 勤務先コード
    def is_input_dcf_dsf_inst_cd(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.dcf_dsf_inst_cd or ''

    # 勤務先名(漢字)
    def is_input_form_inst_name_kanji(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.form_inst_name_kanji or ''

    # 勤務先名(かな・カナ)
    def is_input_form_inst_name_kana(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.form_inst_name_kana or ''

    # 勤務先都道府県
    def is_selected_prefc_cd(self, selected_prefc_cd):
        if not self.is_form_submitted():
            return ''
        return self._selected_value(self.form_data.prefc_cd, selected_prefc_cd)

    def is_input_form_prefc_cd(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.prefc_cd or ''

    # 所属部科(漢字)
    def is_input_blng_sec_name(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.blng_sec_name or ''

    # 診療科目(漢字)
    def is_input_trt_course_name(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.trt_course_name or ''

    # 出身大学(漢字)
    def is_input_alma(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.alma or ''

    # 卒年
    def is_input_grad_y(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.grad_y or ''

    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 _selected_value(self, form_value: str, current_value: str):
        return 'selected' if form_value == current_value else ''
