import { dateUtil } from "@/utils/dateUtil";
import { reactive, toRefs } from "vue";
import { tryOnMounted, tryOnUnmounted } from "@vueuse/core";
import { IntervalHandle } from "@/typings/global";
import { useI18n } from "vue-i18n";

export function useNow(immediate = true) {
  let timer: IntervalHandle;

  const state = reactive({
    year: 0,
    month: 0,
    week: "",
    day: 0,
    hour: "",
    minute: "",
    second: 0,
    meridiem: ""
  });

  const i18n = useI18n();

  const update = () => {
    const now = dateUtil();

    const h = now.format("HH");
    const m = now.format("mm");
    const s = now.get("s");

    state.year = now.get("y");
    state.month = now.get("M") + 1;

    if (i18n.locale.value == "zh-cn") {
      state.week = "星期" + ["日", "一", "二", "三", "四", "五", "六"][now.day()];
    } else if (i18n.locale.value == "en") {
      state.week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][now.day()];
    }

    state.day = now.get("date");
    state.hour = h;
    state.minute = m;
    state.second = s;

    state.meridiem = now.format("A");
  };

  function start() {
    update();
    clearInterval(timer);
    timer = setInterval(() => update(), 1000);
  }

  function stop() {
    clearInterval(timer);
  }

  tryOnMounted(() => {
    immediate && start();
  });

  tryOnUnmounted(() => {
    stop();
  });

  return {
    ...toRefs(state),
    start,
    stop
  };
}
