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

const {ccclass, property} = cc._decorator;


@ccclass
export default class LaJiZhan extends GameBase {
    @property(cc.Node)
    leSeSpine:cc.Node = null

    @property(cc.Node)
    btn_fanYiFan:cc.Node = null

    @property(cc.Node)
    btn_shuaXin:cc.Node = null

    // @property(cc.Node)
    // qipao:cc.Node = null
    
    @property(cc.Label)
    daojishiLab:cc.Label = null

    @property(cc.Label)
    daojishiLab2:cc.Label = null
    
    @property(cc.Node)
    freeTimeArea:cc.Node = null

    @property(cc.Label)
    daojishiFan:cc.Label = null
    @property(cc.Label)
    freeTimeFan:cc.Label = null

    
    @property(cc.Node)
    iconNode:cc.Node = null

    @property(cc.Node)
    iconMoneyNode:cc.Node = null

    @property(cc.Node)
    goodsEndArea1:cc.Node = null

    @property(cc.Node)
    goodsEndArea2:cc.Node = null

    maskGuideNode:cc.Node = null


    isAction:boolean = false

    freeLajiIndex = 3
    daojishiIndexFix = 300
    daojishiIndex = 300
    isDaojishi = false
    daojishiDt = 0


    freeTimeVD = 20
    freeTimedtVd = 0
    daojishiTime = 3
    isFanlaji = false
    isFanlajiClick = false


    clickIndex = 0
    clickIndexMax = 6

    _moneyIcon = []
    _goodIcon = []
    _dt:number = 0
    mainTaskId = 0
    addMoneyClick = 3
    taskFindDianHuaben = false
    isFindDianHuaben = false

    onLoad () {
        super.onLoad()

    }

    protected start(): void {
   
        this.refreshBtnStatus()
        this.showDialogQiPao()

        this.freeTimeArea.zIndex = 1
        this.node.getChildByName('exitRoom').zIndex = 2

        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        this.mainTaskId = mainTaskInfo.Id
  

        this.guideStep()
    }


    onDestroy(){



        if(cc.isValid(this.maskGuideNode)){
            this.maskGuideNode.removeFromParent()
            this.maskGuideNode.destroy()
        }

        this.isFanlajiClick = false


        
        this.freeTimedtVd = 0
        this._moneyIcon.length = 0
        this._goodIcon.length = 0
        super.onDestroy()
    }



    guideStep(){
    }

    guideView(nodeArray){
        if(this.maskGuideNode == null){
            PrefabManage.loadPrefabByType(GameType.GuideMskNode, this.node, (prefab)=>{
                let guideNodeArray = nodeArray
                this.maskGuideNode = prefab
                let firstNode = guideNodeArray.shift()
                prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
            })
        }else{
            this.maskGuideNode.active = true
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            this.maskGuideNode.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
        }
    }


    autoShoujiIcon(isDouble){
        let doubleIndex = 1
        if(isDouble){
            doubleIndex = 2
        }
        let goodAllMoney = 0
        if(this._goodIcon.length>0){
            this.node.getChildByName('背包3').opacity = 255
            let kouzi = this.node.getChildByName('背包3')

            let endPosi = Common5.getNodeToTargetPos(this._goodIcon[0],kouzi)
            let indexD = 0
            let index = 0
            for(let node of this._goodIcon){
                index++
                indexD=0.1
                goodAllMoney += DustbinManager.getCurGoodByid(node['objData'].goodId).priceNum
                cc.tween(node)
                .delay(indexD)
                .to(0.5,{x:endPosi.x, y:endPosi.y, scale:0})
                .call(()=>{
                    let objData = node['objData']
                    let _goodNum = 1*doubleIndex
                    let bagConfig:GoodsLocalProperty = {
                        goodId:objData.goodId,
                        goodNum:_goodNum,
                    }
                    BagManager.addBagList(bagConfig)
                    node.removeFromParent()
                    node.destroy()
                })
                .start()
            }
            let delay_ = 0.05*this._goodIcon.length+0.5
          
            if(this.isFindDianHuaben){
                this.isFindDianHuaben = false
                this.taskFindDianHuaben = false
               
            }

            this.scheduleOnce(()=>{
                this.node.getChildByName('背包3').opacity = 0
           
            }, delay_)
        }

    }

   



    showDialogQiPao(){
        // let index = Math.floor(Math.random()*StrTab.length)
        // let str = StrTab[index]
        // this.qipao.getChildByName("str").getComponent(cc.Label).string = str
        let index = Math.floor(Math.random()*3)
        let qiPaoNode = this.node.getChildByName("qiPao_"+index)
        qiPaoNode.stopAllActions()
        qiPaoNode.active = true
        qiPaoNode.scale = 0
        let _scale = 1
        if(index == 2){
            _scale = -1
        }
        cc.tween(qiPaoNode)
            .to(0.3,{scaleX:_scale, scaleY:1})
            .delay(2.5)
            .call(()=>{
                qiPaoNode.active = false
            })
        .start()
    }

