import PrefabManage,{ GameType } from "../PrefabManager/PrefabManage";
import Common5 from "../../Platform/th/Common5";
import User from "../../FrameWork/User/User";
import GameBase from "../SCommon/GameBase";
import InterfaceManager from "../Manager/InterfaceManager";
import GetAward from "../SCommon/GetAward";
import UserManager from "../Manager/UserManager";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import GameReport from "../../FrameWork/Report/ZyZyReport";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import BagManager from "../Manager/BagManager";
import ShipuManager from "../Manager/ShipuManager";
import ProbabilityManager from "../Manager/ProbabilityManager";

const {ccclass, property} = cc._decorator;

enum PropType{
    ShengZi,
    GongNu,
    LieQiang,
    Wang
}
let RateConfig=[
    {propType:PropType.ShengZi,nameStr:"绳索",moneyNum:20},
    {propType:PropType.GongNu,nameStr:"弓弩",moneyNum:180},
    {propType:PropType.LieQiang,nameStr:"猎枪",moneyNum:800},
    {propType:PropType.Wang,nameStr:"网",moneyNum:0}
]


@ccclass
export default class ShouLieChang extends GameBase {


    @property(cc.Node)
    btn_start: cc.Node = null;
    @property(cc.Node)
    btn_startFiveTimes: cc.Node = null;


    @property(cc.Node)
    taosheng: cc.Node = null;
    @property(cc.Node)
    gongjian: cc.Node = null;
    @property(cc.Node)
    bushouwang: cc.Node = null;
    @property(cc.Node)
    qiang: cc.Node = null;


    curPropId:PropType = PropType.GongNu
    isCanStart:boolean = true
    maskGuideNode:cc.Node = null
    onLoad () {
        super.onLoad()
    }

 
    start () {
        Common5.playRemoteAudioMusic('sound/common/狩猎背景')
        //Common5.playMusic("sound/狩猎背景");
        this.initView()

    }
    


    initView(){

        this.setBtnNumberStr()
        // this.refreshTaskDesc()
        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        
        if(mainId == MainTaskIdEnum.MainTask_505){
            ProbabilityManager.setShoulieCommonFixFixIndex(1)
            //bottomBtnNode btn_start
            let node = this.node.getChildByName('bottomBtnNode').getChildByName('btn_start')
            this.guideView([node])

        }

    }

