import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import User from "../../../FrameWork/User/User";
import AppPlatform from "../../../FrameWork/Util/AppPlatform";
import Common5 from "../../../Platform/th/Common5";
import KaiKuangDataManage from "../../../Scripts/Manager/KaiKuangDataManage";
import PrefabManage, { GameType } from "../../../Scripts/PrefabManager/PrefabManage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class KaiKuangFirstView extends cc.Component {

    @property(cc.Node)
    itemTopNode:cc.Node = null
    @property(cc.Node)
    itemMidNode:cc.Node = null
    @property(cc.Node)
    itemBottomNode:cc.Node = null
    @property(cc.Node)
    introductionBox:cc.Node = null
    
    curSelectItem:cc.Node = null

    onLoad () {
        //确认勘探之后再隐藏该座山峰
        EventMgr.onEvent_custom(ryw_Event.changeScene,()=>{
            console.log('changeScene++=KaiKuangFirstView')
            this.node.removeFromParent()
            this.node.destroy()
        },this)
        
        EventMgr.onEvent_custom(ryw_Event.Event_hideMounting, () => {
            if(this.curSelectItem){
                this.curSelectItem.active = false
            }
        }, this);

        let callFunc = ()=>{
            this.initContent()
        }
        if(!Common5.AllMountingConfig){
            cc.loader.loadRes('Json/kaiKuangConfig/AllMountingConfig.json', function (err, gameconfig) {
                if (err) {
                    console.log(err);
                    return;
                }
                Common5.AllMountingConfig = gameconfig.json;
                console.log("山峰配置加载成功");
                callFunc()
            });
        }else{
            callFunc()
        }

    }


    protected start(): void {
        this.scheduleOnce(()=>{
            PrefabManage.preloadPrefabByType(GameType.KaiKuangMainView)
        },0.1)
        
    }

    initContent(){
        let allMounting = KaiKuangDataManage.getMountingInfo()
        console.log("获取allMounting==",allMounting)

        this.initContentByTypeStr(this.itemTopNode,allMounting["高"])
        this.initContentByTypeStr(this.itemMidNode,allMounting["中"])
        this.initContentByTypeStr(this.itemBottomNode,allMounting["低"])
    }
    initContentByTypeStr(parentNode,idArr){
        for(var i=0;i<idArr.length;i++){
            let mountingConfig = KaiKuangDataManage.getMountingConfigById(idArr[i].id)
            let item = parentNode.getChildByName("item_"+i)
            if(idArr[i].isFinish){
                item.active = false
            }else{  
                item.active = true
                item.getChildByName("mountingNameStr").getComponent(cc.Label).string = mountingConfig.mountingName
                item.getChildByName("mountingPrice").getComponent(cc.Label).string = Common5.getNumberChangeHanzi(mountingConfig.mountingPrice,'1',1)+""
                
                Common5.addUrlSprite_custom('kuangshan/icon/'+mountingConfig.iconName, item.getChildByName("spFrame").getComponent(cc.Sprite))


                //Common5.getSpriteFrameFromBundle("GameRes","kaiKuang/texture/icon/"+mountingConfig.iconName,item.getChildByName("spFrame").getComponent(cc.Sprite));
                item.attr({mountingConfig:mountingConfig})
            }
        }
    }
    onBtnRefreshClick(){
        Common5.playEffect("ui")
        let tab = {
            onClose: (finish)=>{
                if (finish) {
                    Common5.kaiKuangVDIndex++
                    
                    Common5.ReportDY("inLevel", '开矿山-AD-刷新开采证');
                    KaiKuangDataManage.clearAllRecord()
                    this.initContent()
                }
                else{
                    Common5.showTips_custom("广告未观看完");
                }
            },onFailed:()=>{

            }
        }
        AppPlatform.playVideo_custom(tab)


    }
    
    onItemClick(event){
        Common5.playEffect("ui")
        this.curSelectItem = event.target
        this.introductionBox.active = true
        this.introductionBox.getChildByName("tittleStr").getComponent(cc.Label).string = event.target.mountingConfig.mountingName+'开采权'
        this.introductionBox.getChildByName("introduceStr").getComponent(cc.Label).string = event.target.mountingConfig.mountingDesc
        this.introductionBox.getChildByName("moneyStr").getComponent(cc.Label).string = "开采费:"+ Common5.getNumberChangeHanzi(event.target.mountingConfig.mountingPrice,'1',1)
       
        Common5.proLoadPicBuyUrl('kuangshan/shan/'+event.target.mountingConfig.effectPath)
        KaiKuangDataManage.setCurMountingConfig(event.target.mountingConfig)
    }
    onBtnKaiCai(){
        Common5.playEffect("ui")
        let curMountingConfig = KaiKuangDataManage.getCurMountingConfig()
        
        if(curMountingConfig.mountingPrice > User.getMoney()){
            Common5.showTips_customTime('余额不足挣钱去吧',1)
            return
        }else{
            // Common5.subMoney(this.expendMoney)
        }
        // KaiKuangDataManage.saveDataToStorage(curMountingConfig.id,true)
        // this.curSelectItem.active = false
        
        this.introductionBox.active = false
        // Common5.subMoney(curMountingConfig.mountingPrice)
        let callFunc = ()=>{
            PrefabManage.loadPrefabByType(GameType.KaiKuangMainView)
        }

        if(Common5.AllMineralsConfig){
            callFunc()
        }else{
            cc.loader.loadRes('Json/kaiKuangConfig/AllMineralsConfig.json', function (err, gameconfig) {
                if (err) {
                    console.log(err);
                    return;
                }
                Common5.AllMineralsConfig = gameconfig.json;
                console.log("矿物加载成功");
                callFunc()
            });
        }
        
    }
    onBtnClose(){
        Common5.playEffect("ui")
        this.introductionBox.active = false
    }
}