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 = (content: any): Promise<IPostMessage | undefined> => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "startRecording", content: content });
};
/** 评测开始录音 */
export const api_startRecordingCb = (content: any, callback: CallBack) => {
	postMessage(
		{
			api: "startRecording",
			content,
		},
		callback
	);
};


/** 评测结束录音 */
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);
	}
};
export const api_goback = () => {
	postMessage({
		api: "goBack",
	});
};
/** 结束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",
	});
};

/** 获取设备延迟检测 */
export const api_getDeviceDelay = () => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "getDeviceDelay" });
};

/** 延迟检测触发 */
export const api_toggleTune = (state: "start" | "stop" | "finishTune", count?: number) => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	if (state === "start") {
		// 开始效音
		return promisefiyPostMessage({
			api: "startTune",
			content: {
				count: count + "",
			},
		});
	} else if (state === "stop") {
		// 结束效音，触发时机： 1.监听后台效音返回 2.点击跳过效音或关闭效音
		return promisefiyPostMessage({ api: "endTune" });
	} else if (state === "finishTune") {
		// 效音完成
		return promisefiyPostMessage({ api: "finishTune" });
	}
};

/** 初始化曲谱音频 和效音音频 */
export const api_createMusicPlayer = (content: any) => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "createMusicPlayer", content });
};

/** 初始化曲谱音频 和效音音频 */
export const api_updateMusicPlayer = (content: any) => {
	if (!storeData.isApp) return Promise.resolve({} as any);
	return promisefiyPostMessage({ api: "createMusicPlayer", content });
};

/** 进入页面设置常量 */
export const api_keepScreenLongLight = () => {
	postMessage({
		api: "keepScreenLongLight",
		content: {
			isOpenLight: true,
		},
	});
};
/** 设置横竖屏
 * 0:横屏
 * 1:竖屏
 */
export const api_setRequestedOrientation = (orientation: number) => {
	postMessage({
		api: "setRequestedOrientation",
		content: {
			orientation,
		},
	});
};

/** 监听APP播放 */
export const api_playProgress = (callback: any) => {
	listenerMessage("playProgress", callback);
};
/** 卸载播放 */
export const api_remove_playProgress = (callback: any) => {
	removeListenerMessage("playProgress", callback);
};

/** 合成音频传伴奏 */
export const api_cloudAccompanyMessage = (audioFileUrl: string) => {
	postMessage({
		api: "cloudAccompanyMessage",
		content: {
			accompanyUrl: audioFileUrl,
		},
	});
};

/** 合成音频传伴奏弹窗 */
export const api_openAdjustRecording = (content: any) => {
	postMessage({
		api: "openAdjustRecording",
		content,
	});
};

/** 监听评测弹窗是否隐藏保存演奏按钮 */
export const hideComplexButton = (callback: CallBack, listen?: boolean) => {
	if (listen) {
		listenerMessage("hideComplexButton", callback);
	} else {
		removeListenerMessage("hideComplexButton", callback);
	}
};


/** 评测模式是否开启/关闭伴奏 */
export const api_onoffAccompaniment = () => {
	postMessage({
		api: "onoffAccompaniment",
		content: {
			state: 0,
		},
	});
};


/** 延迟检测校音，校音音频播放完成回调 */
export const addCheckPlayEnd = (callback: CallBack) => {
	listenerMessage("checkPlayEnd", callback);
};

/** 取消监听延迟检测校返回 */
export const removeCheckPlayEnd = (callback: CallBack) => {
	removeListenerMessage("checkPlayEnd", callback);
};

/** 检查APP端websocket状态 */
export const api_checkSocketStatus = () => {
	return promisefiyPostMessage({ api: "checkSocketStatus" });
};

/** 监听APP发送的异常信息 */
export const addAccompanyError = (callback: CallBack) => {
	listenerMessage("accompanyError", callback);
};

/** 取消监听APP发送的异常信息 */
export const removeAccompanyError = (callback: CallBack) => {
	removeListenerMessage("accompanyError", callback);
};

/** 检测socket链接状态 */
export const addSocketStatus = (callback: CallBack) => {
	listenerMessage("socketConnectSuccessReport", callback);
};

/** 取消检测socket链接状态 */
export const removeSocketStatus = (callback: CallBack) => {
	removeListenerMessage("socketConnectSuccessReport", callback);
};

/** 检查APP端websocket状态 */
export const api_disconnectSocket = () => {
	return promisefiyPostMessage({ api: "disconnectSocket" });
};



