import GameReport from "../../FrameWork/Report/ZyZyReport";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import Common5 from "../../Platform/th/Common5";
import WXAPI from "../../Platform/weixin/WXAPI";
import UserManager from "../Manager/UserManager";
import GameBase from "../SCommon/GameBase";

const {ccclass, property} = cc._decorator;

enum TouchNodeType{
    SaiChe,
    HongLvDeng
}

let XinNianHongBaoConfig = [
    {hongBaoNum:8,awardNum:880000},
    {hongBaoNum:88,awardNum:88000000},
    {hongBaoNum:188,awardNum:1880000000}
]
@ccclass
export default class SaiChe extends GameBase {
    @property(cc.Node)
    gameBg:cc.Node = null
    @property(cc.Node)
    boxNode:cc.Node = null
    @property(cc.Node)
    passNum:cc.Node = null

    @property(cc.Node)
    btn_restart:cc.Node = null
    @property(cc.Node)
    btn_ad:cc.Node = null 
    
    @property([cc.Node])
    guanZi_zuoArr:cc.Node[] = []
    @property([cc.Node])
    guanZi_youArr:cc.Node[] = []
    
    @property(cc.Node)
    cheZiItem:cc.Node = null


    @property(cc.Node)
    baoXiangBtn:cc.Node = null
    @property(cc.Node)
    qiPaoBaoXiang:cc.Node = null
    
    // @property([cc.SpriteFrame])
    // cheZiSpFrameArr:cc.SpriteFrame[] = []


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


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

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

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

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

    passTab = []//奖励的阶段通过数
    cheZiArr = []
    guanZiSpeedArr = [250,200,230]//管子移动速度
    guanZiIsMoveToRigthArr = [true,true,true]
    cheZiMoveSpeed:number = 1600//车子移动速度
    isStopAll:boolean = true//是否暂停所有运动物体
    isStopDiCi:boolean = false//是否暂停地刺(红绿灯控制)
    isNowHongDeng:boolean = false
    // perCarMoney:number = 50
    targetSum:number = 0
    targetMaxNum:number = 0
    targetAward:number = 0
    passCarNum:number = 0//已通过车的数量
    bigAward:number = 5000000
    perHongLvDengCost:number = 20000
    // freeAdNum:number = 2//看广告次数
    _dt:number = 0
    awardNum:number = 0

    fixMoney = 8000000000
    Proportion = 0.0005 
    rewardBili= [0.03, 0.08, 0.13, 0.2, 0.56]
    rewardMoney = []
    jieduanIndex = 0

    onDestroy(){
        // AppPlatform.stopRecord_custom()
        super.onDestroy()
    }

    onLoad () {
        super.onLoad()
        // this.passTab = [10,50,100,200,500]
        // this.targetSum = this.passTab[4]
        // this.targetMaxNum = this.passTab[this.jieduanIndex]
        this.initHongBaoNode()
        // let freeNum = 3
        // if(freeNum > 0){
        //     this.btn_restart.active = true
        //     this.btn_ad.active = false
        //     this.btn_restart.getChildByName("leftNums").getComponent(cc.Label).string = `开 始`
        // }else{
        //     this.btn_restart.active = false
        //     this.btn_ad.active = true
        //     this.btn_ad.getChildByName("freeNums").getComponent(cc.Label).string = `获取次数`
        // }
        this.setGmClbx()
        // this.openTouchEvent(this.gameBg,TouchNodeType.SaiChe)
        // this.openTouchEvent(this.hongLvDeng,TouchNodeType.HongLvDeng)
        // this.initAllGuanZi()
        for(let i = 0; i<this.rewardBili.length; i++){
            let bili = this.rewardBili[i]
            let earnMoney = 1000
            let allMoney = (this.fixMoney+earnMoney*Common5.earnMoneySecond)*this.Proportion
            let jieduanMoney = Math.floor(allMoney*bili)
            this.rewardMoney.push(jieduanMoney)
        }

        console.log(this.rewardMoney, 'this.rewardMoney++++')
        
    }

