// 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 Common5 from "../../../../Platform/th/Common5";
import GuiYiKuaiDiScript from "./GuiYiKuaiDiScript";

const {ccclass, property} = cc._decorator;

enum FANGXIANG {
    UP,
    DOWN
}
@ccclass
export default class DianTiScript extends cc.Component {

    @property(cc.SpriteFrame)
    diantiwaiLiang = null
    @property(cc.SpriteFrame)
    xiaocheLiang = null
    @property(cc.SpriteFrame)
    hongyiLiang = null
    @property(cc.SpriteFrame)
    laotaiLiang = null
    @property(cc.SpriteFrame)
    nvhaiLiang = null

    @property(cc.SpriteFrame)
    diantiwaiAn = null
    @property(cc.SpriteFrame)
    xiaocheAn = null
    @property(cc.SpriteFrame)
    hongyiAn = null
    @property(cc.SpriteFrame)
    laotaiAn= null
    @property(cc.SpriteFrame)
    nvhaiAn= null
    // onLoad () {}
    @property(cc.Node)
    diantiwaiNode = null
    @property(cc.Node)
    xiaocheNode= null
    @property(cc.Node)
    hongyiNode = null
    @property(cc.Node)
    laotaiNode= null
    @property(cc.Node)
    nvhaiNode= null

    @property(cc.Node)
    diantiSpine= null

    @property(cc.Label)
    diantiCengshu= null

    @property(cc.Label)
    diantiCengshu2= null


    @property(cc.Node)
    renwunvhai= null

    @property(cc.Node)
    renwuhongyi= null

    @property(cc.Node)
    renwulaotai= null
    @property(cc.Node)
    annniuNode:cc.Node= null
    
    @property(cc.Node)
    GuiYiKuaiDiNode = null

    @property(cc.Node)
    kuaidi206Node = null

    @property(cc.Node)
    shangbaNode = null

    @property(cc.Node)
    kuaidi606Node = null
    @property(cc.Node)
    penwuNode = null

    

    diantiClickIndexArray = []
    curDiantiIndex = 1
    isBlack =true
    _timeout = null
    startUpdate: boolean = false;
    fangxiangIndex = FANGXIANG.UP
    
    firstInit4Lou = false

