import json
from typing import Optional

from pydantic import BaseModel

from src.model.db.inst_div_master import InstDivMasterModel
from src.model.db.prefc_master import PrefcMasterModel
from src.model.db.ultmarc_inst import UltmarcInstDBModel
from src.model.request.ultmarc_inst import UltmarcInstSearchModel
from src.system_var import environment


class UltmarcInstViewModel(BaseModel):
    subtitle: str = '施設検索一覧'
    is_batch_processing: Optional[bool]
    prefc_models: list[PrefcMasterModel]
    inst_div_models: list[InstDivMasterModel]
    inst_data: Optional[list[UltmarcInstDBModel]] = []
    form_data: Optional[UltmarcInstSearchModel]

    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.inst_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

    # ULT施設コード
    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_selected_inst_div_cd(self, selected_inst_div_cd):
        if not self.is_form_submitted():
            return ''
        return self._selected_value(self.form_data.inst_div_cd, selected_inst_div_cd)

    def is_input_form_inst_div_cd(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.inst_div_cd or ''

    # ULT施設名(漢字)
    def is_input_form_inst_name_kanji(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.form_inst_name_kanji or ''

    # ULT施設名(かな・カナ)
    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_input_postal_number(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.postal_number or ''

    # 電話番号
    def is_input_inst_phone_number(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.inst_phone_number or ''

    # 削除施設表示
    def is_checked_delFlg(self):
        if not self.is_form_submitted():
            return 'checked'
        return self._checked_value(self.form_data.delFlg)

    def is_input_delFlg(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.delFlg or ''

    # ULT施設住所
    def is_input_inst_addr(self):
        if not self.is_form_submitted():
            return ''
        return self.form_data.inst_addr 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 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.inst_data) == 0

    def is_data_overflow_max_length(self):
        return len(self.inst_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 ''

    def _checked_value(self, form_value: str):
        return 'checked' if form_value else ''