// MIDI播放&评测相关的api

/** 发送midi音频等信息 */
export const api_cloudDetail = (content: any, callback: CallBack) => {
	postMessage(
		{
			api: "cloudDetail",
			content,
		},
		callback
	);
};

/** 检查midi播放器状态，status: 'init' | 'play' | 'suspend' */
export const api_cloudGetMediaStatus = () => {
	return promisefiyPostMessage({ api: "cloudGetMediaStatus" });
};

/** midi开始播放 */
export const api_cloudPlay = (content: any) => {
	promisefiyPostMessage({
		api: "cloudPlay",
		content,
	});
};

/** midi暂停播放 */
export const api_cloudSuspend = (content: any) => {
	promisefiyPostMessage({
		api: "cloudSuspend",
		content,
	});
};

/** midi跳转到指定位置播放 */
export const api_cloudSetCurrentTime = (content: any) => {
	promisefiyPostMessage({
		api: "cloudSetCurrentTime",
		content,
	});
};

/** midi调整播放速度 */
export const api_cloudChangeSpeed = (content: any) => {
	promisefiyPostMessage({
		api: "cloudChangeSpeed",
		content,
	});
};

/** midi设置声轨音量 */
export const api_cloudVolume = (content: any) => {
	promisefiyPostMessage({
		api: "cloudVolume",
		content,
	});
};

/** midi,播放系统节拍器 */
export const api_cloudMetronome = (content: any, callback: CallBack) => {
	postMessage(
		{
			api: "cloudMetronome",
			content,
		},
		callback
	);
};

/** midi练习播放&评测播放回调 */
export const api_cloudTimeUpdae = (callback: any) => {
	listenerMessage("cloudTimeUpdae", callback);
};

/** 卸载监听midi播放回调 */
export const api_remove_cloudTimeUpdae = (callback: any) => {
	removeListenerMessage("cloudTimeUpdae", callback);
};

/** midi播放结束回调 */
export const api_cloudplayed = (callback: any) => {
	listenerMessage("cloudplayed", callback);
};

/** 卸载midi播放结束回调 */
export const api_remove_cloudplayed = (callback: any) => {
	removeListenerMessage("cloudplayed", callback);
};

/** midi评测传offsetTime和micDelay */
export const api_midiMicDelay = (content: any) => {
	postMessage({
		api: "proxyServiceMessage",
		content,
	});
};

/** 监听老师端，上课页面，功能按钮方向 */
export const addImagePos = (callback: any) => {
	listenerMessage("imagePos", callback);
};

/** 开始延迟检测 */
export const api_startDelayCheck = (content: any) => {
	promisefiyPostMessage({
		api: "startDelayCheck",
		content,
	});
};

/** 关闭延迟检测 */
export const api_closeDelayCheck = (content: any) => {
	promisefiyPostMessage({
		api: "closeDelayCheck",
		content,
	});
};

/** 监听APP取消延迟检测 */
export const api_cancelDelayCheck = (callback: any) => {
	listenerMessage("cancelDelayCheck", callback);
};

/** 监听延迟检测成功的回调 */
export const api_finishDelayCheck = (callback: any) => {
	listenerMessage("finishDelayCheck", callback);
};

/** 监听延迟检测成功的回调 */
export const api_remove_finishDelayCheck = (callback: any) => {
	removeListenerMessage("finishDelayCheck", callback);
};

/** 取消监听APP取消延迟检测 */
export const api_remove_cancelDelayCheck = (callback: any) => {
	removeListenerMessage("cancelDelayCheck", callback);
};

/** 监听APP播放进度 */
export const simple_playProgress = (callback: any) => {
	listenerMessage("api_playProgress", callback);
};
/** 卸载播放 */
export const simple_remove_playProgress = (callback: any) => {
	removeListenerMessage("api_playProgress", callback);
};

/** 监听APP播放开始 */
export const simple_play = (callback: any) => {
	listenerMessage("api_play", callback);
};

/** 监听APP播放暂停 */
export const simple_paused = (callback: any) => {
	listenerMessage("api_paused", callback);
};

/** 监听APP拖动进度 */
export const simple_updateProgress = (callback: any) => {
	listenerMessage("api_updateProgress", callback);
};

/** simple页面渲染完成 */
export const simple_musicPage = (content: any) => {
	postMessage({
		api: "api_musicPage",
		content,
	});
};

/** 监听重新评测消息 */
export const api_retryEvaluating = (callback: any) => {
	listenerMessage("retryEvaluating", callback);
}