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

const {ccclass, property} = cc._decorator;


@ccclass
export default class KaiKuangMainView extends cc.Component {

    @property(cc.Node)
    mountingNode:cc.Node = null
    @property(cc.Node)
    tanCeSpine:cc.Node = null
    @property(cc.Node)
    btn_kanTan:cc.Node = null 
    @property(cc.Node)
    btn_kanTanAd:cc.Node = null 
    @property(cc.Node)
    tanKuangNode:cc.Node = null 

    curMountingConfig = null
    curMineralsNum = 1
    curMineralsConfig = null
    awardPrice:number = 0
    //awardPrice:number = 0
    //mountingPrice:number = 0
    onLoad () {
        this.curMountingConfig = KaiKuangDataManage.getCurMountingConfig()
        //this.mountingPrice = KaiKuangDataManage.getCurMountingPrice()
        if(this.curMountingConfig.mountingEffectType == "Sprite"){
            this.mountingNode.getChildByName("mountingSpine").active = false
            this.mountingNode.getChildByName("mountingSpFrame").active = true
            //Common5.getSpriteFrameFromBundle("GameRes","kaiKuang/texture/shan/"+this.curMountingConfig.effectPath,this.mountingNode.getChildByName("mountingSpFrame").getComponent(cc.Sprite));
       
            Common5.addUrlSprite_custom('kuangshan/shan/'+this.curMountingConfig.effectPath, this.mountingNode.getChildByName("mountingSpFrame").getComponent(cc.Sprite))

        }else if(this.curMountingConfig.mountingEffectType == "Spine"){
            // this.mountingNode.getChildByName("mountingSpine").active = true
            // this.mountingNode.getChildByName("mountingSpFrame").active = false
            // Common5.getSpineFromBundle("GameRes","kaiKuang/texture/shan/"+this.curMountingConfig.effectPath,this.mountingNode.getChildByName("mountingSpine"),(node)=>{
            //     this.mountingNode.getChildByName("mountingSpine").addChild(node)
            //     node.getComponent(sp.Skeleton).setAnimation(0, 'animation', true)
            // });
        }
        // User.setKuaiDiMangHeNum(User.getKuaiDiMangHeNum() - 1)
        let freeTimes = User.getKaiKuangFreeNum()
        if(freeTimes >0){
            this.btn_kanTan.active = true
            this.btn_kanTan.getChildByName("mountingNameStr").getComponent(cc.Label).string = "开采("+freeTimes +"/3)"
            this.btn_kanTanAd.active = false
        }else{
            this.btn_kanTanAd.active = true
            this.btn_kanTan.active = false
        }
    }

