

import { sys } from 'cc';
// import { Alert } from '../game/gameUi/Alert';
import { KS } from './Platform/kuaishouSDK/kuaishou';
import { Meizu } from './Platform/meizuSDK/meizu';
import { OPPO } from './Platform/oppoSDK/oppoAdMange';
import { Toutiao } from './Platform/toutiao/toutiao';
import { Vivo } from './Platform/vivoSDK/Vivo';
import { Wechat } from './Platform/wechat/wechat';
import { EType, IPos, WXCustomAd } from './Platform/wechat/WXCustomAd';
import { XM } from './Platform/xiaomiSDK/xiaomi';
import { tyqSDK } from './tyq-sdk';

// import { JXDef } from '../conventions/JXCommon';
let adStr = "广告";
/**渠道 */
export enum Channel {
    /**默认 浏览器*/
    DEFAULT = 0,
    /**微信 */
    WECHAT = 1,
    /**头条 抖音 */
    TT = 2,
    /**快手 */
    KS = 3,
    /**oppo渠道 */
    OPPO = 4,
    /**vivo渠道 */
    VIVO = 5,
    /**魅族渠道 */
    MEIZU = 6,
    /**小米渠道 */
    XIAOMI = 7,
    ANDROID,
    IOS,
}
export interface IUserInfo {
    nickName: string;
    avatar: string;
    sex?: number,
    uid?: string,
}
export default class SdkMgr {
    protected static _ins: SdkMgr;

    /**录屏对象 */
    private _mediaRecorder: any = null;
    public static get ins(): SdkMgr {
        if (!this._ins) {
            this._ins = new SdkMgr;
        }
        return this._ins;
    }

    constructor() {
        // if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
        //     this.channel = Channel.TT;
        // } else if (window.kwaigame) {
        //     this.channel = Channel.KS;
        // } else if (sys.platform == sys.Platform.WECHAT_GAME && window.wx) {
        //     this.channel = Channel.WECHAT;
        // } else if (sys.isMobile && sys.isBrowser) {
        //     this.channel = Channel.DEFAULT;
        // } else if (false) {
        //     this.channel = Channel.DEFAULT;
        // }
        if (sys.platform == sys.WECHAT_GAME) {
            this.channel = Channel.WECHAT;
        }
    }

    private channel: Channel = Channel.DEFAULT;
    private _videoRes: string = "";
    public getChannel() {
        return this.channel;
    }

    /**观看广告 */
    public showRewardAd(des: any, callback: Function, cancel?: Function, fail?: Function) {
        let cb = () => {
            // GameMgr.lUserData.doneTask(ETaskType.watchAd);
            // loader.loadRes('Alert', Prefab, (err, prefab: Prefab) => {
            //     // if (err) {
            //     //     console.error(err);
            //     //     return;
            //     // }
            //     // let node = instantiate(prefab);
            //     // find('Canvas').addChild(node);
            //     // node.getComponent(Alert).init("广告观看成功");
            // })
            callback();
        }

        console.log("观看广告", des);
        switch (this.channel) {
            case Channel.DEFAULT: {
                cb();
                break;
            }
            case Channel.TT: {
                Toutiao.showRewardVideoAd("guanggao").then((is) => {
                    if (is) {
                        console.log("success")
                        this.umaSetPoint(adStr, { way: des });
                        cb();
                    } else if (cancel) {
                        cancel();
                    }
                }, (res) => {

                    // GameMgr.uiMgr.showToast("广告加载失败");
                    if (fail) {
                        fail();
                    }
                })
                break;
            }
            case Channel.WECHAT: {

                Wechat.showRewardVideoAd().then((isSuc) => {
                    if (isSuc) {
                        console.log("success")
                        this.umaSetPoint(adStr, { way: des });
                        cb();

                    } else if (cancel) {
                        cancel();
                        // loader.loadRes('Alert', Prefab, (err, prefab: Prefab) => {
                        //     if (err) {
                        //         console.error(err);
                        //         return;
                        //     }
                        //     let node = instantiate(prefab);
                        //     find('Canvas').addChild(node);
                        //     node.getComponent(Alert).init("取消观看广告");
                        // })
                    }

                }, (res?) => {
                    if (fail) {
                        fail();
                    }
                    // loader.loadRes('Alert', Prefab, (err, prefab: Prefab) => {
                    //     // if (err) {
                    //     //     console.error(err);
                    //     //     return;
                    //     // }
                    //     // let node = instantiate(prefab);
                    //     // find('Canvas').addChild(node);
                    //     // node.getComponent(Alert).init("广告加载失败");
                    // })
                })
                break;
            }
            case Channel.MEIZU: {
                Meizu.showRewardedVideoAd().then(() => {
                    console.log("success")
                    this.umaSetPoint(adStr, { way: des });
                    cb();
                }, () => {
                    console.log("fail")
                })
                break;
            }
            case Channel.OPPO: {
                OPPO.showRewardVideoAd(() => {
                    console.log("success")
                    this.umaSetPoint(adStr, { way: des });
                    cb();
                }, () => {
                    console.log("fail")
                })
                break;
            }
            case Channel.VIVO: {
                Vivo.showRewardVideoAd((isSuc) => {
                    if (isSuc) {
                        this.umaSetPoint(adStr, { way: des });

                        console.log("success")
                        cb();
                    } else {
                        console.log("fail")
                    }
                })
                break;
            }
            case Channel.XIAOMI: {
                XM.showRewardedVideoAd(() => {
                    this.umaSetPoint(adStr, { way: des });
                    console.log("success");
                    cb();
                }, () => {
                    console.log("fail")
                })
                break;
            }
            case Channel.KS: {
                KS.showRewardedVideoAd(() => {
                    this.umaSetPoint(adStr, { way: des });
                    cb();
                })
                break;
            }
        }

    }

