// 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 DaDianScript from "../../../../FrameWork/Base/DaDianScript";
import WordGameBaseComponent from "../../../../FrameWork/Base/WordGameBaseComptent";
import { ryw_Event } from "../../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../../FrameWork/Event/EventMgr";
import Common from "../../../../FrameWork/Util/Common";
import Common5 from "../../../../Platform/th/Common5";
import Game from "../../../../Scripts/WenZiRes/ui/Game";
import ChooseBtnLayer from "../../../WordGame/gameComScript/ChooseBtnLayer";
import DuiHuaScript from "../../../WordGame/guizeGame/comScript/DuiHuaScript";
import GZTipBtnScript from "../../../WordGame/guizeGame/comScript/GZTipBtnScript";
import GuiZeLayer from "../../../WordGame/guizeGame/comScript/GuiZeLayer";
import DianTiScript from "./DianTiScript";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GuiYiKuaiDiScript extends WordGameBaseComponent {

    @property(cc.Node)
    layerArray: cc.Node[] = []

    @property(cc.Node)
    resultLayer: cc.Node[] = []

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

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

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

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

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

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    curVdTipIndex = 0 //当前提示界面的索引0,1,2,3,4,5,6,
    duihuaConfig = null
    resultDuihuaArray = null
    titleArrayConfig = null
    curLayerIndex = 0
    stepIndex = 0 //对话有用
    curTime = 0

    timeAngle = 30
    kuaidiDesc: string;
    IsChoose = false;
    isQiaomen = false;
    bundleName = null

    onLoad(): void {
       
    }

    start() {
        super.start();
        DaDianScript.userEnterDaDian()
        Common5.playMusicCustom('guiyiKuaidi','sound/背景音')

        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle,'script/GuiYiKuaiDiConfig',(assest)=>{
            //this.jsonData = JSON.stringify(assest.json)

            this.jsonData =assest.json

            Common.Type = 0;
            Common.subLevel = this.curLayerIndex
            Common.GameSubTipConfigs = this.jsonData.tipsArray //GuiYiKuaiDiConfig.getInstance().getGameConfig('tipsArray')
            Common.GameSubAnswerConfigs = this.jsonData.answersArray//GuiYiKuaiDiConfig.getInstance().getGameConfig('answersArray');
    
    
    
            Common.GameSubChooseArray = this.jsonData.chooseArray//GuiYiKuaiDiConfig.getInstance().getGameConfig('chooseArray')
            Common.GameguizeArray = this.jsonData.GameguizeArray//GuiYiKuaiDiConfig.getInstance().getGameConfig('GameguizeArray')
    
            this.titleArrayConfig = this.jsonData.titleArray//GuiYiKuaiDiConfig.getInstance().getGameConfig('titleArray')
            this.duihuaConfig = this.jsonData.duihuaArray//GuiYiKuaiDiConfig.getInstance().getGameConfig('duihuaArray')
            this.resultDuihuaArray = this.jsonData.resultDuihuaArray//GuiYiKuaiDiConfig.getInstance().getGameConfig('resultDuihuaArray')
            this.bundleName = this.jsonData.bundleName//GuiYiKuaiDiConfig.getInstance().getGameConfig('bundleName')

            this.refreshTipBtn()
            this.refreshTitle()
            this.showDuihua(this.stepIndex)
            this.setpIndexAdd()
        })

        
        EventMgr.onEvent_custom(ryw_Event.GuiZeGameBtnClick, (data) => {
            let touchIndex = data.touchIndex
            if (this.curLayerIndex == 0) {
                this.changeCurTipIndex(1)
            } else if (this.curLayerIndex == 1) {
                this.changeCurTipIndex(3)
            } else if (this.curLayerIndex == 2) {
                this.changeCurTipIndex(5)
            }
            this.gameStep(touchIndex)
        }, this)

        EventMgr.onEvent_custom(ryw_Event.openGuizeLayerEvent, (data) => {
            this.guizeBtnLayer()
        }, this)

        EventMgr.onEvent_custom(ryw_Event.chooseResult, (data_) => {
            this.chooseResult(data_.touchIndex)

        }, this)

        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.showLaotpuNode(data_.targetNode)

        }, this)

        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck, (data_) => {
            this.showLaotpuNode(data_.targetNode)

        }, this)

    }

    showLaotpuNode(targetNode) {
        let node = this.layerArray[this.curLayerIndex]
        console.log('dadengButton11')

        if (node.getChildByName('tuicheButton')) {
            if (targetNode == node.getChildByName('tuicheButton')) {
                node.getChildByName('小人').active = true
                this.showDuihua(this.stepIndex)
                this.setpIndexAdd()
            } else if (targetNode == node.getChildByName('dadengButton')) {
                console.log('dadengButton')
                let heiying = node.getChildByName('heiying')
                heiying.active = true
                cc.tween(heiying)
                    .to(2.0, { opacity: 0 })
                    .start()
            }
        } else if (node.getChildByName('qiaomen')) {

            if (targetNode == node.getChildByName('qiaomen')) {
                this.isQiaomen = true
                let stepIndex = 11
                this.showDuihua(stepIndex)
            }
        }

    }

    gameStep(touchIndex) {
        console.log(touchIndex, 'gameStep+11++++++++++++======')
        console.log(this.stepIndex, ' this.stepIndex+11++++++++++++======')

        if (touchIndex == this.stepIndex) {
            console.log(touchIndex, 'gameStep+22++++++++++++======')
            if (this.stepIndex == 1) { //规则一
                this.showGuizeLayer()
                this.showDuihua(this.stepIndex)
                this.setpIndexAdd()
            } else if (this.stepIndex == 2) { // 进入第二层

                this.showDuihua(this.stepIndex)
                this.setpIndexAdd()
            } else if (this.stepIndex == 3) {
                // this.showGuizeLayer()
                // this.showDuihua(this.stepIndex)
                // this.setpIndexAdd()
            } else if (this.stepIndex == 4) {//规则2
                this.showGuizeLayer()

            } else if (this.stepIndex == 6) {//规则3
                this.showGuizeLayer()
                // this.showDuihua(this.stepIndex)
                // this.setpIndexAdd()
            }
        }
    }

    changeLayer() {

    }

    refreshTitle() {

        let title = this.titleArrayConfig[this.curLayerIndex]
        let titleLab = this.node.getChildByName('标题').getChildByName('lab').getComponent(cc.Label)
        if (titleLab.string) {
            titleLab.string = title
        }
        Game.ins.setGameTitle(title)
    }

    refreshTipBtn() {
        // let tipScr: GZTipBtnScript = this.GZTipBtn.getComponent('GZTipBtnScript')
        // tipScr.refreshTipsCurView()

        // let title = this.titleArrayConfig[this.curLayerIndex]
        // let titleLab = this.node.getChildByName('标题').getChildByName('lab').getComponent(cc.Label)
        // if (titleLab.string) {
        //     titleLab.string = title
        // }

    }

    showGuizeLayer() {
        this.GuiZeLayer.active = true
        let script_: GuiZeLayer = this.GuiZeLayer.getComponent('GuiZeLayer')
        script_.setGuizeArray(this.curLayerIndex)
    }

    guizeBtnLayer() {
        this.GuiZeLayer.active = true
        let script_: GuiZeLayer = this.GuiZeLayer.getComponent('GuiZeLayer')
        script_.refreshView()
    }

    showDuihua(stepIndex, func?: Function) {
        this.DuiHuaPrefab.active = true
        let script_: DuiHuaScript = this.DuiHuaPrefab.getComponent('DuiHuaScript')
        let duihuaArray = this.duihuaConfig[stepIndex]
        script_.setDuihuaArray(this.bundleName,duihuaArray, func)

    }

    showChooseDuihua(duihuaArray, func?: Function) {
        this.DuiHuaPrefab.active = true
        let script_: DuiHuaScript = this.DuiHuaPrefab.getComponent('DuiHuaScript')
        script_.setDuihuaArray(this.bundleName,duihuaArray, func)

    }


    showDuihuaResult(duihuaIndex, func?: Function) {
        this.DuiHuaPrefab.active = true
        let script_: DuiHuaScript = this.DuiHuaPrefab.getComponent('DuiHuaScript')
        let duihuaArray = this.resultDuihuaArray[duihuaIndex]
        console.log(duihuaArray, 'duihuaArray+++++++++++==')
        script_.setDuihuaArray(this.bundleName,duihuaArray, func)
    }



    setpIndexAdd() {
        this.stepIndex++
        console.log(this.stepIndex, 'setpIndexAdd setpIndexAdd+++++++++++===')
    }

    // update (dt) {}
    gameEnd() {
        console.log('game end+++++++++++===')
    }


    qidongClickEvent(event, data_) {
        Common5.playEffect("点击音效");
        console.log(data_, 'data_')
        //快递, 变颜色
        let target = event.target
        target.active = false
        this.showDuihua(this.stepIndex, () => {
            let node = this.layerArray[this.curLayerIndex]
            let kuaidi1 = node.getChildByName('kuaidi1')
            let kuaidi2 = node.getChildByName('kuaidi2')
            let kuaidi3 = node.getChildByName('kuaidi3')
            let kuaidi4 = node.getChildByName('kuaidi4')

            let blackMotuo = node.getChildByName('摩托车2')
            if (blackMotuo.active && (kuaidi1.active == false && kuaidi2.active == false && kuaidi3.active && kuaidi4.active) && this.timeAngle % 360 >= -150 && this.timeAngle % 360 <= 0 && this.isQiaomen) {
                console.log('进入下一层')

                this.refreCurLayerView()


            } else {
                console.log('不能进入下一层失败')
                this.failShow(this.curLayerIndex)
            }
        })
        this.setpIndexAdd()




    }

    failShow(curLayerIndex, isshowDuihua = true, callFunc?: Function) {

        this.scheduleOnce(() => {

        }, 0.1)

        Common5.playEffect("人物跳脸");
        let node = this.resultLayer[curLayerIndex]
        let renwuNode = node.getChildByName('renwuNode')
        renwuNode.scale = 0.01
        let zhuahengBg = node.getChildByName('zhuahengBg')
        node.active = true
        cc.tween(renwuNode)
            .to(0.3, { scale: 1 }, { easing: 'elasticOut' })
            .delay(1.0)
            .call(() => {

                if (zhuahengBg) {

                    Common5.playEffect("跳脸抓痕");
                    zhuahengBg.active = true
                    let zhuhen = zhuahengBg.getChildByName('抓痕')
                    zhuhen.getComponent(sp.Skeleton).setAnimation(0, 'animation', false)
                }
                if (isshowDuihua) {
                    this.showDuihuaResult(curLayerIndex)
                }
                if (callFunc) {
                    callFunc()
                }
                this.scheduleOnce(() => {
                    Game.ins.showFail();
                }, 2)
            })
            .start()

    }
    refreCurLayerView() {

        let node = this.layerArray[this.curLayerIndex]
        cc.tween(node)
            .to(3.0, { opacity: 0 })

            .delay(1.0)
            .call(() => {
                this.curLayerIndex++
                if(this.curLayerIndex<=Common.GameSubAnswerConfigs.length-1){
                    Common.subLevel = this.curLayerIndex
                    Game.ins.updateTishiBtn()
                }
                let curVdTipIndex = this.curLayerIndex * 2
                this.changeCurTipIndex(curVdTipIndex)
                let node_1 = this.layerArray[this.curLayerIndex]
                node_1.active = true
                this.showDuihua(this.stepIndex)
                this.setpIndexAdd()
                this.refreshTitle()
                node.active = false
            })
            .start()
    }
    //调整时间

    enterDoorClickEvent() {
        Common5.playEffect("点击音效");
        let ChooseLayer = this.node.getChildByName('GZChooseLayer2')
        ChooseLayer.active = true

        Common.GameSubChooseArray = this.jsonData.chooseArray//GuiYiKuaiDiConfig.getInstance().getGameConfig('chooseArray')

        let script_: ChooseBtnLayer = ChooseLayer.getComponent('ChooseBtnLayer')
        script_.setChooseBtnView()
    }




    kuaidi206ClickEvent() {

        this.kuaidiDesc = 'kuaidi206'
        let stepIndex = 6
        this.showDuihua(
            stepIndex,

            () => {
                let ChooseLayer = this.node.getChildByName('ChooseLayer')
                ChooseLayer.active = true
                this.setChooseCloseBtn(true)

                Common.GameSubChooseArray = this.jsonData.chooseArray1//GuiYiKuaiDiConfig.getInstance().getGameConfig('chooseArray1')

                let script_: ChooseBtnLayer = ChooseLayer.getComponent('ChooseBtnLayer')
                script_.setChooseBtnView()
            }
        )
        this.setpIndexAdd()



    }

    kuaidi602Event() {
        let stepIndex = 9
        this.showDuihua(
            stepIndex,
        )
    }
    kuaidi606Event() {
        let stepIndex = 10
        this.showDuihua(
            stepIndex,
        )
    }

    penwu606ClickEvent() {
        let stepIndex = 7
        this.showDuihua(
            stepIndex,
        )
        this.setpIndexAdd()
    }

    hongyiClickEvent() {
        this.kuaidiDesc = 'hongyi'
        let stepIndex = 8
        this.showDuihua(
            stepIndex,

            () => {
                let ChooseLayer = this.node.getChildByName('GZChooseLayer2')
                ChooseLayer.active = true
                //this.setChooseCloseBtn(false)
                Common.GameSubChooseArray = this.jsonData.chooseArray2//GuiYiKuaiDiConfig.getInstance().getGameConfig('chooseArray2')

                let script_: ChooseBtnLayer = ChooseLayer.getComponent('ChooseBtnLayer')
                script_.setChooseBtnView()
            }
        )
        this.setpIndexAdd()


    }



    chooseResult(touchIndex) {


        if (Common.GameSubChooseArray.btnEffectArray) {
            let duihuaArray = Common.GameSubChooseArray.btnEffectArray[touchIndex]
            if (duihuaArray) {
                console.log(duihuaArray, 'duihuaArray+++===')
                this.showChooseDuihua(duihuaArray)
            }

        }

        if (this.curLayerIndex == 1) {
            let node = this.layerArray[this.curLayerIndex]
            let tuiche = node.getChildByName('tuiche')
            let dadeng = node.getChildByName('dadeng')

            let ChooseLayer = this.node.getChildByName('ChooseLayer')
            ChooseLayer.active = false


            if (touchIndex == 0) {


                if (tuiche.active && dadeng.active) {
                    console.log('能进入下一层')
                    this.refreCurLayerView()
                } else {
                    console.log('第二次失败')
                    this.failShow(this.curLayerIndex)
                }
            } else {
                console.log('第二次失败')
                this.failShow(this.curLayerIndex)

            }
        } else if (this.curLayerIndex == 2) {
            let ChooseLayer = this.node.getChildByName('ChooseLayer')
            ChooseLayer.active = false
            if (this.kuaidiDesc == 'hongyi') {
                //失败
                //电梯抖动失败
                if (touchIndex == 0) {
                    //进来
                    let node = this.layerArray[this.curLayerIndex]
                    let scrnode: DianTiScript = node.getComponent('DianTiScript')
                    scrnode.diantiAninFail(() => {
                        let curLayerIndex = 3
                        this.showDuihuaResult(curLayerIndex)
                    }, () => {
                        //失败
                        let index = 3
                        this.failShow(index, false)
                    })
                } else {
                    //拒接
                    let index = 3
                    this.failShow(index, false)
                }



            } else {
                if (touchIndex == -1) {
                    this.IsChoose = true
                    let node = this.layerArray[this.curLayerIndex]
                    let scrnode: DianTiScript = node.getComponent('DianTiScript')
                    scrnode.nextSelfNodeAnim()
                } else {
                    //失败
                    let index = 2
                    this.failShow(index)
                }

            }
        }
    }

    getIsChooseTrue() {
        return this.IsChoose
    }


    successUI() {
        this.scheduleOnce(() => {
             Game.ins.showSuccess();;
        }, 2.0)
    }

    laotaiFail2() {
        let index = 6
        this.failShow(index)
    }

    nvhaiFail() {
        let index = 4
        this.failShow(index)
    }

    closeSelfClickEvent(event) {
        Common5.playEffect("点击音效");
        let target = event.target
        target.active = false
    }

    addTimeClickEvent(event) {
        Common5.playEffect("点击音效");
        let target = event.target
        let shizhen = target.getChildByName('时针') //时针
        this.timeAngle -= 30
        console.log(this.timeAngle, 'this.timeAngle++++++++++++====')
        shizhen.angle = this.timeAngle


        console.log(this.timeAngle % 360, 'this.timeAngle%360++++++++++++====')

        let node = this.layerArray[this.curLayerIndex]
        let xiaoshishen = node.getChildByName('timeNode').getChildByName('表盘').getChildByName('时针')
        xiaoshishen.angle = this.timeAngle
    }


    setChooseCloseBtn(show) {
        let ChooseLayer = this.node.getChildByName('ChooseLayer')
        let script_: ChooseBtnLayer = ChooseLayer.getComponent('ChooseBtnLayer')
        script_.setCloseBtnShow(show)
    }

    setChooseLayerHide() {
        let ChooseLayer = this.node.getChildByName('ChooseLayer')
        ChooseLayer.active = false

        let ChooseLayer2 = this.node.getChildByName('GZChooseLayer2')
        ChooseLayer2.active = false
    }

    laoTaiFail() {
        let index = 5
        this.failShow(index, true, () => {
            let script_: DuiHuaScript = this.DuiHuaPrefab.getComponent('DuiHuaScript')
            script_.setDuihuaPosiChange()
        })
        //对话位置偏移
    }

    changeCurTipIndex(curVdTipIndex) {
        this.curVdTipIndex = curVdTipIndex
        //Common.subLevel = this.curVdTipIndex
        this.refreshTipBtn()
    }
}