// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import User from "../../FrameWork/User/User";
import Common5 from "../../Platform/th/Common5";
import LevelUpManager from "../Manager/LevelUpManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class FeiQiJiDi1 extends cc.Component {

    @property(cc.Label)
    daojishiLab: cc.Label = null;

    @property(cc.Node)
    daojishiNode: cc.Node = null;

    @property(cc.ProgressBar)
    progress: cc.ProgressBar = null;


    @property(cc.Label)
    proLabDi: cc.Label = null;
    isDaojishi = false
    _time = 0
    // LIFE-CYCLE CALLBACKS:
    clickMax = 30
    clickIndex = 0

    clickIndex2 = 0

    timeMax = 10
    // onLoad () {}
    
    menBooldNum = 3000000000000 //门的血量
    menBooldNumFix = 3000000000000//门的血量

    peopleBooldNum = 0
    clickTimeOut = null
    firstClick = true
    onDestroy(){
        cc.audioEngine.stopMusic()
    }
    
    start () {
        // 预加载动画和下一个预制体
        Common5.playRemoteAudioMusic('sound/JuQingBG/废弃基地背景音')
        this.scheduleOnce(()=>{
            PrefabManage.preloadPrefabByType(GameType.FeiQiJiDi2)
            Common5.loadRemoteSpine('Spine/FeiQiJiDi', '洛北辰')
            Common5.loadRemoteSpine('Spine/FeiQiJiDi', '甄刑')
        },0.1)

        this.proLabDi.string = Common5.getNumberChangeHanzi(this.menBooldNum,'1',1)+'/'+Common5.getNumberChangeHanzi(this.menBooldNumFix,'1',1)
        this.peopleBooldNum =  LevelUpManager.getCurClickIncome()
    }

    protected update(dt: number): void {
        if(!this.isDaojishi){
            return
        }

        this._time+=dt
        if(this._time>=1){
            this._time = 0
            this.clickIndex++
            let tempIndex = this.timeMax - this.clickIndex
            this.daojishiLab.string = `倒计时:${tempIndex}s`
            if(tempIndex <= 0){
                this.isDaojishi = false
                //失败
                let jingbaoqi = this.node.getChildByName('警报器')
                this.setAnim(jingbaoqi, '警报器', true)
                this.node.getChildByName('clickOpenDoor').active = false
                PrefabManage.preloadPrefabByType(GameType.BoxTipNode)

                this.scheduleOnce(()=>{
                    PrefabManage.loadPrefabByType(GameType.BoxTipNode, null, (prefab)=>{
                        prefab.getComponent('BoxTipScript').setViewData(2,'进入废弃基地失败,可提升等级后再次尝试', ()=>{
                            this.node.removeFromParent()
                            this.node.destroy()
                            PrefabManage.loadPrefabByType(GameType.FeiQiJiDi1)
                        }, ()=>{
                            //
                            this.endGame()
                            this.changeView()
                        })
                        prefab.getComponent('BoxTipScript').setBtnStr('重新开始', '直接进入')
                        prefab.getComponent('BoxTipScript').setCloseBtnView(true)
                        
                        prefab.getComponent('BoxTipScript').setCloseFunc(()=>{
                            //let name = this.node.name
                            this.node.removeFromParent()
                            this.node.destroy()
                            //EventMgr.emitEvent_custom(ryw_Event.ExitBtnEvent, name);
                        })
                    })
                },1.0)
         
                
            }

            
        }
    }

    
    endGame(){
        cc.tween(this.node)
        .to(1.0,{opacity:0})
        .call(()=>{
              
            let name = this.node.name
            this.node.removeFromParent()
            this.node.destroy()
            EventMgr.emitEvent_custom(ryw_Event.ExitBtnEvent, name);
        })
        .start()

    
    }

    
    changeView(){
        PrefabManage.loadPrefabByType(GameType.FeiQiJiDi2)
    }

    setAnim(node, anim, loop = false){
        node.getComponent(sp.Skeleton).setAnimation(0,anim,loop)
    }

    clickOpenDoorEvent(){
        let tempIndex = this.timeMax - this.clickIndex
        if(tempIndex <= 0){
            return
        }

        Common5.playEffectCustom('FeiQiJiDi', '敲门')
        this.menBooldNum-=this.peopleBooldNum

        let tielian =  this.node.getChildByName('铁链')

        clearTimeout(this.clickTimeOut);
        this.clickTimeOut = setTimeout(() => {
            this.setAnim(tielian, '静止铁链', true)
        }, 1000);


        this.setAnim(tielian, '拍门', false)
        if(this.firstClick){
            this.daojishiNode.active = true
            this.firstClick = false
            this.isDaojishi = true
        }

        this.progress.progress = this.menBooldNum/this.menBooldNumFix
        this.proLabDi.string = Common5.getNumberChangeHanzi(this.menBooldNum,'1',1)+'/'+Common5.getNumberChangeHanzi(this.menBooldNumFix,'1',1)

        if(this.menBooldNum<=0){
            this.node.getChildByName('clickOpenDoor').active = false
                
            this.isDaojishi = false
            //成功
            this.node.getChildByName('手指箭头').active = false
            
            this.daojishiNode.active = false
            cc.tween(tielian)
            .to(2, {opacity:0})
            .call(()=>{
                this.endGame()
                this.changeView()
            })
            .start()
        }

    }
    // update (dt) {}
}