    onKanTanBtnClick(){
        //Common5.playEffect("sound/按键点击")
        // this.awardPrice = 
        if(this.curMountingConfig.mountingPrice > User.getMoney()){
            Common5.showTips_customTime('余额不足挣钱去吧',1)
            return
        }else{
            UserManager.subMoney(this.curMountingConfig.mountingPrice)
        }

        let kaikuang = 1
        User.setKaikuangNum(kaikuang)
        
        EventMgr.emitEvent_custom(ryw_Event.Event_hideMounting)
        KaiKuangDataManage.saveDataToStorage(this.curMountingConfig.id,true)
        Common5.ReportDY("inLevel", '富豪俱乐部-开采按钮');
        User.setKaiKuangFreeNum(User.getKaiKuangFreeNum() - 1)
        //this.curMineralsNum = KaiKuangDataManage.getKanTanMineralsNum()
        
        this.curMineralsConfig = KaiKuangDataManage.getKanTanMineralsConfig()
        if(this.curMineralsConfig){
            this.awardPrice = this.curMineralsConfig.mineralsPrice
        }
       


        this.btn_kanTan.active = false
        this.btn_kanTanAd.active = false
        Common5.playEffect("扫描")
        this.tanCeSpine.active = true
        this.tanCeSpine.getComponent(sp.Skeleton).setAnimation(0, 'animation', false)
        cc.tween(this.node)
            .delay(2)
            .call(()=>{
                this.tanCeSpine.getComponent(sp.Skeleton).setAnimation(0, 'jingzhi', false)
                this.showTanKuangActive()
            })
        .start()
    }
    onKanTanADBtnClick(){
        //Common5.playEffect("sound/按键点击")
        let tab = {
            onClose: (finish)=>{
                if (finish) {
                    Common5.kaiKuangVDIndex++
                    Common5.ReportDY("inLevel", '开矿山-AD-增加开采次数');
                    this.btn_kanTanAd.active = false
                    this.btn_kanTan.active = true
                    User.setKaiKuangFreeNum(3)
                    this.btn_kanTan.getChildByName("mountingNameStr").getComponent(cc.Label).string = "开采(3/3)"
                }
                else{
                    Common5.showTips_custom("广告未观看完");
                }
            },onFailed:()=>{

            }
        }
        AppPlatform.playVideo_custom(tab)  
    }
    showTanKuangInfo(){
        console.log('开矿showTanKuangInfo123')
        if(this.curMineralsConfig){
            this.showTanKuangKS()
        }else{
            let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
            let mainId = mainTaskInfo.Id
            let goodArray =  null
            if(mainId == MainTaskIdEnum.MainTask_535){
               
                let array = [ 1711,1712,1713,1714,1715]
                for(let i = 0;i<array.length;i++){
                    if(BagManager.getBagGoodNums(array[i])<=0){
                        goodArray =  [{goodId:array[i],goodNum: 1}] // 
                    }
                }
               
               

            }

    
            PrefabManage.loadPrefabByType(GameType.GetAward,null,(prefabNode)=>{
                prefabNode.getComponent(GetAward).initView(goodArray, ()=>{
                    if(mainId == MainTaskIdEnum.MainTask_535){
                        let haveAll = BagManager.isHaveAllGood([1711,1712,1713,1714,1715])
                        if(haveAll){
                            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_535)
                            User.setRoomGoodStatus(23)
                            TaskManager.setCurTask(MainTaskIdEnum.MainTask_536)
                            //box
                            PrefabManage.loadPrefabByType(GameType.BoxTipNode, null, (prefab)=>{
                                prefab.getComponent('BoxTipScript').setViewData(1,'回家融合五行源石', ()=>{
                                    this.node.removeFromParent()
                                    this.node.destroy()
                                    PrefabManage.loadPrefabByType(GameType.MyRoom)
                                })
                                prefab.getComponent('BoxTipScript').setBtnStr('确定')
                                prefab.getComponent('BoxTipScript').setCloseBtnView(false)
                                prefab.zIndex = 99
                            })
                        }else{
                            this.node.removeFromParent()
                            this.node.destroy()
                        }
                    }
                });
            },true)
         
        }
        
        

    }

    showTanKuangKS(){
        this.tanKuangNode.active = true
        let kuangShiNode = this.tanKuangNode.getChildByName("kuangShiNode")
    
        
        kuangShiNode.getChildByName("kuangShiSpFrame").active = true
        //Common5.getSpriteFrameFromBundle("GameRes","kaiKuang/texture/kuangshi/"+this.curMineralsConfig.effectPath,kuangShiNode.getChildByName("kuangShiSpFrame").getComponent(cc.Sprite));
        Common5.addUrlSprite_custom('kuangshan/kuangshi/'+this.curMineralsConfig.effectPath, kuangShiNode.getChildByName("kuangShiSpFrame").getComponent(cc.Sprite))

        //kuangShiNode.scale = 0
        let nameStr = this.tanKuangNode.getChildByName("allNode").getChildByName("di").getChildByName("nameStr")
        let descStr = this.tanKuangNode.getChildByName("allNode").getChildByName("box").getChildByName("descStr")
        let moneyStr = this.tanKuangNode.getChildByName("allNode").getChildByName("di").getChildByName("moneyStr")
        let btn_bag = this.tanKuangNode.getChildByName("allNode").getChildByName("btn_bag")
        let btn_sale = this.tanKuangNode.getChildByName("allNode").getChildByName("btn_sale")
        let peiIcon = this.tanKuangNode.getChildByName("allNode").getChildByName("peiIcon")
        nameStr.getComponent(cc.Label).string = this.curMineralsConfig.mineralsName
        descStr.getComponent(cc.Label).string = this.curMineralsConfig.mineralsDesc
        moneyStr.getComponent(cc.Label).string = "价值:" +Common5.getNumberChangeHanzi(this.awardPrice,'1',1)
        btn_bag.active = false
        btn_sale.active = true
        
        peiIcon.active = this.curMineralsConfig.isFeiKuang
    }

    showTanKuangActive(){
        this.showTanKuangInfo()
    }

    onBtnSaleClick(){
        //Common5.playEffect("sound/按键点击")
        // let kuangShiNode = this.tanKuangNode.getChildByName("kuangShiNode")
        UserManager.addMoney(this.awardPrice,this.node)
        // Common5.showMoneyFlyParticle(kuangShiNode)
        this.node.removeFromParent()
        this.node.destroy()
        // Common5.removeSubBackNode(this.node)

    }

    
}