    start () {
        this.changeAn()

        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck,(data)=>{
            this.touchMovecheck(data)
        },this)
        
    }

    touchMovecheck(data){
        console.log('11111111111111')
        if(data.targetNode == this.kuaidi206Node){
            this.scheduleOnce(()=>{
                let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
                scrc.setChooseCloseBtn(true)
                scrc.kuaidi206ClickEvent()
            },1.5)
        }else if(data.targetNode == this.kuaidi606Node){
            let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
            let isChoose = scrc.getIsChooseTrue()
            if(this.shangbaNode.active == false && this.kuaidi606Node.active == false && this.kuaidi206Node.active == false && isChoose){
                scrc.successUI()
            }else if(this.shangbaNode.active == false && this.kuaidi606Node.active == false){
                //this.nextSelfNodeAnim()
            }
        }else if(data.targetNode == this.penwuNode){
        
            let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
            let isChoose = scrc.getIsChooseTrue()
            scrc.penwu606ClickEvent()
            if(this.shangbaNode.active == false && this.kuaidi606Node.active == false && this.kuaidi206Node.active == false && isChoose){
                scrc.successUI()
            }else if(this.shangbaNode.active == false && this.kuaidi606Node.active == false){
                //this.nextSelfNodeAnim()
            }
          
        }
    }

    diantiClickEvent(event, data){
        if( this.startUpdate){
            return
        } 
        let num_ =  parseInt(data)
        if(num_ == this.curDiantiIndex){
            return
        } 
        this.fangxiangIndex = 0
        let target = event.target
        let btnLiang = target.getChildByName('btnLiang')
        btnLiang.active = true

        if(this.diantiClickIndexArray.indexOf(num_) < 0){
            this.diantiClickIndexArray.push(num_)
            if (null != this._timeout){
                clearTimeout(this._timeout);
                this._timeout = null;
            }
         
            this._timeout = setTimeout(()=>{
                //电梯启动,开始电梯动画
                //重大到小
                if(this.firstInit4Lou == false){
                    let have6 = 6
                    if(this.diantiClickIndexArray.indexOf(have6) >= 0){
                        let num_4 = 4
                        this.diantiClickIndexArray.push(num_4)
                    }
                   
                }

                this.diantiClickIndexArray.sort(function(a,b){
                    return b-a
                })

                this.fangxiangIndex = 0
                if(this.diantiClickIndexArray[0]>this.curDiantiIndex){
                    //不变向上
                    this.fangxiangIndex = 0
                }else{
                    this.fangxiangIndex = 1
                    this.diantiClickIndexArray.sort(function(a,b){
                        return a-b
                    })
                }
                console.log( this.diantiClickIndexArray)
           
                this.playDiantiAnim()
                this.startUpdate = true
            }, 2000)

        }
    }

    playDiantiAnim(){
        let daodaDianti = this.diantiClickIndexArray.pop()
        if(!daodaDianti){
            this.startUpdate = false
            return
        }
        cc.tween(this.node)
        .delay(1.0)
        .call(()=>{
        
            if(daodaDianti>this.curDiantiIndex){
                this.curDiantiIndex++
            }else{
                this.curDiantiIndex--
            }
            console.log(this.curDiantiIndex)
            this.diantiCengshu.string =  this.curDiantiIndex+''
            this.diantiCengshu2.string =  this.curDiantiIndex+''
            if(this.curDiantiIndex == daodaDianti){
                Common5.playEffectCustom('guiyiKuaidi', "sound/电梯到了");

                this.node.stopAllActions()
                let target = this.annniuNode.children[this.curDiantiIndex-1]
                let btnLiang = target.getChildByName('btnLiang')
                btnLiang.active = false
                this.diantiSpine.getComponent(sp.Skeleton).setAnimation(0,'kai', false)

                if(daodaDianti == 2){
                    //选项
                    if(this.isBlack == false){
                        //失败
                        this.renwulaotai.active = true
                        this.scheduleOnce(()=>{
                          
                            this.laotaiHideFailGame()
                        },1.5)
                    }else{
                        
                        // let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
                        // scrc.kuaidi602Event()

                        this.renwunvhai.active = false
                        this.renwunvhai.getChildByName('shangba').active = false
                        this.renwuhongyi.active = false
                        this.renwulaotai.active = true
                    }
             
             
                }else if(daodaDianti == 6){

                    if(this.isBlack == false){

                    }else{
                        this.scheduleOnce(()=>{
                            let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
                            scrc.nvhaiFail()
                        },1.5)
                    }
              
                    // let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
                    // scrc.kuaidi606Event()

                    this.renwunvhai.active = true
                    this.renwunvhai.getChildByName('shangba').active = true
                    this.renwuhongyi.active = false
                    this.renwulaotai.active = false
                }else{
                    this.firstInit4Lou = true
     
                    this.renwunvhai.active = false
                    this.renwunvhai.getChildByName('shangba').active = false
                    this.renwuhongyi.active = true
                    this.renwulaotai.active = false

                    //亮灯
                    if(this.isBlack == false){
                        this.scheduleOnce(()=>{
                            this.hongyinvXiaoshi()  
                        },1.5)

                    }else{
                        this.scheduleOnce(()=>{
                            let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
                            scrc.setChooseCloseBtn(false)
                            scrc.hongyiClickEvent()
                        },1.5)
                    }

    
                }
            } 
        })  
        .union()
        .repeatForever()
        .start()
    }

    changeLiang(){
        this.node.getChildByName('灯光').active = true

        this.diantiSpine.getComponent(sp.Skeleton).setSkin('liang')
        this.diantiwaiNode.getComponent(cc.Sprite).spriteFrame = this.diantiwaiLiang
        this.xiaocheNode.getComponent(cc.Sprite).spriteFrame = this.xiaocheLiang
        this.hongyiNode.getComponent(cc.Sprite).spriteFrame = this.hongyiLiang
        this.laotaiNode.getComponent(cc.Sprite).spriteFrame = this.laotaiLiang
        this.nvhaiNode.getComponent(cc.Sprite).spriteFrame = this.nvhaiLiang
        if(this.renwuhongyi.active && this.curDiantiIndex == 4){
            // 红衣女尖叫消失,关门,next
            this.hongyinvXiaoshi()
        }
        if(this.renwulaotai.active&& this.curDiantiIndex == 2){
            let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
            scrc.laotaiFail2()
            //失败
        }
    }


    changeAn(){
        this.node.getChildByName('灯光').active = false
        this.diantiSpine.getComponent(sp.Skeleton).setSkin('an')
        this.diantiwaiNode.getComponent(cc.Sprite).spriteFrame = this.diantiwaiAn
        this.xiaocheNode.getComponent(cc.Sprite).spriteFrame = this.xiaocheAn
        this.hongyiNode.getComponent(cc.Sprite).spriteFrame = this.hongyiAn
        this.laotaiNode.getComponent(cc.Sprite).spriteFrame = this.laotaiAn
        this.nvhaiNode.getComponent(cc.Sprite).spriteFrame = this.nvhaiAn
        if(this.renwunvhai.active&& this.curDiantiIndex == 6){
            let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
            scrc.nvhaiFail()
            //失败
        }
    }

    kaidengClickEvent(){
        Common5.playEffect("点击音效");
        if(this.isBlack){
            this.isBlack = false
            this.changeLiang()
        }else{
            this.isBlack = true
            this.changeAn()
        } 
    }


    nextSelfNodeAnim(){

        console.log('nextSelfNodeAnim')
        this.diantiSpine.getComponent(sp.Skeleton).setAnimation(0,'guan', false)

        let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
        let isChoose = scrc.getIsChooseTrue()
        if(this.penwuNode.active == false && this.kuaidi606Node.active == false && this.kuaidi206Node.active == false && isChoose){
            scrc.successUI()
        }


        if(this.diantiClickIndexArray.length ==0){
            this.startUpdate = false
            return
        }
  
        this.scheduleOnce(()=>{
            this.playDiantiAnim()
        },1.5)
     
    }

    hongyinvXiaoshi(){
        Common5.playEffect("女生惨叫");
        let renwuhongyi = this.node.getChildByName('renwuhongyi')
        cc.tween(renwuhongyi)
        .to(1,{opacity:0})
        .call(()=>{
            let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
            scrc.setChooseLayerHide()
            this.nextSelfNodeAnim()
        })
        .start()
    }


    diantiAninFail(callFunc1,callFunc2){
        let renwuhongyi = this.node.getChildByName('renwuhongyi')
        let renwuhongyi2 = this.node.getChildByName('renwuhongyi2')
        renwuhongyi2.active = true
        cc.tween(renwuhongyi)
        .to(1,{opacity:0})
        .call(()=>{
            cc.tween(renwuhongyi2)
            .to(1,{opacity:255})
            .call(()=>{
     
                this.diantiSpine.getComponent(sp.Skeleton).setAnimation(0,'guan', false)

                this.scheduleOnce(()=>{
                    if(callFunc1){
                        callFunc1()
                    }
                    let curIndex =  this.curDiantiIndex*3
                    cc.tween(this.GuiYiKuaiDiNode)
                    .to(0.1, {angle:-5})
                    .to(0.1, {angle:5})
                    .call(()=>{
                        this.curDiantiIndex--
                        this.diantiCengshu.string =  this.curDiantiIndex+''
                        this.diantiCengshu2.string =  this.curDiantiIndex+''
                    })
                    .union()
                    .repeat(curIndex)
                    .call(()=>{
                        this.GuiYiKuaiDiNode.angle = 0
                     
                    })
                    .delay(1.0)
                    .call(()=>{
                        this.GuiYiKuaiDiNode.angle = 0
                        if(callFunc2){
                            callFunc2()
                        }
                    })
                    .start()
              
        
          
                },1.5)
            })
            .start()
        })
        .start()
    }

    closeDianti606ClickEvent(){
        Common5.playEffect("点击音效");
        if(this.renwunvhai.active){
            if(this.shangbaNode.active == false && this.kuaidi206Node.active == false){
                this.nextSelfNodeAnim()
            }else{
                this.nextSelfNodeAnim()
                //shibai
                this.scheduleOnce(()=>{
                    let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
                    scrc.nvhaiFail()
                },1.5)
        
            }
        
        }
    }

    laotaiHideFailGame(){
        Common5.playEffect("女生惨叫");
        cc.tween(this.renwulaotai)
        .to(1.5, {opacity:0})
        .call(
            ()=>{
                let scrc:GuiYiKuaiDiScript = this.GuiYiKuaiDiNode.getComponent('GuiYiKuaiDiScript')
                scrc.laoTaiFail()
            }
        )
        .start()
    }
    // update (dt) {}
}