    start () {
        Common5.playMusic("CommonRes/sound/电玩城");
        this.refreshQipao()
    }

    refreshQipao(){
        let rewardMoney = this.rewardMoney[this.jieduanIndex]
        let qian = this.qipaoNode.getChildByName('qian').getComponent(cc.Label)
        qian.string = Common5.getNumberChangeHanzi(rewardMoney)+''
    }

    update (dt) {
        if(this.isStopAll){
            return
        }
        if(dt > 0.5){
            dt = 1/60
        }
        if(!this.isStopDiCi){
            //地刺运动
            for(var i=0;i<3;i++){
                // Common5.checkIntersectsBox()
                if(this.guanZiIsMoveToRigthArr[i]){
                    this.guanZi_zuoArr[i].x += dt*this.guanZiSpeedArr[i]
                    this.guanZi_youArr[i].x -= dt*this.guanZiSpeedArr[i]
                    if(this.guanZi_zuoArr[i].x >= 0){
                        this.guanZiSpeedArr[i] = Math.floor(Math.random()*400 + 100)
                        this.guanZiIsMoveToRigthArr[i] = false
                    }
                }else{
                    this.guanZi_zuoArr[i].x -= dt*this.guanZiSpeedArr[i]
                    this.guanZi_youArr[i].x += dt*this.guanZiSpeedArr[i]
                    if(this.guanZi_zuoArr[i].x <= -350){
                        this.guanZiSpeedArr[i] = Math.floor(Math.random()*400 + 100)
                        this.guanZiIsMoveToRigthArr[i] = true
                    }
                }
            }
        }
        //车子运动
        for(var i=0;i<this.cheZiArr.length;i++){
            this.cheZiArr[i].y -= dt*this.cheZiMoveSpeed
            
            //碰撞检测
            for(var j=0;j<3;j++){
                if(Common5.checkIntersectsBox(this.cheZiArr[i].getChildByName("langArea"),this.guanZi_zuoArr[j].getChildByName("kongLongArea"))){
                    Common5.playEffect("撞车")
                    console.log("碰撞了=====")
                    this.isStopAll = true
                    this.isNowHongDeng = false
             
                    this.closeTouchEvent(this.gameBg)
                    this.scheduleOnce(()=>{
                        this.handGameOver()
                    },0.5)
                }
            }
            if(this.cheZiArr[i].y <= -465){
                Common5.playEffectCustom('SaiChe','sound/接到红包')
                this.penZiSpine.getComponent(sp.Skeleton).setAnimation(0,'2',false)
                this.pengZhuangSpine.active = true
                this.pengZhuangSpine.getComponent(sp.Skeleton).setAnimation(0,'pengzhuang',false)
                this.cheZiArr[i].removeFromParent()
                this.cheZiArr.splice(i,1)
                this.passCarNum += 1
                let awardNum = 0
                for(var j=this.passTab.length-1;j>= 0;j--){
                    if(this.passCarNum >= this.passTab[j]){
                        awardNum = j+1
                        break
                    }
                }
                if(this.awardNum != awardNum){
                    this.awardNum = awardNum
                    //this.flyXingXingSpine()
                    //this.flyMoney()
                }
                this.baoXiangBtn.getChildByName("awardNum").getComponent(cc.Label).string = `x${awardNum}`
                this.passNum.getComponent(cc.Label).string = `${this.passCarNum}/${this.targetSum}`
                if(this.passCarNum>=this.targetSum){
                    this.isStopAll = true
                    this.jieduanIndex = 0
                    this.isNowHongDeng = false
                
                    this.closeTouchEvent(this.gameBg)
                    this.handGameOver()
                    return
                }
            }
        }
    }

