import { TrackPlayerEvents } from "@/core.defination/trackPlayer";
import TrackPlayer from "@/core/trackPlayer";
import NativeUtils from "@/native/utils";
import { atom, getDefaultStore, useAtomValue } from "jotai";
import { useEffect, useRef, useState } from "react";
import BackgroundTimer from "react-native-background-timer";
const deadlineAtom = atom<number | null>(null);
const closeAfterPlayEndAtom = atom(false);
let timerId: any;
async function exitApp() {
await TrackPlayer.reset();
NativeUtils.exitApp();
}
function setScheduleClose(deadline: number | null) {
getDefaultStore().set(deadlineAtom, deadline);
timerId && BackgroundTimer.clearTimeout(timerId);
if (deadline && deadline > Date.now()) {
timerId = BackgroundTimer.setTimeout(async () => {
const playAfterEnd = getDefaultStore().get(closeAfterPlayEndAtom);
if (playAfterEnd) {
TrackPlayer.on(TrackPlayerEvents.PlayEnd, exitApp);
} else {
exitApp();
}
}, deadline - Date.now());
} else {
if (timerId) {
BackgroundTimer.clearTimeout(timerId);
}
timerId = null;
}
}
function setCloseAfterPlayEnd(closeAfterPlayEnd: boolean) {
if (!closeAfterPlayEnd) {
TrackPlayer.off(TrackPlayerEvents.PlayEnd, exitApp);
}
getDefaultStore().set(closeAfterPlayEndAtom, closeAfterPlayEnd);
}
function useScheduleCloseCountDown() {
const deadline = useAtomValue(deadlineAtom);
const [countDown, setCountDown] = useState(
deadline ? deadline - Date.now() : null);
const intervalRef = useRef<any>();
useEffect(() => {
intervalRef.current && clearInterval(intervalRef.current);
intervalRef.current = null;
if (!deadline || deadline <= Date.now()) {
setCountDown(null);
return;
} else {
setCountDown(Math.max(deadline - Date.now(), 0) / 1000);
intervalRef.current = setInterval(() => {
setCountDown(Math.max(deadline - Date.now(), 0) / 1000);
}, 1000);
}
return () => {
intervalRef.current && clearInterval(intervalRef.current);
intervalRef.current = null;
};
}, [deadline]);
return countDown;
}
const useCloseAfterPlayEnd = () => useAtomValue(closeAfterPlayEndAtom);
export { setScheduleClose, useScheduleCloseCountDown, setCloseAfterPlayEnd, useCloseAfterPlayEnd };