import {
  GET_HISTORY_SIDEBAR_API,
  GET_HISTORY_POINT_API,
  GET_HISTORY_DATA_API,
  GET_HISTORY_FAVORITE_API,
  POST_HISTORY_FAVORITE_API,
  DELETE_HISTORY_FAVORITE_API,
  UPDATE_HISTORY_FAVORITE_API,
  GET_HISTORY_EXPORT_API,
} from "./api";
import instance, { fileInstance } from "@/util/request";
import apihandler from "@/util/apiHandler";
import downloadExcel from "@/util/downloadExcel";

export const getHistorySideBar = async ({
  sub_system_tag,
  department_id,
  elec_type_id,
  building_guid,
}) => {
  const res = await instance.post(GET_HISTORY_SIDEBAR_API, {
    sub_system_tag,
    department_id,
    elec_type_id,
    building_guid,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getHistoryPoints = async (Device_list) => {
  const res = await instance.post(GET_HISTORY_POINT_API, {
    Device_list,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getHistoryData = async ({
  Type,
  Start_date,
  End_date,
  Start_time,
  End_time,
  Device_list,
  Points,
  table_type,
}) => {
  /*
  {
    Type,
    Start_date,
    End_date,
    Start_time,
    End_time,
    Device_list,
    Points,
  }
  */
  const res = await instance.post(GET_HISTORY_DATA_API, {
    Start_date,
    End_date,
    Start_time,
    End_time,
    Device_list: Array.isArray(Device_list) ? Device_list : [Device_list],
    Points: Array.isArray(Points) ? Points : [Points],
    Type: parseInt(Type),
    table_type: parseInt(table_type),
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getHistoryExportData = async ({
  Type,
  Start_date,
  End_date,
  Start_time,
  End_time,
  Device_list,
  Points,
}) => {
  /*
  {
    Type,
    Start_date,
    End_date,
    Start_time,
    End_time,
    Device_list,
    Points,
  }
  */
  const res = await fileInstance.post(
    GET_HISTORY_EXPORT_API,
    {
      // ...exportContent,
      Start_date: Start_date,
      End_date: End_date,
      Start_time: Start_time,
      End_time: End_time,
      Points: Array.isArray(Points) ? Points : [Points],
      Device_list: Array.isArray(Device_list) ? Device_list : [Device_list],
      Type: parseInt(Type),
      Building_tag_list: [...new Set(Device_list.map((d) => d.split("_")[1]))],
    },
    { responseType: "blob" }
  );

  return apihandler(
    res.code,
    res,
    {
      msg: res.msg,
      code: res.code,
    },
    downloadExcel
  );
};

export const getHistoryFavorite = async () => {
  const res = await instance.post(GET_HISTORY_FAVORITE_API);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const addHistoryFavorite = async (value) => {
  const res = await instance.post(POST_HISTORY_FAVORITE_API, {
    device_name_tag: value.sub_system_tag,
    Device_list: Array.isArray(value.Device_list)
      ? value.Device_list
      : [value.Device_list],
    Points: Array.isArray(value.Points) ? value.Points : [value.Points],
    favorite_name: value.favorite_name,
    Type: parseInt(value.Type),
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const deleteHistoryFavorite = async (favorite_guid) => {
  const res = await instance.post(DELETE_HISTORY_FAVORITE_API, {
    favorite_guid,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const editHistoryFavorite = async ({ favorite_guid, favorite_name }) => {
  const res = await instance.post(UPDATE_HISTORY_FAVORITE_API, {
    favorite_guid,
    favorite_name,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};
