// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { ryw_Event } from "../FrameWork/Event/EventEnum";
import EventMgr from "../FrameWork/Event/EventMgr";
import GameReport, { ENTERTYPE } from "../FrameWork/Report/ZyZyReport";
import User from "../FrameWork/User/User";
import AppPlatform from "../FrameWork/Util/AppPlatform";
import Common5 from "../Platform/th/Common5";
import CommonDefine from "../Platform/th/CommonDefine";
import TTAPI from "../Platform/tt/TTAPI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MainScene extends cc.Component {

    @property(cc.Node)
    GameNode: cc.Node = null;

    @property(cc.Node)
    clickEffect: cc.Node = null;

    @property(cc.Node)
    levelNode: cc.Node = null;
    @property(cc.Node)
    mainNode: cc.Node = null;
    // LIFE-CYCLE CALLBACKS:
    isLoading = false;

    // add 20231211
    @property(cc.Node)
    tt_logo: cc.Node = null;
    @property(cc.Node)
    tt_lingqufuli: cc.Node = null;
    @property(cc.Node)
    wx_logo: cc.Node = null;
    @property(cc.Node)
    loading: cc.Node = null;

    @property(cc.Node)
    tianjiashouye: cc.Node = null;
    @property(cc.Node)
    dingyue: cc.Node = null;
    @property(cc.Node)
    paihangbang: cc.Node = null;

    public static ins: MainScene = null;
    onLoad() {
        if (AppPlatform.is_WECHAT_GAME_custom()) {
            this.wx_logo.active = true;
            this.tt_lingqufuli.active = false;
            this.tt_logo.active = false;
            this.tianjiashouye.active = false
            this.dingyue.active = false
            this.paihangbang.active = false

        }
    }
    start() {
        MainScene.ins = this;

        TTAPI.createGridGamePanel()

        AppPlatform.PlatformOnShow(() => {

            Common5.onHideState = {
                gold: 0, //金币
                strength: 0,  //体力
                sendCardTimes: 0, //发牌次数
                unlockLevel: 0, //已经解锁关卡/槽位
                removeTimes: 0, //消除次数
                default1: 0, //默认字段1
                default2: 0, //默认字段2
                default3: 0 //默认字段3
            }
            GameReport.setLoadingTime((new Date()).getTime());
        })

        AppPlatform.PlatformOnHide(() => {
            let loadingFinishTime = (new Date()).getTime()
            let loadingTime = GameReport.getLoadingTime()
            let staytime = (loadingFinishTime - loadingTime) / 1000
            Common5.onHideState.default1 = staytime

            Common5.onHideState.unlockLevel = User.getUnlockLevel()
            let curGameName = '主界面'
            if (Common5.selectGameInfo) {
                curGameName = Common5.selectGameInfo.title2
            }
            GameReport.OnHideReport2('onHide', Common5.onHideState, curGameName)
        })

        GameReport.EnterReport(ENTERTYPE.XUANGUAN, '主界面')
        // cc.tween(this.dancerStatic).delay(1).to(2,{scale:1}).start();
        // cc.tween(this.node.getChildByName("logo")).delay(1).to(1,{scale:1}).delay(2).to(1,{opacity:0}).call(()=>{
        //     this.dancerAnimatio.active = true;
        //     this.dancerStatic.active = false;
        //     this.soundBoxAnimatio.active = true;
        //     this.soundBoxStatic.active = false;
        //     this.light.active = true;
        // }).start();

        this.getUserId()

    }
    showLoading() {
        this.loading.active = true;
    }
    hidLoading() {
        MainScene.ins.isLoading = false;
        this.loading.active = false;
    }
    startGame() {
        this.GameNode.active = false;
        this.levelNode.opacity = 255;
        this.mainNode.active = false;
    }
    toMain() {
        this.GameNode.active = false;
        this.levelNode.opacity = 0;
        this.mainNode.active = true;
    }
    showClickEffect(pos) {
        let effectNode = cc.instantiate(this.clickEffect);
        effectNode.active = true;
        effectNode.setParent(this.node);
        effectNode.setPosition(this.node.convertToNodeSpaceAR(pos));
        //console.log(pos)
        this.scheduleOnce(() => {
            effectNode.destroy();
        }, 0.3)
    }


    getCurlevel() {
        let level = -1;
        if (User.getFirstIn()) {
            level = 0;
        }
        else {
            level = User.getLeveNum_custom();
            let successArr = User.getSuccessLevels();
            for (let i = 0; i < successArr.length; i++) {
                if (level == successArr[i]) {
                    level = -1;
                    break;
                }
            }
        }
        return level;
    }

    lingQuFuLiClickEvent() {
        Common5.playEffect("ui")
        GameReport.EnterReport(ENTERTYPE.XUANGUAN, '领取福利');
        Common5.getPrefabFromBundle("ZoomGame", "prefab/view/cebianlan", null, (node) => {
        })
    }

    zuomianClickEvent(Event) {
        Common5.playEffect("ui");
        TTAPI.addToZM();
    }

    dingyueClickEvent(Event) {
        Common5.playEffect("ui");
        TTAPI.dingyueXiaoxi();
    }

    paihangbangClickEvent(Event) {
        Common5.playEffect("ui");
        TTAPI.getImRankList();
    }


    getUserId() {
        // console.error('获取数据++++1')

        let onFail = () => {

        }

        let onSuccess = (response) => {
            console.log(response, '数据+++=====')
            let userid = User.getUserId()
            console.error(response, userid, '数据+++=====')
            if (response.msg == 'OK') {
                User.setUserId(response.openid)
                //
                console.log('获取服务器数据222222')
                let onSuccesscall = (GameData) => {
                    User.readDataByUrl(GameData)

                    this.scheduleOnce(() => {
                        EventMgr.emitEvent_custom(ryw_Event.updateLevel);
                    }, 0.3)
                }

                let onFailcall = () => {
                    console.log('获取服务器数据3333失败')
                }

                CommonDefine.getServerData(onSuccesscall, onFailcall)

            } else {
                if (userid) {
                    console.log('有userid再次获取数据')
                    let onSuccesscall = (GameData) => {
                        User.readDataByUrl(GameData)
                        this.scheduleOnce(() => {
                            EventMgr.emitEvent_custom(ryw_Event.updateLevel);
                        }, 0.3)
                    }
                    let onFailcall = () => {

                    }
                    CommonDefine.getServerData(onSuccesscall, onFailcall)
                } else {
                    //重新获取code获取userid
                    console.log('重新获取code获取userid')
                    CommonDefine.replayGetLoginInfo(
                        () => {

                            let onSuccesscall = (GameData) => {
                                User.readDataByUrl(GameData)
                                this.scheduleOnce(() => {
                                    EventMgr.emitEvent_custom(ryw_Event.updateLevel);
                                }, 0.3)
                            }
                            let onFailcall = () => {

                            }
                            CommonDefine.getServerData(onSuccesscall, onFailcall)
                        }
                    )
                }
            }
        }

        if (!AppPlatform.is_TT_GAME_custom()) {

        } else {

            let userid = User.getUserId()
            if (userid == '') {
                if (CommonDefine.loginCode) {

                    console.log('获取服务器数据没有userid')
                    CommonDefine.getLoginInfo(CommonDefine.loginCode, onFail, onSuccess)
                }
            } else {
                //获取服务器数据
                console.log('获取服务器数据有userid')
                let onSuccesscall = (GameData) => {
                    User.readDataByUrl(GameData)

                    this.scheduleOnce(() => {
                        EventMgr.emitEvent_custom(ryw_Event.updateLevel);
                    }, 0.3)
                }

                let onFailcall = () => {

                }

                CommonDefine.getServerData(onSuccesscall, onFailcall)

            }




        }
    }
    // update (dt) {}
}