    refreshBtnStatus(){
        let freeNum = 5//User.getLaJiFreeNum()
        this.btn_fanYiFan.getChildByName("timeStr").getComponent(cc.Label).string = '翻一翻'//`翻一翻(${freeNum}/3)`
        if(freeNum <= 0){
            this.btn_fanYiFan.active = false
            this.btn_shuaXin.active = true

            this.daojishiLab.node.active = true
            this.daojishiLab2.node.active = true
            if(LaJiZhanDataManage.getLajiTimeStamp() == 0){
                LaJiZhanDataManage.setLajiTimeStamp(new Date().getTime()) //毫秒
                this.daojishiIndex = this.daojishiIndexFix
                this.isDaojishi = true
                let time_ = Common5.formatTime3_custom(this.daojishiIndex)
                this.daojishiLab.string = time_
            }else{
                let curTime = new Date().getTime()
                //console.log((curTime-LaJiZhanDataManage.getLajiTimeStamp())/1000, '(curTime-LaJiZhanDataManage.getLajiTimeStamp())/1000++++++++++++======')
                this.daojishiIndex = this.daojishiIndexFix - (curTime-LaJiZhanDataManage.getLajiTimeStamp())/1000
            
                this.daojishiIndex>0?this.isDaojishi = true:this.isDaojishi = false
                if(this.daojishiIndex>0){
                    let time_ = Common5.formatTime3_custom(this.daojishiIndex)
                    this.daojishiLab.string = time_
                }else{
                    User.setLaJiFreeNum(this.freeLajiIndex)
                    LaJiZhanDataManage.setLajiTimeStamp(0)
                    this.refreshBtnStatus()
    
    
                }
            }
          
  
            //
        }else{
            this.btn_fanYiFan.active = true
            this.btn_shuaXin.active = false
            this.isDaojishi = false
            this.daojishiLab.node.active = false
            
            this.daojishiLab2.node.active = false
        }
    }
    onBtnShuaXinClick(){
        if(this.isAction){
            return
        }
        let tab = {
            onClose: (finish)=>{
                if (finish) {
            
                    this.reportKey(()=>{
                        GameReport.ADReport('垃圾桶刷新次数',1)
                    })

                    Common5.ReportDY("inLevel", '垃圾桶-AD-刷新次数');
                    User.setLaJiFreeNum(this.freeLajiIndex)
                    LaJiZhanDataManage.setLajiTimeStamp(0)
                    this.refreshBtnStatus()
                }
                else{
                    this.reportKey(()=>{
                        GameReport.ADReport('垃圾桶刷新次数',0)
                    })
                    Common5.showTips_custom("广告未观看完");
                }
            },onFailed:()=>{

            }
        }
        AppPlatform.playVideo_custom(tab)
    }
    onBtnWuLianClick(){
        
        if(this.isAction){
            return
        }
        let tab = {
            onClose: (finish)=>{
                if (finish) {
                    Common5.ReportDY("inLevel", '垃圾桶-AD-翻个够');
        
                    this.reportKey(()=>{
                        GameReport.ADReport('垃圾桶翻个够',1)
                    })

                    this.isAction = true
                    this.leSeSpine.stopAllActions()
                    this.leSeSpine.getComponent(sp.Skeleton).setAnimation(0,"fan",true)
                    cc.tween(this.leSeSpine)
                        .delay(1)
                        .call(()=>{
                            this.leSeSpine.getComponent(sp.Skeleton).setAnimation(0,"jingzhi",false)
                            this.playNewIcon(5,true)
                        })
                    .start()
                    ProbabilityManager.gameClickLajiTimeAdd(true)
                }
                else{
                    this.reportKey(()=>{
                        GameReport.ADReport('垃圾桶翻个够',0)
                    })
                    Common5.showTips_custom("广告未观看完");
                }
            },onFailed:()=>{

            }
        }
        AppPlatform.playVideo_custom(tab)
    }
    onBtnFanYiFanClick(){
        if(this.isAction){
            return
        }
        this.isAction = true
        // User.setLaJiFreeNum(User.getLaJiFreeNum() - 1)
       
        this.reportKey(()=>{
            GameReport.BtnsReport('垃圾桶翻一翻')
        })
  
        this.leSeSpine.stopAllActions()
        this.leSeSpine.getComponent(sp.Skeleton).setAnimation(0,"fan",true)
        cc.tween(this.leSeSpine)
            .delay(1)
            .call(()=>{
                this.leSeSpine.getComponent(sp.Skeleton).setAnimation(0,"jingzhi",false)
                this.playNewIcon(1,false)
            })
        .start()


        ProbabilityManager.gameClickLajiTimeAdd(false)
        // this.refreshBtnStatus()
    }


    clickLajiEvent(event){
        let target = event.target
        let objData = target['objData']
        let priceNum = objData.priceNum

        UserManager.addMoney(priceNum, this.node)

        
        target.removeFromParent()
        target.destroy()
    }



 



    playNewIcon(awardNum,isAD?){
        let goodArray = InterfaceManager.getMangheGoodsListByType('Dustbin',awardNum,isAD)

        PrefabManage.loadPrefabByType(GameType.GetAward,null,(prefabNode)=>{
            prefabNode.getComponent(GetAward).initView(goodArray, ()=>{
                this.isAction = false
               
            }, true);
        },false)

     
        return
    
    }



    
    showGuide(){
        
    }
}