/*
 * @Descripttion: 
 * @version: 1.0.0
 * @Author: YeeChan
 * @Date: 2020-07-09 18:54:40
 */

import AppPlatform from "../Util/AppPlatform";

/**
 * 设备震动相关
 */
export default class VibrateMgr {
    //开关
    public static isEnable_custom = true;

    //短震动
    public static vibrateShort_custom(): void {
        if (!VibrateMgr.isEnable_custom)
            return;
        let name = "";
        if (AppPlatform.is_TT_GAME_custom()) {
            name = "tt";
        } else if (AppPlatform.is_WECHAT_GAME_custom()) {
            name = "wx";
        } else if (AppPlatform.is_OPPO_GAME_custom() || AppPlatform.is_VIVO_GAME_custom()) {
            name = "qg";
        } else if (AppPlatform.is_QQ_PLAY_custom()) { //qq小游戏
            name = "qq";
        }
        if (name != "") {
            window[name].vibrateShort();
        }
    }

    //长震动
    public static vibrateLong_custom(): void {
        if (!VibrateMgr.isEnable_custom)
            return;

        let name = "";
        if (AppPlatform.is_TT_GAME_custom()) {
            name = "tt";
        } else if (AppPlatform.is_WECHAT_GAME_custom()) {
            name = "wx";
        } else if (AppPlatform.is_OPPO_GAME_custom() || AppPlatform.is_VIVO_GAME_custom()) {
            name = "qg";
        } else if (AppPlatform.is_QQ_PLAY_custom()) { //qq小游戏
            name = "qq";
        }
        if (name != "") {
            window[name].vibrateLong();
        }
    }

    //定时震动,毫秒
    public static vibrate_custom(time: number) {
        if (!VibrateMgr.isEnable_custom) {
            return;
        }
        let num = 0;
        let count = time / 15;

        let funCall = null;
        funCall = function (cTime: number) {
            setTimeout(() => {
                VibrateMgr.vibrateShort_custom();
                num++;
                if (num <= count) {
                    funCall(cTime);
                }
            }, cTime)
        }


        if (AppPlatform.is_TT_GAME_custom()) {//抖音,字节跳动
            count = time / 15;
            funCall(16)
        } else if (AppPlatform.is_WECHAT_GAME_custom()) {
            count = time / 15; //微信小游戏中震动的时间是15毫秒的整数倍时间,本质是对短震动的封装
            funCall(16)
        } else if (AppPlatform.is_OPPO_GAME_custom()) {
            count = time / 20; //OPPO小游戏中震动的时间是20毫秒的整数倍时间,本质是对短震动的封装
            funCall(21)
        } else if (AppPlatform.is_QQ_PLAY_custom()) {//qq小游戏
            count = time / 20; //OPPO小游戏中震动的时间是20毫秒的整数倍时间,本质是对短震动的封装
            funCall(21)
        }
    }
}