    onBtnStartClick(){
        console.log("当前道具id==",this.curPropId)
        if(!this.isCanStart){
            return
        }
        this.isCanStart = false
        
        let type = 'ShengZi'
        if(this.curPropId == PropType.ShengZi){
            type = 'ShengZi'
        }else if(this.curPropId == PropType.GongNu){
            type = 'GongNu'
        }else if(this.curPropId == PropType.LieQiang){
            type = 'LieQiang'
        }else if(this.curPropId == PropType.Wang){
            type = 'Wang'
        }
        let goodArray = InterfaceManager.getMangheGoodsListByType('ShouLie',1,false,type)



        if(this.curPropId == PropType.Wang){
            
        }else if(this.curPropId == PropType.ShengZi){
            
        }else if(this.curPropId == PropType.GongNu){
            // if(!this.subMoney()){
            //     this.isCanStart = true
            //     return
            // }


            this.gongjian.getComponent(sp.Skeleton).setAnimation(0, 'fashe', false)
            cc.tween(this.node)
                .delay(1.3)
                .call(()=>{
                    Common5.playRemoteAudioEffect('sound/common/弓弩')
                   
                })
                .delay(0.9)
                .call(()=>{
                    this.getAward(goodArray)
                    this.gongjian.getComponent(sp.Skeleton).setAnimation(0, 'jingzhi', false)
                })
                .delay(1)
                .call(()=>{
                    // this.isCanStart = true
                })
            .start()
        }else if(this.curPropId == PropType.LieQiang){
           
        }
    }
    onBtnStartFiveTimesClick(){
        if(!this.isCanStart ){
            return
        }
        console.log("当前道具id==",this.curPropId)

        let tab = {
            onClose: (finish)=>{
                if (finish) {
                    Common5.ReportDY("inLevel", "长白山-AD-五连发");
                    this.reportKey(()=>{
                        GameReport.ADReport('长白山五连发',1)
                    })
                    this.startFiveTimes()
                }
                else{
                    this.reportKey(()=>{
                        GameReport.ADReport('长白山五连发',0)
                    })
                    Common5.showTips_custom("广告未观看完");
                }
            },onFailed:()=>{
                
            }
        }
        AppPlatform.playVideo_custom(tab)

    }
    startFiveTimes(){
        this.isCanStart = false
        let type = 'ShengZi'
        if(this.curPropId == PropType.ShengZi){
            type = 'ShengZi'
        }else if(this.curPropId == PropType.GongNu){
            type = 'GongNu'
        }else if(this.curPropId == PropType.LieQiang){
            type = 'LieQiang'
        }else if(this.curPropId == PropType.Wang){
            type = 'Wang'
        }
        let goodArray = InterfaceManager.getMangheGoodsListByType('ShouLie',5,true,type)


        if(this.curPropId == PropType.ShengZi){
       
        
        }else if(this.curPropId == PropType.GongNu){
       
            this.gongjian.getComponent(sp.Skeleton).setAnimation(0, 'wulian', false)
            cc.tween(this.node)
                .delay(1.3)
                .call(()=>{
                    Common5.playRemoteAudioEffect('sound/common/弓弩')
                })
                .delay(0.9)
                .call(()=>{
                    this.getAward(goodArray)
                    this.gongjian.getComponent(sp.Skeleton).setAnimation(0, 'jingzhi', false)
                })
              
            .start()
        }else if(this.curPropId == PropType.LieQiang){
        
           
        }
    }
    setBtnNumberStr(){
        this.taosheng.active = false
        this.gongjian.active = false
        this.bushouwang.active = false
        this.qiang.active = false

        let moneyNum = 0
        let nameStr = ""
        for(var i=0;i<RateConfig.length;i++){
            if(this.curPropId == RateConfig[i].propType){
                moneyNum = RateConfig[i].moneyNum
                nameStr = RateConfig[i].nameStr
                break
            }
        }
        if(this.curPropId == PropType.Wang){
            this.btn_startFiveTimes.active = false
            this.bushouwang.active = true
            let leftTimes = User.getSLCWangAd()
            if(leftTimes <= 0){
                this.btn_start.getChildByName("moneyStr").getComponent(cc.Label).string = "免费次数0"
          

            }else{
        
                this.btn_start.getChildByName("moneyStr").getComponent(cc.Label).string = `免费次数${leftTimes}/5`
                // this.bushouwang.active = true
            }
        }else if(this.curPropId == PropType.ShengZi){
            this.btn_start.getChildByName("moneyStr").getComponent(cc.Label).string = `${nameStr}${moneyNum}`
            this.taosheng.active = true
            this.btn_startFiveTimes.active = true
        }else if(this.curPropId == PropType.GongNu){
            this.btn_start.getChildByName("moneyStr").getComponent(cc.Label).string = '狩猎'//`${nameStr}${moneyNum}`
            this.gongjian.active = true
            this.btn_startFiveTimes.active = true
        }else if(this.curPropId == PropType.LieQiang){
            this.btn_start.getChildByName("moneyStr").getComponent(cc.Label).string = `${nameStr}${moneyNum}`
            this.qiang.active = true
            this.btn_startFiveTimes.active = true
        }

    }
    guideView(nodeArray,opacity = -1){
        PrefabManage.loadPrefabByType(GameType.GuideMskNode, null, (prefab)=>{
            // prefab.zIndex = 199
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
            if(opacity!=-1){
                prefab.getComponent(NewGuideScript).setOpacityMaskNode(opacity)
            }
        })
    }
    guideStep(goodArray){
        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        
        if(mainId == MainTaskIdEnum.MainTask_505){
            if(BagManager.isHaveAllGood([1703])){
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_505)
                TaskManager.setCurTask(MainTaskIdEnum.MainTask_506)
                //跳转
                PrefabManage.loadPrefabByType(GameType.BoxTipNode, null, (prefab)=>{
                    prefab.getComponent('BoxTipScript').setViewData(1,'回家交药材给妹妹治疗', ()=>{
                        this.node.removeAllChildren()
                        this.node.destroy()
                        PrefabManage.loadPrefabByType(GameType.MyRoom)
                    })
                    prefab.getComponent('BoxTipScript').setBtnStr('确定')
                    prefab.getComponent('BoxTipScript').setCloseBtnView(false)
                    prefab.zIndex = 100
                })

            }
        }

        
        
    }
    //获取随机的奖励物品
    getAward(goodArray){
        
        console.log(goodArray,'获取随机的奖励物品===')
        // let isHavePro = false
        // if(goodArray.length>1){
        //     if(BagManager.getBagGoodNums(1723)<=0 && User.getMyLevel()>=400){
        //         for(let i = 0; i<goodArray.length;i++){
        //             let dataArray = goodArray[i]
        //             if(dataArray.goodId>=1701 && dataArray.goodId<=1750 ){

        //             }else{
        //                 dataArray.goodId = 1723
        //                 isHavePro = true
        //                 break
        //             }
        //         }
        //     }
        // }
        PrefabManage.loadPrefabByType(GameType.GetAward,null,(prefabNode)=>{
            prefabNode.getComponent(GetAward).initView(goodArray, ()=>{
                this.isCanStart = true
                this.guideStep(goodArray)
                //引导回家提交物品
                // if(isHavePro){
                //     let jiesuoId = 2
                //     ShipuManager.shipuUnLockById(jiesuoId)
                // }
                //ShipuManager.setShipuUnLock()
            });
        },true)

    }
    subMoney(){
        let moneyNum = 0
        for(var i=0;i<RateConfig.length;i++){
            if(this.curPropId == RateConfig[i].propType){
                moneyNum = RateConfig[i].moneyNum
                break
            }
        }
        if(moneyNum > UserManager.getCurMoney()){
            PrefabManage.showTextTips("余额不足,请去地摊或者商人处换钱再来!");
            return false
        }
        UserManager.subMoney(moneyNum)
        return true
    }
    onDestroy(){
        Common5.playRemoteAudioMusic('sound/common/背景音')

        EventMgr.emitEvent_custom(ryw_Event.updateDaoYe); // 刷新按钮
        super.onDestroy()
    }

}