import {
  GET_REALTIME_DATA_API,
  GET_ELEC_MONTH_API,
  GET_ELEC_DAY_API,
  GET_REALTIME_DIST_API,
  GET_ELECUSE_DAY_API,
  GET_TAI_POWER_API,
  GET_SIDEBAR_API,
  GET_SEARCH_API,
  GET_REPORT_API,
  GET_Excel_API,
  GET_DEMAND_API,
  POST_EDIT_DEMAND_API,
  GET_REALTIME_DEMAND_API,
  GET_CARBON_API,
  POST_EDIT_CARBON_API,
  GET_TIME_ELEC_API,
  POST_TIME_ELEC_API,
} from "./api";
import instance, { fileInstance } from "@/util/request";
import apiHandler from "@/util/apiHandler";
import downloadExcel from "@/util/downloadExcel";

export const getRealTimeData = async () => {
  const res = await instance.post(GET_REALTIME_DATA_API);

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

export const getElecUseMonth = async () => {
  const res = await instance.post(GET_ELEC_MONTH_API);

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

export const getElecUseofDay = async () => {
  const res = await instance.post(GET_ELEC_DAY_API);

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

export const getRealTimeDist = async ({
  building_guid,
  department_id_list,
  floor_guid_list,
}) => {
  const res = await instance.post(GET_REALTIME_DIST_API, {
    building_guid,
    department_id_list,
    floor_guid_list,
  });

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

export const getElecUseDay = async ({
  building_guid,
  department_id_list,
  floor_guid_list,
}) => {
  const res = await instance.post(GET_ELECUSE_DAY_API,{
    building_guid,
    department_id_list,
    floor_guid_list,
  });

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

export const getTaipower = async ({
  coefficient,
  building_guid,
  department_id_list,
  floor_guid_list,
}) => {
  const res = await instance.post(GET_TAI_POWER_API, {
    coefficient,
    building_guid,
    department_id_list,
    floor_guid_list,
  });

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

export const getSideBar = async (system_type) => {
  const res = await instance.post(GET_SIDEBAR_API, { system_type });

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

export const getEnergySearch = async (type) => {
  const res = await instance.post(GET_SEARCH_API, { type });

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

export const getReport = async ({
  department,
  elecType,
  floor,
  start_time,
  end_time,
  type,
}) => {
  const res = await instance.post(GET_REPORT_API, {
    department,
    elecType,
    floor,
    start_time,
    end_time,
    type,
  });

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

export const getExcel = async ({
  department,
  elecType,
  floor,
  start_time,
  end_time,
  type,
}) => {
  const res = await fileInstance.post(
    GET_Excel_API,
    {
      department,
      elecType,
      floor,
      start_time,
      end_time,
      type,
    },
    { responseType: "blob" }
  );

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

export const getDemand = async (building_guid) => {
  const res = await instance.post(GET_DEMAND_API, { building_guid });

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

export const postEditDemand = async ({
  id,
  contract,
  alert,
  reset,
  building_guid,
}) => {
  const res = await instance.put(POST_EDIT_DEMAND_API, {
    id,
    contract,
    alert,
    reset,
    building_guid,
  });

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

export const getCarbonValue = async (building_guid) => {
  const res = await instance.post(GET_CARBON_API, { building_guid });

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

export const postEditCarbonValue = async ({
  id,
  coefficient,
  building_guid,
}) => {
  const res = await instance.put(POST_EDIT_CARBON_API, {
    id,
    coefficient,
    building_guid,
  });

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

export const getRealTimeDemand = async (building_guid) => {
  const res = await instance.post(GET_REALTIME_DEMAND_API, { building_guid });

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

export const getTimeElec = async (building_guid) => {
  const res = await instance.post(GET_TIME_ELEC_API, { building_guid });

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

export const postTimeElec = async ({ sheet, cost, building_guid }) => {
  const res = await instance.put(POST_TIME_ELEC_API, {
    sheet,
    cost,
    building_guid,
  });

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