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 LiXianShouYi from "../GameMethodRes/LiXianShouYi/LiXianShouYi";
// import LiXianShouYiManager from "../GameMethodRes/LiXianShouYi/LiXianShouYiManager"; 
import JuQingManager from "../JuQingChat/JuQingManager";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import BagManager from "../Manager/BagManager";
import UserManager from "../Manager/UserManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import GetAward from "../SCommon/GetAward";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FirstSelectScene extends cc.Component {

    @property(cc.Node)
    backgroud: cc.Node = null;
    @property(cc.Node)
    mainLayer: cc.Node = null;
    @property(cc.Node)
    prefabLayer: cc.Node = null;

    // onLoad () {}

    start() {
        // Common5.preloadRemoteAudio('CommonSound/主场景')
        // Common5.preloadRemoteAudio('CommonSound/投资人家')
        // Common5.preloadRemoteAudio('CommonSound/西餐厅内')
        // Common5.preloadRemoteAudio('CommonSound/西餐厅外')



        // Common5.playRemoteAudioMusic('CommonSound/主场景')
        this.initComponent();
    }

    initComponent() {
        this.getServerData()

        // JuQingManager.unLockNewJuQing('WX_QL1201')
        // User.setCurGameLevelIndex(2)
        // // TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_210)
        // let baDaCaiXiUnlockStatus = User.getBaDaCaiXiUnlockStatus()
        // baDaCaiXiUnlockStatus.XiangCai = true
        // baDaCaiXiUnlockStatus.ChuanCai = true
        // baDaCaiXiUnlockStatus.SuCai = true
        // baDaCaiXiUnlockStatus.HuiCai = true
        // baDaCaiXiUnlockStatus.ZheCai = true
        // baDaCaiXiUnlockStatus.MinCai = true
        // baDaCaiXiUnlockStatus.YueCai = true
        // baDaCaiXiUnlockStatus.LuCai = true
        // User.setBaDaCaiXiUnlockStatus(baDaCaiXiUnlockStatus)
        // let bagConfig = {
        //     goodId:3098,
        //     goodNum:1
        // }
        // BagManager.addBagList(bagConfig)

        // PrefabManage.loadPrefabByType(GameType.FirstSelectScene_LevelView, this.mainLayer, () => {
        //     PrefabManage.loadPrefabByType(GameType.FirstSelectScene_LeftBtnUI, this.mainLayer, () => {
        //         PrefabManage.loadPrefabByType(GameType.FirstSelectScene_CloudAnimation, this.backgroud);
        //     });
        // });
        this.scheduleOnce(() => {
            //先preload必要的
            // let needLoadArr = [GameType.CommonTextTips, GameType.GetAward, GameType.TaskInfoNode, 
            //     GameType.JuQingDialog, GameType.ChatList, GameType.Setting, GameType.BeiBao,GameType.CityPrefab,
            //     GameType.HuangJinYuFu,GameType.KuaiDiZhan, GameType.Paimaihang,GameType.ShouHuoji,GameType.GuPiao,
            //     GameType.HuXinGongYuan,GameType.KaiKuangFirstView,GameType.ChoushuiFirst,GameType.CiShanChouJiang
            // ]
            // PrefabManage.preloadPrefabArray(needLoadArr)
        })
        // this.scheduleOnce(() => {
        //     let liXianMoney = LiXianShouYiManager.getLiXianShouYi()
        //     if (liXianMoney > 0) {
        //         PrefabManage.loadPrefabByType(GameType.LiXianShouYi, null, (prefabNode) => {
        //             prefabNode.getComponent(LiXianShouYi).initView(liXianMoney)
        //         })
        //     }
        // }, 0.5)
        if (User.getFirstStepIndex() == 0) {
            PrefabManage.loadPrefabByType(GameType.FirstSelectScene_LevelView, this.mainLayer, (levelView) => {
                (levelView as cc.Node).opacity = 0;
                PrefabManage.loadPrefabByType(GameType.FirstSelectScene_StartAnimation, this.prefabLayer, (startAnimation) => {
                    PrefabManage.loadPrefabByType(GameType.FirstSelectScene_LeftBtnUI, this.mainLayer, () => {
                        PrefabManage.loadPrefabByType(GameType.FirstSelectScene_CloudAnimation, this.backgroud);
                    });
                    cc.tween(startAnimation).delay(1.7)
                        .call(() => {
                            (levelView as cc.Node).opacity = 255;
                        }).removeSelf().start();
                });
            });
        } else {
            PrefabManage.loadPrefabByType(GameType.FirstSelectScene_LevelView, this.mainLayer, () => {
                PrefabManage.loadPrefabByType(GameType.FirstSelectScene_LeftBtnUI, this.mainLayer, () => {
                    PrefabManage.loadPrefabByType(GameType.FirstSelectScene_CloudAnimation, this.backgroud);
                });
            });
        }
    }

    // 测试代码
    onClickTest() {
        let input = this.node.getChildByName("输入").getComponent(cc.EditBox).string;
        if (input && input.length > 0) {
            let inputs = input.split("_");
            if (inputs[0] == "level") {
                let level = parseInt(inputs[1]);
                if (level && level >= 1 && level <= 25) {
                    TaskManager.setCurUnLockMainTaskInfo(level * 100 + 1);
                    User.setCurGameLevelIndex(level - 1);
                    cc.director.loadScene("FirstSelectScene");
                } else {
                    PrefabManage.showTextTips("输入的关卡不存在!");
                }
            } else if (inputs[0] == "bag") {
                let goodsId = parseInt(inputs[1]);
                if (goodsId != null) {
                    let property = BagManager.getGoodsProperty(goodsId);
                    if (property) {
                        let goodArray = [{ goodId: goodsId, goodNum: 1 }];
                        PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
                            prefabNode.getComponent(GetAward).initView(goodArray);
                        });
                        return;
                    }
                }
                PrefabManage.showTextTips("输入的物品不存在!");
            } else {
                PrefabManage.showTextTips("输入格式不正确!");
            }
        }
    }
    getServerData() {
        if (!AppPlatform.is_TT_GAME_custom()) {
            //登录获取
        } else {
            let userID = User.getUserId()
            console.error(userID, '读取数据userID')

            if (userID == '') {
                //读取数据
                let onFail = () => { }
                let onSuccess = (response) => {
                    console.log(response, '登录成功获取userid++1+=====')
                    if (response.msg == 'OK') {
                        User.setUserId(response.openid)
                        User.setFirstIn(false);
                        console.warn('获取服务器数据完成')
                        let comSuccesscall = (GameData) => {
                            if (GameData) {
                                UserManager.readDataByUrl(GameData)
                            }
                            this.scheduleOnce(() => {
                                console.warn('邵阳再次load++==FirstSelectScene', GameData)
                                cc.director.loadScene("FirstSelectScene");
                            }, 0.5)
                        }

                        let comFailcall = () => {

                        }
                        CommonDefine.getServerData(comSuccesscall, comFailcall)
                    }
                }

                let appsuccesscall = (code) => {
                    console.error(code, '没有code获取code22')
                    CommonDefine.getLoginInfo(code, onFail, onSuccess)
                }
                let appfailcall = () => {
                    //
                    console.error('appfailcall')
                }
                AppPlatform.loginPlatform_custom(appsuccesscall, appfailcall)
                console.error('读取数据22')
            } else {
                console.log('不需要读取数据')
            }
        }
    }
    // update (dt) {}
}