from typing import Optional

from fastapi import Form

from src.util.sanitize import sanitize

from src.model.request.request_base_model import RequestBaseModel
from src.util.string_util import is_not_empty


@sanitize
class MasterMainteCsvDlModel(RequestBaseModel):
    ta_cd: Optional[str]
    adapt_ta_cd: Optional[str]
    inst_cd: Optional[str]
    adapt_inst_cd: Optional[str]
    emp_cd: Optional[str]
    adapt_emp_cd: Optional[str]
    apply_date_from: Optional[str]
    adapt_apply_date_from: Optional[str]
    start_date_from: Optional[str]
    adapt_start_date_from: Optional[str]
    start_date_to: Optional[str]
    adapt_start_date_to: Optional[str]
    end_date_from: Optional[str]
    adapt_end_date_from: Optional[str]
    end_date_to: Optional[str]
    adapt_end_date_to: Optional[str]
    select_table: Optional[str]
    create_date_from: Optional[str]
    adapt_create_date_from: Optional[str]
    create_date_to: Optional[str]
    adapt_create_date_to: Optional[str]
    update_date_from: Optional[str]
    adapt_update_date_from: Optional[str]
    update_date_to: Optional[str]
    adapt_update_date_to: Optional[str]

    @classmethod
    def as_form(
        cls,
        ctrl_ta_cd: Optional[str] = Form(None),
        ctrl_inst_cd: Optional[str] = Form(None),
        ctrl_emp_cd: Optional[str] = Form(None),
        ctrl_apply_date_from: Optional[str] = Form(None),
        ctrl_start_date_from: Optional[str] = Form(None),
        ctrl_start_date_to: Optional[str] = Form(None),
        ctrl_end_date_from: Optional[str] = Form(None),
        ctrl_end_date_to: Optional[str] = Form(None),
        radio_select_table: Optional[str] = Form(None),
        ctrl_create_date_from: Optional[str] = Form(None),
        ctrl_create_date_to: Optional[str] = Form(None),
        ctrl_update_date_from: Optional[str] = Form(None),
        ctrl_update_date_to: Optional[str] = Form(None)
    ):
        return cls.__convert_request_param(
            cls,
            ctrl_ta_cd,
            ctrl_inst_cd,
            ctrl_emp_cd,
            ctrl_apply_date_from,
            ctrl_start_date_from,
            ctrl_start_date_to,
            ctrl_end_date_from,
            ctrl_end_date_to,
            radio_select_table,
            ctrl_create_date_from,
            ctrl_create_date_to,
            ctrl_update_date_from,
            ctrl_update_date_to
        )

    def __convert_request_param(
        cls,
        ctrl_ta_cd: str,
        ctrl_inst_cd: str,
        ctrl_emp_cd: str,
        ctrl_apply_date_from: str,
        ctrl_start_date_from: str,
        ctrl_start_date_to: str,
        ctrl_end_date_from: str,
        ctrl_end_date_to: str,
        radio_select_table: str,
        ctrl_create_date_from: str,
        ctrl_create_date_to: str,
        ctrl_update_date_from: str,
        ctrl_update_date_to: str
    ):
        ctrl_ta_cd = ctrl_ta_cd if is_not_empty(ctrl_ta_cd) else ''
        ctrl_inst_cd = ctrl_inst_cd if is_not_empty(ctrl_inst_cd) else ''
        ctrl_emp_cd = ctrl_emp_cd if is_not_empty(ctrl_emp_cd) else ''

        adapt_apply_date_from = ''
        if is_not_empty(ctrl_apply_date_from):
            adapt_apply_date_from = ctrl_apply_date_from.replace('/', '')
        else:
            ctrl_apply_date_from = ''

        adapt_start_date_from = ''
        adapt_start_date_to = ''
        if is_not_empty(ctrl_start_date_from):
            adapt_start_date_from = ctrl_start_date_from.replace('/', '')
        else:
            ctrl_start_date_from = ''
        if is_not_empty(ctrl_start_date_to):
            adapt_start_date_to = ctrl_start_date_to.replace('/', '')
        else:
            ctrl_start_date_to = ''

        adapt_end_date_from = ''
        adapt_end_date_to = ''
        if is_not_empty(ctrl_end_date_from):
            adapt_end_date_from = ctrl_end_date_from.replace('/', '')
        else:
            ctrl_end_date_from = ''
        if is_not_empty(ctrl_end_date_to):
            adapt_end_date_to = ctrl_end_date_to.replace('/', '')
        else:
            ctrl_end_date_to = ''

        adapt_create_date_from = ''
        adapt_create_date_to = ''
        if is_not_empty(ctrl_create_date_from):
            adapt_create_date_from = ctrl_create_date_from.replace('/', '-') + ' 00:00:00'
        else:
            ctrl_create_date_from = ''
        if is_not_empty(ctrl_create_date_to):
            adapt_create_date_to = ctrl_create_date_to.replace('/', '-') + ' 23:59:59'
        else:
            ctrl_create_date_to = ''

        adapt_update_date_from = ''
        adapt_update_date_to = ''
        if is_not_empty(ctrl_update_date_from):
            adapt_update_date_from = ctrl_update_date_from.replace('/', '-') + ' 00:00:00'
        else:
            ctrl_update_date_from = ''
        if is_not_empty(ctrl_update_date_to):
            adapt_update_date_to = ctrl_update_date_to.replace('/', '-') + ' 23:59:59'
        else:
            ctrl_update_date_to = ''

        return cls(
            ta_cd=ctrl_ta_cd,
            adapt_ta_cd=ctrl_ta_cd,
            inst_cd=ctrl_inst_cd,
            adapt_inst_cd=ctrl_inst_cd,
            emp_cd=ctrl_emp_cd,
            adapt_emp_cd=ctrl_emp_cd,
            apply_date_from=ctrl_apply_date_from,
            adapt_apply_date_from=adapt_apply_date_from,
            start_date_from=ctrl_start_date_from,
            adapt_start_date_from=adapt_start_date_from,
            start_date_to=ctrl_start_date_to,
            adapt_start_date_to=adapt_start_date_to,
            select_table=radio_select_table,
            end_date_from=ctrl_end_date_from,
            adapt_end_date_from=adapt_end_date_from,
            end_date_to=ctrl_end_date_to,
            adapt_end_date_to=adapt_end_date_to,
            create_date_from=ctrl_create_date_from,
            adapt_create_date_from=adapt_create_date_from,
            create_date_to=ctrl_create_date_to,
            adapt_create_date_to=adapt_create_date_to,
            update_date_from=ctrl_update_date_from,
            adapt_update_date_from=adapt_update_date_from,
            update_date_to=ctrl_update_date_to,
            adapt_update_date_to=adapt_update_date_to
        )