    flyMoney(){
        let rewardMoney = this.rewardMoney[this.jieduanIndex]
        UserManager.addMoney(rewardMoney, this.qipaoNode)
        this.jieduanIndex++
        this.refreshQipao()
    }


    flyXingXingSpine(){
        this.xingxingSpine.active = true
        this.xingxingSpine.setPosition(this.qiPaoBaoXiang.getPosition())
        let localPos = this.baoXiangBtn.getPosition()
        cc.tween(this.xingxingSpine)
            .to(1,{x:localPos.x,y:localPos.y})
            .call(()=>{
                this.xingxingSpine.active = false
            })
        .start()
    }
    initAllGuanZi(){
        for(var i=0;i<3;i++){
            this.guanZi_zuoArr[i].x = -350
            this.guanZi_youArr[i].x = 350
        }
    }
    initAllCheZi(){
        for(var i=0;i<this.cheZiArr.length;i++){
            this.cheZiArr[i].removeFromParent()
        }
        this.cheZiArr = []
    }
    openTouchEvent(node,touchType){
        node.on(cc.Node.EventType.TOUCH_START,this.touchStartNode,this)
        node.attr({touchType:touchType})
    }
    closeTouchEvent(node){
        node.off(cc.Node.EventType.TOUCH_START,this.touchStartNode,this)
    }
    touchStartNode(event){
        if(event.target.touchType == TouchNodeType.SaiChe){
            let cheZi = cc.instantiate(this.cheZiItem)
            cheZi.active = true
            // let randomNum = Math.floor(Math.random()*5)
            // cheZi.getComponent(cc.Sprite).spriteFrame = this.cheZiSpFrameArr[randomNum]
            this.gameBg.addChild(cheZi)
            this.cheZiArr.push(cheZi)
        }
    }



    onBtnRestartClick(){
    
        this.setGmClbx()
        this.passCarNum = 0
        this.passNum.getComponent(cc.Label).string = `${this.passCarNum}/${this.targetSum}`
        this.openTouchEvent(this.gameBg,TouchNodeType.SaiChe)
        // this.openTouchEvent(this.hongLvDeng,TouchNodeType.HongLvDeng)
        this.initAllGuanZi()
        this.initAllCheZi()
 
        this.isStopAll = false
        this.boxNode.active = false

        // AppPlatform.startRecord_custom()
        // this.freeAdNum = 2
    }
    onBtnLingQuClick(){
        UserManager.addMoney(this.targetAward)
        this.boxNode.active = false
        this.selectNode.active = true
    }
    fuhuoClickEvent(){
        Common5.playEffect("sound/按键点击")
        let tab = {
            onClose: (finish)=>{
                if (finish) {
                    Common5.ReportDY("inLevel", '红包活动-AD-复活继续');
                    this.reportKey(()=>{
                        GameReport.ADReport('红包活动复活继续',1)
                    })
                    //User.setSaiCheFreeNum(User.getSaiCheFreeNum() - 1)
            
                    // this.btn_restart.active = false
                    this.setGmClbx()
                    this.passNum.getComponent(cc.Label).string = `${this.passCarNum}/${this.targetSum}`
                    this.openTouchEvent(this.gameBg,TouchNodeType.SaiChe)
             
                    this.initAllGuanZi()
                    this.initAllCheZi()
                    this.isNowHongDeng = true
          
                    this.isStopAll = false
                    this.boxNode.active = false
            
                    // AppPlatform.startRecord_custom()
                }
                else{
                    this.reportKey(()=>{
                        GameReport.ADReport('红包活动复活继续',0)
                    })
                    Common5.showTips_custom("广告未观看完");
                }
            },onFailed:()=>{

            }
        }
        AppPlatform.playVideo_custom(tab)
    }

