// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        personFugui:cc.Node,
        personUncle:cc.Node,
        personOut:cc.Node,
        price:cc.Node,
        person_hui:cc.Node,
        hand:cc.Node,
        content:cc.Node,
        chatOne:cc.Label,
        mainCallPrice:cc.Node,
        mainCallPriceTwo:cc.Node,
        contentTable:"50000,1次",
        clickYes:true,
        count:1,
        downLoad:cc.Node,
        kill:cc.Node,
        buttonAudio: {
            default: null,
            type: cc.AudioClip
        },
        outAudio:{
            default: null,
            type: cc.AudioClip
        },

        outFiveAudio:{
            default: null,
            type: cc.AudioClip
        },
        outAudioFive:{
            default: null,
            type: cc.AudioClip
        },
        outAudioSeven:{
            default: null,
            type: cc.AudioClip
        },
        audioSeven:{
            default: null,
            type: cc.AudioClip
        },
        time: cc.Label,
        countdown: 3,
        countdown7_1:{
            default: null,
            type: cc.AudioClip
        },
        countdown7_2:{
            default: null,
            type: cc.AudioClip
        },
        countdown7_3:{
            default: null,
            type: cc.AudioClip
        },
        countdown7_success:{
            default: null,
            type: cc.AudioClip
        },
        audioTen:{
            default: null,
            type: cc.AudioClip
        },
        outAudioTen:{
            default: null,
            type: cc.AudioClip
        },
        audio15:{
            default: null,
            type: cc.AudioClip
        },
        countdown10_1:{
            default: null,
            type: cc.AudioClip
        },
        countdown10_2:{
            default: null,
            type: cc.AudioClip
        },
        countdown10_3:{
            default: null,
            type: cc.AudioClip
        },
        countdown10_success:{
            default: null,
            type: cc.AudioClip
        },
        screamAudio:{
            default: null,
            type: cc.AudioClip
        },
        door:cc.Node,
        carHide:cc.Node,
        egg:cc.Node,
        toSuccessHand:cc.Node,
        door_1:cc.Node,
        btnGiveUp:cc.Node,
        btnAddPrice:cc.Node
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    playMusic(){
        let audioId = cc.audioEngine.playEffect(this.buttonAudio, false);
        cc.audioEngine.setVolume(audioId, 0.5);
    },
    startClick(){
        this.playMusic()
        cc.director.loadScene("end_fail");
    },

    toSuccess(){
       
        if( !this.btnAddPrice.active&&!this.btnGiveUp.active&&this.count>1 ){
            this.playMusic()
            cc.director.loadScene("end_success");
        }
       
    },

    onDownLoad(e){
        // $('#btn').on('touchstart', function (e) {
            window.playableSDK.sendEvent('clickContent', {
                section: 'section2',
                area: 'area3'
            });
            window.playableSDK.openAppStore()


        // })
    },

    addPrice(){
        this.playMusic()
    
        if(this.count<=1&&this.clickYes&&!this.mainCallPrice.active&&!this.mainCallPriceTwo.active&&this.personOut.active){

            this.personFugui.setScale(0.6)
            this.count ++
            this.unscheduleAllCallbacks()
            this.price.active= true
            this.content.active = false
            this.hand.destroy();
            this.clickYes = false
            let sprite = this.node.getChildByName('five').getComponent(cc.Sprite)
            let spriteElse = this.node.getChildByName('five_narrator').getComponent(cc.Sprite)
            if(this.count == 1){
                let audioId = cc.audioEngine.playEffect(this.outAudioFive, false);
                cc.audioEngine.setVolume(audioId, 1);
            }
            if(this.count==2){

                sprite.spriteFrame = new cc.SpriteFrame(cc.url.raw('resources/ten.png'));
                spriteElse.spriteFrame = new cc.SpriteFrame(cc.url.raw('resources/ten_narrator.png'));
                let audioId = cc.audioEngine.playEffect(this.audioTen, false);
                cc.audioEngine.setVolume(audioId, 1);
            }
            this.scheduleOnce(function () {
                this.price.active= false
                this.mainCallPrice.active = true
                this.priceShow()
            }, 1.5);
        }
    },
    start () {
        console.log(this.personFugui)
        // this.chatOne.string = this.contentTable

        this.time.string = 3;           //  场景文本框为 显示5
        this.countdown = 3;
        if (this.countdown >= 0) {
            this.schedule(function () {    // 计时器将每隔 1s 执行一次。
              
                this.DoSomething();
                if(this.countdown==0){
                    this.time.active = false
                }   
            }, 1);
        }
    },
    DoSomething() {                // 倒计时算法
        if (this.countdown >= 1) {
            this.countdown = this.countdown - 1;
            this.time.string = this.countdown; 
            //场景中文本框显示 
            cc.log("countdown=" + this.countdown);
        }
    },
    priceShow(){
        let spriteElse = this.node.getChildByName('five_narrator').getComponent(cc.Sprite)
        let spriteTwo = this.node.getChildByName('7').getComponent(cc.Sprite)
        if(this.count == 1){
            this.personFugui.setScale(0.5)
            let audioId = cc.audioEngine.playEffect(this.outFiveAudio, false);
            cc.audioEngine.setVolume(audioId, 1);
            this.scheduleOnce(function () {    // 计时器将每隔 1s 执行一次。
               this.mainCallPrice.active = false
               this.clickYes = true
               this.mainCallPriceTwo.active = true
               this.personUncle.setScale(0.6)
               let audioId = cc.audioEngine.playEffect(this.audioSeven, false);
               cc.audioEngine.setVolume(audioId, 1);
               var n=1
               this.scheduleOnce(function(){
                spriteElse.spriteFrame = new cc.SpriteFrame(cc.url.raw('resources/7_narrator.png'));
                this.mainCallPrice.active = true
                this.mainCallPriceTwo.active = false
                let audioId = cc.audioEngine.playEffect(this.outAudioSeven, false);
                this.scheduleOnce(function(){
                    this.mainCallPrice.active = false
                    
                    this.personUncle.setScale(0.5)
                    
                    cc.audioEngine.setVolume(audioId, 1);
                    this.schedule(function(){
                        console.log("n:",n)
                        this.content.active = true
                        
                        let countdown
                        if(n===5){
                            this.content.active = false
                            this.unscheduleAllCallbacks()
                            this.startClick()
                            return 
                        }else if(n===4){
                            this.chatOne.string = "70000,成交"
                            countdown = cc.audioEngine.playEffect(this.countdown7_success, false);
                            cc.audioEngine.setVolume(countdown, 1);
                        }else if(n===3){
                            this.chatOne.string = "70000,"+n+'次'
                            countdown = cc.audioEngine.playEffect(this.countdown7_3, false);
                            cc.audioEngine.setVolume(countdown, 1);
                        }else if(n===2){
                            this.chatOne.string = "70000,"+n+'次'
                            countdown = cc.audioEngine.playEffect(this.countdown7_2, false);
                            cc.audioEngine.setVolume(countdown, 1);
                        }else if(n===1){
                            this.chatOne.string = "70000,"+n+'次'
                            countdown = cc.audioEngine.playEffect(this.countdown7_1, false);
                            cc.audioEngine.setVolume(countdown, 1);
                        }
                        n++
                    },1)
                },1.5)
               },2)
            }, 1.5);
        }else if(this.count == 2){
            this.personFugui.setScale(0.5)
            spriteElse.spriteFrame = new cc.SpriteFrame(cc.url.raw('resources/ten_narrator.png'));
            spriteTwo.spriteFrame = new cc.SpriteFrame(cc.url.raw('resources/15.png'));
            let audioId = cc.audioEngine.playEffect(this.outAudioTen, false);
            cc.audioEngine.setVolume(audioId, 1);
            this.scheduleOnce(function () {  
                this.personOut.setScale(0.6)  // 计时器将每隔 1s 执行一次。
                let audio15 = cc.audioEngine.playEffect(this.audio15, false);
                cc.audioEngine.setVolume(audio15, 1);
                this.mainCallPrice.active = false
                this.clickYes = true
                this.mainCallPriceTwo.active = true
                var n=1
                // this.scheduleOnce(function(){
                
                //  this.mainCallPrice.active = true
                 this.scheduleOnce(function(){
                    this.personOut.setScale(0.5)
                     this.mainCallPrice.active = false
                     this.mainCallPriceTwo.active = false
                     this.kill.active = true
                    //  this.schedule(function(){
                    //      console.log("n:",n)
                    //      this.content.active = true
                    //      this.chatOne.string = "150000,"+n+'次'
                         
                    //      if(n===4){
                    //          this.content.active = false
                    //          this.unscheduleAllCallbacks()
                    //          this.startClick()
                    //          return 
                    //      }
                    //      n++
                    //  },1)
                 },2)
                // },1)
             }, 2);
        }
    },
    kicking(){
       
        this.playMusic()

        this.personOut.active = false
        this.kill.active = false

        this.mainCallPrice.active = false
        this.mainCallPriceTwo.active = false
        
        this.scheduleOnce(function(){
            let audioId = cc.audioEngine.playEffect(this.outAudio, false);
            cc.audioEngine.setVolume(audioId, 0.5);

            this.scheduleOnce(function(){
                var scream = cc.audioEngine.playEffect(this.screamAudio, false);
                cc.audioEngine.setVolume(scream, 1);
                var anim = this.person_hui.getComponent(cc.Animation);
                console.log(anim)
                anim.play("outPerson");


               
                var n = 1
                this.schedule(function(){
                    this.content.active = true
                    // this.chatOne.string = "100000,"+n+'次'
                    
                    let countdown
                    if(n===5){

                        this.carHide.active = false
                        this.content.active = false
                        this.carHide.active = false
                        this.btnGiveUp.active =false
                        this.btnAddPrice.active = false
                        let two = this.door.getComponent(cc.Animation);
                        two.play("hideDoor")
                        this.toSuccessHand.active = true
                        this.unscheduleAllCallbacks()
                        // this.unscheduleAllCallbacks()
                        // let two = this.door.getComponent(cc.Animation);
                        // two.play("hideDoor");
                        // this.content.active = false
                        // this.toSuccess()
                        // this.scheduleOnce(function(){
                        //     this.toSuccess()
                        //     
                        // },2)
                        return 
                    }else if(n===4){
                        this.chatOne.string = "100000,成交"
                        countdown = cc.audioEngine.playEffect(this.countdown10_success, false);
                        cc.audioEngine.setVolume(countdown, 1);
                    }else if(n===3){
                        this.chatOne.string = "100000,"+n+'次'
                        countdown = cc.audioEngine.playEffect(this.countdown10_3, false);
                        cc.audioEngine.setVolume(countdown, 1);
                    }else if(n===2){
                        this.chatOne.string = "100000,"+n+'次'
                        countdown = cc.audioEngine.playEffect(this.countdown10_2, false);
                        cc.audioEngine.setVolume(countdown, 1);
                    }else if(n===1){
                        this.chatOne.string = "100000,"+n+'次'
                        countdown = cc.audioEngine.playEffect(this.countdown10_1, false);
                        cc.audioEngine.setVolume(countdown, 1);
                    }
                    n++
                },1)
            },.5)
        },.5)
       
        // let spriteOut = this.node.getChildByName('person_out').getComponent(cc.Sprite)
        
        // spriteOut.spriteFrame = new cc.SpriteFrame(cc.url.raw('resources/person_out.png'));
    },
    changeMos(){
        this.playMusic()
        // this.scheduleOnce(function(){
        //     let audioId = cc.audioEngine.playEffect(this.outAudio, false);
        //     cc.audioEngine.setVolume(audioId, 0.5);
            
            
        // },.5)
        // this.personOut.active = false
        this.kill.active = false
        this.door_1.active = true
        let spriteTwo = this.node.getChildByName('7').getComponent(cc.Sprite)
        spriteTwo.spriteFrame = new cc.SpriteFrame(cc.url.raw('resources/out.png'));
        
       
        this.scheduleOnce(function(){
            this.mainCallPriceTwo.active = true
        },1)
       
        this.scheduleOnce(function(){
            this.mainCallPriceTwo.active = false
            var n = 1
            if(!this.mainCallPriceTwo.active){
                this.schedule(function(){
                    console.log("n:",n)
                    this.content.active = true
                    this.chatOne.string = "100000,"+n+'次'
                    let countdown
                    if(n===5){
                        this.content.active = false
                        this.carHide.active = false
                        this.door_1.active = false
                        this.btnGiveUp.active =false
                        this.btnAddPrice.active = false
                        let two = this.door.getComponent(cc.Animation);
                        two.play("hideDoor")
                        this.toSuccessHand.active = true
                        this.unscheduleAllCallbacks()
                        // this.scheduleOnce(function(){
                        //     this.toSuccess()
                        //     this.unscheduleAllCallbacks()
                        // },2)
                       
                        return 
                    }else if(n===4){
                        this.chatOne.string = "100000,成交"
                        countdown = cc.audioEngine.playEffect(this.countdown10_success, false);
                        cc.audioEngine.setVolume(countdown, 1);
                    }else if(n===3){
                        this.chatOne.string = "100000,"+n+'次'
                        countdown = cc.audioEngine.playEffect(this.countdown10_3, false);
                        cc.audioEngine.setVolume(countdown, 1);
                    }else if(n===2){
                        this.chatOne.string = "100000,"+n+'次'
                        countdown = cc.audioEngine.playEffect(this.countdown10_2, false);
                        cc.audioEngine.setVolume(countdown, 1);
                    }else if(n===1){
                        this.chatOne.string = "100000,"+n+'次'
                        countdown = cc.audioEngine.playEffect(this.countdown10_1, false);
                        cc.audioEngine.setVolume(countdown, 1);
                    }
                    n++
                },1)
            }
        },2)
        // let anim = this.person_hui.getComponent(cc.Animation);
        // console.log(anim)
        // anim.play("outPerson");
        
        // this.mosaic.active=true

        // this.mainCallPrice.active = false
        // this.mainCallPriceTwo.active = false
        
      
        
    },
    showEgg(){
        let two = this.egg.getComponent(cc.Animation);
        two.play("smallToBig");
    }
    // update (dt) {
    // },
});