    public umaSetPoint(event: string, obj?: object) {
        return;
        switch (this.channel) {
            case Channel.DEFAULT: {
                break;
            }
            case Channel.TT: {

                tt.uma && tt.uma.trackEvent(event, obj ? obj : null);

                break;
            }
            case Channel.KS: {

                break;
            }
            case Channel.WECHAT: {
                wx.uma && wx.uma.trackEvent(event, obj ? obj : null);
                break;
            }
        }
    }

    public showMoreGamesModal() {
        switch (this.channel) {
            case Channel.TT: {
                Toutiao.showMoreGamesModal();
                break;
            }
        }
    }

    public ttFocusOn() {
        Toutiao.openAwemeUserProfile();
    }

    /**录屏开始 */
    protected startRecoding() {

    }

    /**事件添加开始录屏 */
    public recordStart() {
        console.log("录屏开始*--*");
        if (this.channel == Channel.TT) {
            Toutiao.startRecordScreen(300, this.startRecoding);
            console.log("录屏开始11111", 11111);
        } else if (this.channel == Channel.KS) {
            this._mediaRecorder = kwaigame.createMediaRecorder();
            this._mediaRecorder.start();
        } else if (this.channel == Channel.WECHAT) {

        }
    }

    public puaseRecord() {
        console.log("暂停录屏");
        if (this.channel == Channel.TT) {
            Toutiao.pauseRecordScreen();
        } else if (this.channel == Channel.KS) {
            this._mediaRecorder.pause();
        }
    }

    public resumeRecord() {
        console.log("继续录屏")
        if (this.channel === Channel.TT) {
            Toutiao.resumeRecordScreen()
        } else if (this.channel == Channel.KS) {
            this._mediaRecorder.resume();
        }
    }

    /**事件添加结束录屏 */
    public recordEnd() {
        console.log("录屏结束*--*");
        if (this.channel == Channel.TT) {
            Toutiao.stopRecordScreen((path) => {
                Toutiao.clipVideo(60, path, (res) => {
                    this._videoRes = res.videoPath;
                    console.log("录屏结束*--*", 11111);
                })
            });
        } else if (this.channel == Channel.KS) {
            this._mediaRecorder.stop();
        }
    }

