import { ref } from "vue";
import { defineStore } from "pinia";

import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";

import type { RequestObject } from "@/modules/04_registryPerson/interface/request/Profile";
import type {
  DataOption,
  InformationOps,
} from "@/modules/04_registryPerson/interface/index/Main";

const mixin = useCounterMixin();
const { showLoader, hideLoader } = mixin;

export const useProfileDataStore = defineStore("profile", () => {
  //ข้อมูลส่วนตัว
  const defaultProfile: RequestObject = {
    bloodGroup: null,
    relationship: null,
    gender: null,
    religion: null,
    citizenId: "",
    nationality: null,
    ethnicity: null,
    birthDate: null,
    lastName: "",
    firstName: "",
    prefix: "",
    rank: null,
    prefixMain: "",
  };

  //ข้อมูลตัวเลือก
  const OpsFilter = ref<InformationOps>({
    prefixOps: [],
    rankOps: [],
    genderOps: [],
    bloodOps: [],
    statusOps: [],
    religionOps: [],
    employeeClassOps: [
      { id: "perm", name: "ลูกจ้างประจำ" },
      { id: "temp", name: "ลูกจ้างชั่วคราว" },
    ],
    employeeTypeOps: [
      { id: "gov", name: "งบประมาณเงินอุดหนุนรัฐบาล" },
      { id: "bkk", name: "งบประมาณกรุงเทพมหานคร" },
    ],
  });
  //รายการตัวเลือก
  const Ops = ref<InformationOps>({
    prefixOps: [],
    rankOps: [],
    genderOps: [],
    bloodOps: [],
    statusOps: [],
    religionOps: [],
    employeeClassOps: [
      { id: "perm", name: "ลูกจ้างประจำ" },
      { id: "temp", name: "ลูกจ้างชั่วคราว" },
    ],
    employeeTypeOps: [
      { id: "gov", name: "งบประมาณเงินอุดหนุนรัฐบาล" },
      { id: "bkk", name: "งบประมาณกรุงเทพมหานคร" },
    ],
  });

  /**
   * ฟังก์ชันดึงข้อมูลตัวเลือกข้อมูลหลัก
   */
  async function fetchPerson() {
    showLoader();
    await http
      .get(config.API.profileNewMetaMain)
      .then((res) => {
        const data = res.data.result;
        let optionbloodGroups: DataOption[] = [];
        data.bloodGroups.map((r: any) => {
          optionbloodGroups.push({
            id: r.id.toString(),
            name: r.name.toString(),
          });
        });
        Ops.value.bloodOps = optionbloodGroups;
        OpsFilter.value.bloodOps = optionbloodGroups;

        let optiongenders: DataOption[] = [];
        data.genders.map((r: any) => {
          optiongenders.push({
            id: r.id.toString(),
            name: r.name.toString(),
          });
        });
        Ops.value.genderOps = optiongenders;
        OpsFilter.value.genderOps = optiongenders;

        let optionprefixs: DataOption[] = [];
        data.prefixs.map((r: any) => {
          optionprefixs.push({
            id: r.id.toString(),
            name: r.name.toString(),
          });
        });
        Ops.value.prefixOps = optionprefixs;
        OpsFilter.value.prefixOps = optionprefixs;

        let optionrank: DataOption[] = [];
        data.rank.map((r: any) => {
          optionrank.push({
            id: r.id.toString(),
            name: r.name.toString(),
          });
        });
        Ops.value.rankOps = optionrank;
        OpsFilter.value.rankOps = optionrank;

        let optionrelationships: DataOption[] = [];
        data.relationships.map((r: any) => {
          optionrelationships.push({
            id: r.id.toString(),
            name: r.name.toString(),
          });
        });
        Ops.value.statusOps = optionrelationships;
        OpsFilter.value.statusOps = optionrelationships;

        let optionreligions: DataOption[] = [];
        data.religions.map((r: any) => {
          optionreligions.push({
            id: r.id.toString(),
            name: r.name.toString(),
          });
        });
        Ops.value.religionOps = optionreligions;
        OpsFilter.value.religionOps = optionreligions;
      })
      .catch((e) => {
        console.log(e);
      })
      .finally(() => {
        setTimeout(() => {
          hideLoader();
        }, 2500);
      });
  }

  /**
   * ฟังกชันค้นหาข้อมูลใน select
   * @param val  คำที่ต้องการค้นหา
   * @param update function
   * @param refData ประเภทตัวเลือก
   */
  const filterSelector = (val: string, update: Function, refData: string) => {
    const newVal = val.toLowerCase();
    switch (refData) {
      case "prefixOps":
        update(() => {
          Ops.value.prefixOps = OpsFilter.value.prefixOps.filter(
            (v: DataOption) => v.name.toLowerCase().indexOf(newVal) > -1
          );
        });
        break;
      case "rankOps":
        update(() => {
          Ops.value.rankOps = OpsFilter.value.rankOps.filter(
            (v: DataOption) => v.name.toLowerCase().indexOf(newVal) > -1
          );
        });
        break;
      case "genderOps":
        update(() => {
          Ops.value.genderOps = OpsFilter.value.genderOps.filter(
            (v: DataOption) => v.name.toLowerCase().indexOf(newVal) > -1
          );
        });
        break;
      case "bloodOps":
        update(() => {
          Ops.value.bloodOps = OpsFilter.value.bloodOps.filter(
            (v: DataOption) => v.name.toLowerCase().indexOf(newVal) > -1
          );
        });
        break;
      case "statusOps":
        update(() => {
          Ops.value.statusOps = OpsFilter.value.statusOps.filter(
            (v: DataOption) => v.name.toLowerCase().indexOf(newVal) > -1
          );
        });
        break;
      case "religionOps":
        update(() => {
          Ops.value.religionOps = OpsFilter.value.religionOps.filter(
            (v: DataOption) => v.name.toLowerCase().indexOf(newVal) > -1
          );
        });
        break;
      case "employeeClassOps":
        update(() => {
          Ops.value.employeeClassOps = OpsFilter.value.employeeClassOps.filter(
            (v: DataOption) => v.name.toLowerCase().indexOf(newVal) > -1
          );
        });
        break;
      case "employeeTypeOps":
        update(() => {
          Ops.value.employeeTypeOps = OpsFilter.value.employeeTypeOps.filter(
            (v: DataOption) => v.name.toLowerCase().indexOf(newVal) > -1
          );
        });
        break;

      default:
        break;
    }
  };

  return {
    defaultProfile,
    Ops,
    OpsFilter,

    fetchPerson,
    filterSelector,
  };
});