    onBtnADClick(){
        Common5.playEffect("sound/按键点击")
        let tab = {
            onClose: (finish)=>{
                if (finish) {
                    Common5.ReportDY("inLevel", '红包-AD-看视频');
                    // User.setSaiCheFreeNum(3)
                    this.isNowHongDeng = false
                    this.reportKey(()=>{
                        GameReport.ADReport('红包看视频',1)
                    })
                    // this.btn_restart.active = true
                    this.btn_ad.active = false
                    // this.btn_restart.getChildByName("leftNums").getComponent(cc.Label).string = `开始(3/3)`
                    // this.freeAdNum -= 1
                    // if(this.freeAdNum < 0){
                    //     this.freeAdNum = 0
                    // }
                    // this.openTouchEvent(this.gameBg,TouchNodeType.SaiChe)
                    this.initAllGuanZi()
                    this.initAllCheZi()
                    // this.isStopAll = false
                    this.boxNode.active = false
                }
                else{
                    this.reportKey(()=>{
                        GameReport.ADReport('红包看视频',0)
                    })
                    Common5.showTips_custom("广告未观看完");
                }
            },onFailed:()=>{

            }
        }
        AppPlatform.playVideo_custom(tab)
    }

    handGameOver(){
        
        this.boxNode.active = true
        // AppPlatform.stopRecord_custom()
        Common5.showInterstitialAd()
        if(this.passCarNum >= this.targetSum){
            Common5.playEffect("sound/成功")
            this.boxNode.getChildByName("failBg").active = false
            cc.tween(this.boxNode.getChildByName("successBg"))
                .set({scale:0.5,active:true})
                .to(0.2,{scale:1.1})
                .to(0.15,{scale:1})
            .start()
            this.passCarNum = 0
            
            this.boxNode.getChildByName("successBg").getChildByName('awardStr').getComponent(cc.Label).string = 'x'+this.targetAward
        }else{
            Common5.playEffect("sound/失败")
            cc.tween(this.boxNode.getChildByName("failBg"))
                .set({scale:0.5,active:true})
                .to(0.2,{scale:1.1})
                .to(0.15,{scale:1})
            .start()
            this.boxNode.getChildByName("successBg").active = false
        }
    }

    setGmClbx(){
     
    }

    shareBtnClickEvent(){
        Common5.playEffect("sound/按键点击")
        
        // WXAPI.share_custom((finish)=>{
        // }, '','');

        // AppPlatform.shareRecord_custom(() => {
        //     // this.lingquClickEvent()
        // }, () => {
        //     //Common5.showTips_custom("录屏分享失败,请稍后再试!");
        // })

    }
    initHongBaoNode(){
        this.selectNode.getChildByName('btn1').getChildByName('str').getComponent(cc.Label).string = `接到${XinNianHongBaoConfig[0].hongBaoNum}个红包`
        this.selectNode.getChildByName('btn2').getChildByName('str').getComponent(cc.Label).string = `接到${XinNianHongBaoConfig[1].hongBaoNum}个红包`
        this.selectNode.getChildByName('btn3').getChildByName('str').getComponent(cc.Label).string = `接到${XinNianHongBaoConfig[2].hongBaoNum}个红包`
        
        this.selectNode.getChildByName('awardStr1').getComponent(cc.Label).string = `x${Common5.getNumberChangeHanzi(XinNianHongBaoConfig[0].awardNum,'1',1)}`
        this.selectNode.getChildByName('awardStr2').getComponent(cc.Label).string = `x${Common5.getNumberChangeHanzi(XinNianHongBaoConfig[1].awardNum,'1',1)}`
        this.selectNode.getChildByName('awardStr3').getComponent(cc.Label).string = `x${Common5.getNumberChangeHanzi(XinNianHongBaoConfig[2].awardNum,'1',1)}`
    }
    selectTypeClick(event,customData){
        // AppPlatform.startRecord_custom()
        this.targetSum = XinNianHongBaoConfig[customData].hongBaoNum
        this.targetAward = XinNianHongBaoConfig[customData].awardNum
        this.selectNode.active = false
        this.onBtnRestartClick()
    }

}