    /**事件转发 */
    public recordCut(cb: Function) {
        if (this.channel == Channel.TT) {
            console.log("录屏" + this._videoRes);
            Toutiao.shareAppMessage("最强小英雄", this._videoRes, ["最强小英雄", "抖音小游戏", "卡牌回合", "卡牌格斗"]).then(() => {
                console.log('分享视频成功');
                cb();
                this.umaSetPoint("分享视频")
            })
        } else if (this.channel == Channel.KS) {
            this._mediaRecorder.publishVideo({
            });
        } else {
            console.log("假装分享视频成功")
            cb();
        }
    }
    //授权
    public authorize(cb: (userInfo: IUserInfo | null) => void) {
        if (this.channel == Channel.TT) {
            Toutiao.TTLogin(() => {

                Toutiao.toutiaoLogin().then((res: any) => {
                    if (res) {
                        let userInfo: IUserInfo = {
                            nickName: res.name,
                            avatar: res.photo,
                        }
                        cb(userInfo);
                        //获取抖音的oppenid
                    } else {
                        cb(null);
                    }


                }).catch((res) => {
                    console.log(`toutiaoLogin 调用失败`);
                    console.log(res);
                    cb(null);
                })
            }, (res) => {
                console.log(res);
                cb(null);
            }, false)
        } else if (this.channel == Channel.KS) {
            KS.KSlogin(() => {
                KS.KSgetUserInfo((res: any) => {

                    let userInfo: IUserInfo = {
                        nickName: res.userName,
                        avatar: res.userHead,
                    }
                    cb(userInfo);
                })
            })
        } else if (this.channel == Channel.DEFAULT) {
            cb(null);
        } else if (this.channel == Channel.WECHAT) {
            // Wechat.login(() => {

            //     Wechat.getUserInfo((res: any) => {
            //         let userInfo: IUserInfo = {
            //             nickName: res.userInfo.nickName,
            //             avatar: res.userInfo.avatarUrl,
            //         }
            //         cb(userInfo);
            //     }, (res) => {
            //         console.log(`getUserInfo 调用失败`);
            //         console.log(res);
            //         cb(null);
            //     })
            // }, (res) => {
            //     log(res);
            //     cb(null);
            // });
            let button = wx.createUserInfoButton({
                type: 'text',
                text: '获取用户信息',
                style: {
                    left: 10,
                    top: 76,
                    width: 200,
                    height: 40,
                    lineHeight: 40,
                    backgroundColor: '#ff0000',
                    color: '#ffffff',
                    textAlign: 'center',
                    fontSize: 16,
                    borderRadius: 4
                }
            })
            button.onTap((res) => {
                console.log(res.userInfo.avatarUrl);
                console.log(res.userInfo.nickName);
            })

        } else {
            cb(null);
        }
    }
    public initGame() {

    }

    public loginOut() {
    }
    /**是否显示分享录屏按钮 */
    public isShowRecordBtn() {
        if (this.channel == Channel.TT || this.channel == Channel.KS) {
            return true;
        }
        return false;
    }

    public isWechat() {
        return this.channel == Channel.WECHAT;
    }
    public preLoadAd() {
        if (this.channel == Channel.WECHAT) {
            Wechat.preLoadAd();
        }
    }
    //显示插屏广告
    public showIntersAd(closeCb?: Function) {
        if (this.channel == Channel.VIVO) {
            Vivo.showInterstitialAd();
        } else if (this.channel == Channel.TT) {
            Toutiao.showInterstitialAd();
        } else if (this.channel == Channel.WECHAT) {
            Wechat.showInterstitialAd(closeCb);
        } else if (this.channel == Channel.OPPO) {
            OPPO.showInterstitialAd();
        }

    }

    public showBanner(node) {
        if (this.channel == Channel.TT) {
            Toutiao.showBannerAd();
        } else if (this.channel == Channel.WECHAT) {
            Wechat.showBannerAd(4000);
        } else if (this.channel == Channel.VIVO) {

            Vivo.showBannerAd();
        } else if (this.channel == Channel.OPPO) {
            OPPO.showBannerAd();
        }
    }
    public showBannerTurns(time) {
        if (this.channel == Channel.WECHAT) {
            Wechat.showBannerTurns(time);
        }
    }
    public hideBanner() {
        if (this.channel == Channel.TT) {
            Toutiao.hideBannerAd();
        } else if (this.channel == Channel.VIVO) {
            Vivo.hideBannerAd();
        } else if (this.channel == Channel.OPPO) {
            OPPO.hideBannerAd();
        } else if (this.channel == Channel.WECHAT) {
            Wechat.hideBannerAd();
        }
    }
    public showWxCustomAd(flag, type: EType, pos: IPos, extraId?, cb?: Function) {
        if (this.channel == Channel.WECHAT) {
            WXCustomAd.createCustomAd(flag, type, pos, extraId, cb);
        }
    }
    public hideWxCustomAd(flag) {
        if (this.channel == Channel.WECHAT) {
            WXCustomAd.hideCustomAd(flag);
        }
    }
    public hideAllWxCustom() {
        if (this.channel == Channel.WECHAT) {
            WXCustomAd.hideAllAd();
        }
    }

    /**跳转小游戏 */
    jumpGame(appid: string, event, id) {
        // if (cc.sys.platform != cc.sys.WECHAT_GAME) return
        Wechat.navigateToMiniProgram(appid, "", () => {
            console.log("成功跳转");
            tyqSDK.collectClickEventGame(event, id);
        }, () => {
            console.log("跳转失败");
            //好友在玩界面
        })
    }

}
