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 CccGame from "../../../WordGame/gameComScript/CccGame/CccGame";
import ChaDianListScript from "../../../WordGame/gameComScript/ChaDianListScript";
import ChooseBtnLayer from "../../../WordGame/gameComScript/ChooseBtnLayer";
import logPrefabScript from "../../../WordGame/gameComScript/logPrefabScript";
// import BiHaiTaoShengConfig from "./BiHaiTaoShengConfig";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BiHaiTaoShengScript extends WordGameBaseComponent {

    @property(cc.Node)
    layerNode: cc.Node[] = [];

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

    //当前层级下标
    curLayerIndex = 0;
    //当前进度
    curSchedule = 0;
    chadianNum = 0;
    chooseIndex: number = -1;
    chooseRecord: number[] = [];

    //文本配置
    titleArrayConfig: string[] = [];
    duihuaArrayConfig: any[] = [];
    chadianListConfig: any = null;
    chooseArrayConfig: any = null;



    start() {
        super.start();
        DaDianScript.userEnterDaDian();
        this.openTouchEvent(this.node.getChildByName('bg'));
        Common5.stopMusic()

        //Common5.playMusic("bihaitaosheng/背景音", "WordGame");

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

            this.jsonData = assest.json

            this.initParameters();
            this.initComponent();
        })
    }

    initParameters() {
        this.curLayerIndex = 0;
        this.curSchedule = 0;
        this.chadianNum = 0;
        this.chooseIndex = -1;
        this.chooseRecord = [];
        this.titleArrayConfig = this.jsonData.titleArray//BiHaiTaoShengConfig.getInstance().getGameConfig('titleArray') as string[];
        this.duihuaArrayConfig = this.jsonData.duihuaArray//BiHaiTaoShengConfig.getInstance().getGameConfig('duihuaArray') as any[];
        this.chadianListConfig = this.jsonData.chadianList//BiHaiTaoShengConfig.getInstance().getGameConfig('chadianList');
        this.chooseArrayConfig = this.jsonData.chooseArray//BiHaiTaoShengConfig.getInstance().getGameConfig('chooseArray');
        Common.Type = 0;
        Common.subLevel = this.curLayerIndex;
        Common.GameSubTipConfigs = this.jsonData.tipsArray//BiHaiTaoShengConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = this.jsonData.answersArray//BiHaiTaoShengConfig.getInstance().getGameConfig('answersArray');
    }

    initComponent() {
        for (const node of this.layerNode) {
            node.active = false;
        }

        this.refreshLayer();

        EventMgr.onEvent_custom(ryw_Event.touchChaDianCheck, (data_) => {
            this.gameChaDianStep(data_.touchIndex);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this);

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

    refreshLayer() {

        let node = this.layerNode[this.curLayerIndex];
        node.active = true;

        if (this.curLayerIndex == 3) {
            if (this['success1']) {
                this.showDuiHua(this.duihuaArrayConfig[0], () => {
                    this.showDuiHua(this.duihuaArrayConfig[1], () => {
                        this.endGameView(1);
                    })
                })
            } else {
                this.showDuiHua(this.duihuaArrayConfig[2], () => {
                    this.endGameView(0);
                })
            }
            return;
        }

        switch (this.curLayerIndex) {
            case 0:
                Common5.playMusicCustom("bihaitaosheng", 'sound/场景一场景二');
                break;
            case 1:
                Common5.playMusicCustom("bihaitaosheng", 'sound/场景一场景二');
                break;
            case 2:
                Common5.playMusicCustom("bihaitaosheng", 'sound/场景三背景音');
                break;
            default:
                break;
        }

        let title = this.titleArrayConfig[this.curLayerIndex];
        let lab = this.node.getChildByName("标题").getChildByName("lab");
        lab.getComponent(cc.Label).string = title;

        Game.ins.setGameTitle(title)
        this.refreshChadian();
    }

    refreshChadian() {
        let node = this.layerNode[this.curLayerIndex];
        let chadianIconList = node.getChildByName('chadianIconList');
        let chadianTouchList = node.getChildByName('chadianTouchList');
        chadianIconList.active = true;
        chadianTouchList.active = true;

        let config = this.chadianListConfig[this.curLayerIndex];
        if (config) {
            let chadianIconList = node.getChildByName('chadianIconList');
            let scr: ChaDianListScript = chadianIconList.getComponent('ChaDianListScript');
            scr.setChaDianData(config.chadianIconList);
        }

        this.curSchedule = 0;
    }

    refreshConclusion() {
        let node = this.layerNode[this.curLayerIndex];
        let chadianIconList = node.getChildByName('chadianIconList');
        let chadianTouchList = node.getChildByName('chadianTouchList');
        chadianIconList.active = false;
        chadianTouchList.active = false;

        if (this.curLayerIndex == 0) {
            node.getChildByName("1").active = false;
            node.getChildByName("role").active = false;
            Common5.playEffectCustom("bihaitaosheng", 'sound/下滑');
            CccGame.playAnimation(node.getChildByName('xiahuazhuiluo'), 'xiahuazhuiluo');
            this.scheduleOnce(() => {
                Common5.playEffectCustom("bihaitaosheng", 'sound/女惨叫');
            }, 0.5);
            this.scheduleOnce(() => {
                this.nextLayer();
            }, 2);
            // node.getChildByName("2").active = false;
            // node.getChildByName("1").active = true;
            // Common5.playEffectCustom("bihaitaosheng", 'sound/电话响铃');
            // this.scheduleOnce(() => {
            //     this.showChooseLayer(0);
            // }, 1);
        } else if (this.curLayerIndex == 1) {
            this.scheduleOnce(() => {
                this.showChooseLayer(1);
                let timer = 100;
                let progressBar = this.chooseLayer.getChildByName('ProgressBar');
                progressBar.active = true;
                this.schedule(() => {
                    if (!this['isEndTimer']) {
                        timer--;
                        progressBar.getComponent(cc.ProgressBar).progress = timer / 100;
                        if (timer == 0) {
                            this['isEndTimer'] = true;
                            this.chooseLayer.active = false;
                            this.scheduleOnce(() => {
                                Common5.playEffectCustom("bihaitaosheng", 'sound/女惨叫');
                            }, 1);
                            CccGame.playAnimation(node.getChildByName('2'), 'shazi');
                            this.scheduleOnce(() => {
                                this.endGameView(0);
                            }, 2);
                        }
                    }
                }, 0.1, 99, 0);
                node.getChildByName('跳崖').active = true;
            }, 1);
        } else if (this.curLayerIndex == 2) {
            // if (this.chooseRecord[0] == 1) {
            //     this.showDuiHua(this.duihuaArrayConfig[2], () => {
            //         this.endGameView(0);
            //     })
            // } else {
            //     CccGame.playAnimation(node.getChildByName('1'), 'chuan');
            //     Common5.playEffectCustom("bihaitaosheng", 'sound/船出现');
            // }
            CccGame.playAnimation(node.getChildByName('1'), 'chuan');
            Common5.playEffectCustom("bihaitaosheng", 'sound/船出现');
        }

        this.curSchedule = 1;
    }

    gameChaDianStep(touchIndex) {
        let config = this.chadianListConfig[this.curLayerIndex];
        let chadianLog = config.chadianLog;

        this.showDuiHua(chadianLog[touchIndex]);
        console.log(this.chadianNum, '  this.chadianNum++=');

        if (this.curSchedule == 0) {
            this.chadianNum++;
            if (this.chadianNum == chadianLog.length) {
                this.chadianNum = 0;
                this.scheduleOnce(() => {
                    this.refreshConclusion();
                }, 3);
            }
        }
    }

    normalTouchCallback(targetNode) {
        let node = this.layerNode[this.curLayerIndex];
        switch (targetNode.name) {
            case "跳崖":
                this['isEndTimer'] = true;
                this.chooseLayer.active = false;
                CccGame.playAnimation(node.getChildByName('2'), 'rentiao');
                this.scheduleOnce(() => {
                    this.nextLayer();
                }, 2);
                break;
            case "枪":
                // if (targetNode.checkNode.name == "A") {
                //     CccGame.playAnimation(node.getChildByName('0'), 'yanhua', true);
                //     Common5.playEffectCustom("bihaitaosheng", 'sound/信号枪');
                //     this['success1'] = true;
                // }
                CccGame.playAnimation(node.getChildByName('0'), 'yanhua', true);
                Common5.playEffectCustom("bihaitaosheng", 'sound/信号枪');
                this['success1'] = true;
                this.scheduleOnce(() => {
                    this.nextLayer();
                }, 2);
                break;
            default:
                break;
        }
    }

    chooseResultCallback(touchIndex: number) {
        this.node.getChildByName("mask").active = false;
        this.chooseRecord[this.chooseIndex] = touchIndex;

        let node = this.layerNode[this.curLayerIndex];
        if (this.curLayerIndex == 0) {
            node.getChildByName("1").active = false;
            node.getChildByName("role").active = false;
            Common5.playEffectCustom("bihaitaosheng", 'sound/下滑');
            CccGame.playAnimation(node.getChildByName('xiahuazhuiluo'), 'xiahuazhuiluo');
            this.scheduleOnce(() => {
                Common5.playEffectCustom("bihaitaosheng", 'sound/女惨叫');
            }, 0.5);
            this.scheduleOnce(() => {
                this.nextLayer();
            }, 2);
        } else if (this.curLayerIndex == 1) {
            this['isEndTimer'] = true;
            this.chooseLayer.active = false;
            let name = ['shazi', 'renxiangshang'][touchIndex];
            CccGame.playAnimation(node.getChildByName('2'), name);
            this.scheduleOnce(() => {
                Common5.playEffectCustom("bihaitaosheng", 'sound/女惨叫');
            }, 1);
            this.scheduleOnce(() => {
                this.endGameView(0);
            }, 2);
        }
    }

    //显示选项
    showChooseLayer(chooseIndex: number) {
        this.chooseLayer.active = true;
        //this.node.getChildByName("mask").active = true;

        this.chooseIndex = chooseIndex;
        Common.GameSubChooseArray = this.chooseArrayConfig[chooseIndex];
        let script_: ChooseBtnLayer = this.chooseLayer.getComponent('ChooseBtnLayer');
        script_.setChooseBtnView();
    }

    //显示对话
    showDuiHua(curLog, func?) {
        console.log("curLog==", curLog);

        if (curLog.str == '') {
            return;
        }

        if (curLog.effectUrl) {
            Common5.playEffectCustom("bihaitaosheng", curLog.effectUrl);
        }

        let string_ = curLog.str
        let qiPaoPos_ = curLog.qiPaoPos
        let delayTime_ = curLog.delayTime || 3;

        let node = this.layerNode[this.curLayerIndex];
        if (qiPaoPos_ != -1) {
            let qiPao = node.getChildByName("qiPao").getChildByName("qiPao_" + qiPaoPos_);
            qiPao.stopAllActions()
            qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            qiPao.active = true
            qiPao.scale = 0
            cc.tween(qiPao)
                .to(0.2, { scale: 1 })
                .delay(delayTime_)
                .call(() => {
                    qiPao.active = false
                })
                .start();
        }
        else {
            let logPrefab = this.node.getChildByName('logPrefab')
            let script_: logPrefabScript = logPrefab.getComponent('logPrefabScript')
            script_.setDailogShow(string_, delayTime_)
        }

        this.scheduleOnce(() => {
            if (func) {
                func();
            }
        }, delayTime_);
    }

    nextLayer() {
        this.curLayerIndex++;
        Common.subLevel = this.curLayerIndex;
        Game.ins.updateTishiBtn()
        for (let i = 0; i < this.layerNode.length; i++) {
            if (this.layerNode[i].active) {
                cc.tween(this.layerNode[i])
                    .delay(1.0)
                    .to(1, { opacity: 0 })
                    .call(() => {
                        this.layerNode[i].active = false;
                        this.refreshLayer();
                    })
                    .start();
            } else {
                this.layerNode[i].active = false;
            }
        };
    }

    endGameView(touchIndex) {
        this.node.getChildByName("mask").active = true;
        Game.ins.stopTime();
        if (touchIndex == 0) {

            this.scheduleOnce(() => {
                Game.ins.showFail();
            }, 3.0)
        } else {
            this.scheduleOnce(() => {
                Game.ins.showSuccess();
            }, 3.0)
        }
    }

    openTouchEvent(node) {
        node.on(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.on(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }

    closeTouchEvent(node: cc.Node) {
        node.off(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.off(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }

    touchStartNode(event) {
        let target = event.target;
        console.log('touchStartNode');
    }

    touchMoveNode(event) {

    }

    touchEndNode(event) {
        let target = event.target
        let logPrefab = this.node.getChildByName('logPrefab')
        logPrefab.getChildByName('duihua').active = false;
    }

    //update(dt) { }
}