import { getRequest } from "@/api/customFetch";
import { getBigScreenRanking, getGainUrl, getPaymentUrl, getSegmentStatic, getSixStatisticsUrl } from "@/api/fetchUrl";
import { usePolling, addRequest } from "@/composables/usePolling";
import { useRouter } from "vue-router";

export const useFetchAllData = () => {
  const router = useRouter();
  const paymentData = ref<any>({});
  const getPaymentData = () => {
    getRequest(getPaymentUrl(), {}, {}).then((resp) => {
      if (resp.code == 200) {
        paymentData.value = JSON.parse(JSON.stringify(resp.result));
      } else if (resp.code == 401) {
        router.push("/login");
      }
    });
  };
  addRequest("paymentData", getPaymentData);
  provide("paymentData", paymentData);

  const gainData = ref<any>({});
  const getGainData = () => {
    getRequest(getGainUrl(), null, {}).then((resp) => {
      if (resp.code === 200) {
        gainData.value = resp.result;
      }
    });
  };
  addRequest("getGainData", getGainData);
  provide("gainData", gainData);

  const askSectionData = ref<any>({});
  const getAskSectionData = () => {
    getRequest(getSegmentStatic(), {}, {}).then((resp) => {
      if (resp.code === 200) {
        askSectionData.value = resp.result;
      }
    });
  };
  addRequest("getAskSectionData", getAskSectionData);
  provide("askSectionData", askSectionData);

  const chargingRankingData = ref<any>({});
  const getChargingRankingData = () => {
    getRequest(getBigScreenRanking(), {take:9999}, {}).then((resp) => {
      if (resp.code === 200) {
        chargingRankingData.value = resp.result;
      }
    });
  };
  addRequest("getChargingRankingData", getChargingRankingData);
  provide("chargingRankingData", chargingRankingData);

  const sixStatisticsData = ref<any>({});
  const getSixStaticsData = () => {
    getRequest(getSixStatisticsUrl(), {}, {}).then((resp) => {
      if (resp.code === 200) {
        sixStatisticsData.value = resp.result;
      }
    });
  };

  addRequest("getSixStaticsData", getSixStaticsData);
  provide("sixStatisticsData", sixStatisticsData);

  usePolling();
};
