import { defineStore } from "pinia";
import { onMounted, ref } from "vue";
import dayjs from "dayjs";
import { getAllDevice } from "@/apis/building";

const useAlarmStore = defineStore("alarmData", () => {
  let timer = null;
  const alarmData = ref([]);
  const allDeviceList = ref([]);

  const getFormateDate = (datetime, format_rule = "YYYY-MM-DD HH:mm") => {
    return dayjs(datetime).format(format_rule);
  };
  // get data from baja
  const getAlarmDataFromBaja = () => {
    window.require &&
      window.requirejs(["baja!"], (baja) => {
        let alarms = [];
        baja.Ord.make(
          `local:|foxs:|alarm:|bql:select *  where sourceState = 'offnormal' order by timestamp desc`
        ).get({
          cursor: {
            before: () => {
              timer = null;
            },
            each: (record) => {
              const device_number = record
                .get("alarmData")
                .get("sourceName")
                .split("_")
                .slice(0, 8)
                .join("_");
              // console.log("getAlarm each", {
              //   uuid: record.get("uuid"),
              //   alarmClass: record.get("alarmClass"),
              //   timestamp_date: getFormateDate(
              //     record.get("timestamp").getJsDate(),
              //     "YYYY/MM/DD"
              //   ),
              //   timestamp_time: getFormateDate(
              //     record.get("timestamp").getJsDate(),
              //     "HH:mm:ss"
              //   ),
              //   device_number,
              //   msg: record.get("alarmData").get("msgText"),
              //   sourceState: record.get("sourceState").getDisplayTag(),
              //   normalTime: getFormateDate(
              //     record.get("normalTime").getJsDate()
              //   ),
              //   ackState: record.get("ackState").getDisplayTag(),
              //   ackedTime: getFormateDate(record.get("ackTime").getJsDate()),
              // });

              alarms.push({
                uuid: record.get("uuid"),
                timestamp_date: getFormateDate(
                  record.get("timestamp").getJsDate(),
                  "YYYY/MM/DD "
                ),
                timestamp_time: getFormateDate(
                  record.get("timestamp").getJsDate(),
                  "HH:mm:ss"
                ),
                alarmClass: record.get("alarmClass"),
                device_number,
                sourceName: record.get("alarmData").get("sourceName"),
                full_name: allDeviceList.value.find(
                  (d) => device_number === d.device_number
                )?.full_name,
                msg: record.get("alarmData").get("msgText"),
                sourceState: record.get("sourceState").getDisplayTag(),
                normalTime: getFormateDate(
                  record.get("normalTime").getJsDate()
                ),
                ackState: record.get("ackState").getDisplayTag(),
                ackedTime: getFormateDate(record.get("ackTime").getJsDate()),
              });
            },
            after: () => {
              alarmData.value = alarms;
              timer = setTimeout(() => {
                getAlarmDataFromBaja();
              }, 2000);
            },
          },
        });
      });
  };

  const getDevice = async () => {
    const res = await getAllDevice();
    allDeviceList.value = res.data;
  };

  onMounted(() => {
    getDevice();
  });

  return { getAlarmDataFromBaja, alarmData };
});
export default useAlarmStore;
