import { storeData } from "../store";
import {
	CallBack,
	IPostMessage,
	listenerMessage,
	postMessage,
	promisefiyPostMessage,
	removeListenerMessage,
} from "../utils/native-message";
/** 获取token */
export const api_getToken = (): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "getToken" });
};
/**获取耳机的插入状态 */
export const getEarphone = (): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "isWiredHeadsetOn" });
};

/** 获取异形屏信息 */
export const isSpecialShapedScreen = (): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "isSpecialShapedScreen" });
};

/** 开始录音 */
export const startSoundCheck = () => {
	postMessage({
		api: "startSoundCheck",
	});
};

/** 录音返回 */
export const sendResult = (callback: CallBack) => {
	listenerMessage("sendResult", callback);
};
/** 取消监听录音返回 */
export const removeResult = (callback: CallBack) => {
	removeListenerMessage("sendResult", callback);
};

/** 结束录音 */
export const endSoundCheck = () => {
	postMessage({
		api: "endSoundCheck",
	});
};

/** 开始评测 */
export const startEvaluating = (content: any): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "startEvaluating", content: content });
};
/** 结束评测 */
export const endEvaluating = (content: any): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "endEvaluating", content: content });
};
/** 取消评测 */
export const cancelEvaluating = () => {
	postMessage({
		api: "cancelEvaluating",
	});
};

/** 评测开始录音 */
export const api_startRecording = (): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "startRecording" });
};
/** 评测结束录音 */
export const api_stopRecording = (): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "stopRecording" });
};

/** 和websocket通信 */
export const api_proxyServiceMessage = (content: any): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "proxyServiceMessage", content });
};
/** 监听app真正开始录音 */
export const api_recordStartTime = (callback: CallBack) => {
	listenerMessage("recordStartTime", callback);
};
/** 卸载监听app真正开始录音 */
export const api_remove_recordStartTime = (callback: CallBack) => {
	removeListenerMessage("recordStartTime", callback);
};

/** 上传评测视频 */
export const api_videoUpdate = (callback: CallBack) => {
	postMessage(
		{
			api: "videoUpdate",
		},
		callback
	);
};

/** 分享 */
export const api_shareAchievements = (content: any): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "shareAchievements", content });
};

/** openwebview */
export const api_openWebView = (content: any): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "openWebView", content });
};

/** 开启摄像头 */
export const api_openCamera = (): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "openCamera" });
};

/** 关闭摄像头 */
export const api_closeCamera = (): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "closeCamera" });
};

/** 安卓隐藏状态栏 */
export const api_setStatusBarVisibility = () => {
	// 安卓的状态栏
	postMessage({
		api: "setStatusBarVisibility",
		content: {
			isVisibility: 0,
		},
	});
};

/** 跟练录音切换 */
export const api_cloudToggleFollow = (state: "start" | "end") => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({
		api: "cloudToggleFollow",
		content: {
			state,
		},
	});
};

/** 跟练录音监听 */
export const api_cloudFollowTime = (onFollowTime: CallBack, listen = true) => {
	if (listen) {
		listenerMessage("cloudFollowTime", onFollowTime);
	} else {
		removeListenerMessage("cloudFollowTime", onFollowTime);
	}
};

/** 结束webview */
export const api_back = () => {
	postMessage({
		api: "back",
	});
};
/** 切换全屏loading */
export const api_cloudLoading = (show = false) => {
	postMessage({
		api: "cloudLoading",
		content: {
			show,
			type: "fullscreen",
		},
	});
};

/** 销毁云教练 */
export const api_cloudDestroy = () => {
	postMessage({
		api: "cloudDestroy",
	});
};
/** 事件埋点统计: 酷乐秀用,其它没有用 */
export const api_setEventTracking = () => {
	postMessage({
		api: "setEventTracking",
		content: {
			type: "klx_xiaokuAI",
		},
	});
};
/** 保存图片 */
export const api_savePicture = (content: any): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "savePicture", content });
};
/** 缓存评测分数 */
export const api_setCache = (content: any) => {
	postMessage({
		api: "setCache",
		content,
	});
};

/** app切换到后台 */
export const api_suspendPlay = (callback: CallBack) => {
	listenerMessage("suspendPlay", callback);
};

/** 开始录制视频 */
export const api_startCapture = () => {
	postMessage({
		api: "startCapture",
	});
};

/** 结束录制视频 */
export const api_endCapture = () => {
	postMessage({
		api: "endCapture",
	});
};
