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

const { ccclass, property } = cc._decorator;

@ccclass
export default class TiePiXiangTaoShengScript extends WordGameBaseComponent {

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

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

    //当前层级下标
    curLayerIndex = 0;
    //当前进度
    curSchedule = 0;
    chadianNum = 0;
    curChooseArray: Map<number, number> = null;
    chooseIndex: number = -1;
    curChooseEmitNode: cc.Node = null;

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


    start() {
        super.start();
        DaDianScript.userEnterDaDian();
        this.openTouchEvent(this.node.getChildByName('bg'));
        // this.initParameters();
        // this.initComponent();

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

            this.jsonData =assest.json

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


        Common5.playMusicCustom('tiepixiangtaosheng', 'sound/背景音');

    }

    initParameters() {
        this.curLayerIndex = 0;
        this.curSchedule = 0;
        this.chadianNum = 0;
        this.chooseIndex = -1;
        this.curChooseArray = new Map();
        this.titleArrayConfig = this.jsonData.titleArray//TiePiXiangTaoShengConfig.getInstance().getGameConfig('titleArray') as string[];
        this.duihuaArrayConfig = this.jsonData.duihuaArray//TiePiXiangTaoShengConfig.getInstance().getGameConfig('duihuaArray') as any[];
        this.chadianListConfig = this.jsonData.chadianList//TiePiXiangTaoShengConfig.getInstance().getGameConfig('chadianList');
        this.chooseListConfig = this.jsonData.chooseList//TiePiXiangTaoShengConfig.getInstance().getGameConfig('chooseList');
        Common.Type = this.curLayerIndex;
        Common.subLevel = this.curLayerIndex;
        Common.GameSubTipConfigs = this.jsonData.tipsArray//TiePiXiangTaoShengConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = this.jsonData.answersArray//TiePiXiangTaoShengConfig.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;

        let title = this.titleArrayConfig[0];
        let lab = this.node.getChildByName("标题").getChildByName("lab");
        lab.getComponent(cc.Label).string = title;
        Game.ins.setGameTitle(title)
        if (this.curLayerIndex == 0) {
            this.showDuiHua(this.duihuaArrayConfig[0], () => {
                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[0];
        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;

        switch (this.curLayerIndex) {
            case 0:
                node.getChildByName("2").active = false;
                node.getChildByName("3").active = true;
                this.showDuiHua(this.duihuaArrayConfig[1], () => {
                    this.showDuiHua(this.duihuaArrayConfig[2], () => {
                        this.nextLayer();
                    });
                });
                break;

            default:
                break;
        }

        this.curSchedule = 1;
    }

    gameChaDianStep(touchIndex) {
        let config = this.chadianListConfig[0];
        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) {
        this.curChooseEmitNode = targetNode;
        switch (targetNode.name) {
            case "喇叭1":
                this.node.getChildByName("mask").active = true;
                Common5.playEffectCustom("tiepixiangtaosheng", "sound/喇叭声");
                this.scheduleOnce(() => {
                    this.showChooseLayer(0);
                }, 2);

                break;
            case "喇叭2":
                this.node.getChildByName("mask").active = true;
                Common5.playEffectCustom("tiepixiangtaosheng", "sound/风声");
                this.scheduleOnce(() => {
                    this.showChooseLayer(1);
                }, 2);
                break;
            case "海水":
                this.node.getChildByName("mask").active = true;
                this.showDuiHua(this.duihuaArrayConfig[3], () => {
                    this.showChooseLayer(2);
                });
                break;
            case "柴油":
                this.node.getChildByName("mask").active = true;
                this.showDuiHua(this.duihuaArrayConfig[4], () => {
                    this.showChooseLayer(3);
                });
                break;
            case "港口电话名片":
                this.layerNode[1].getChildByName("timer").active = false;
                this.showDuiHua(this.duihuaArrayConfig[5], () => {
                    if (this.curChooseArray.get(0) == 0
                        && this.curChooseArray.get(1) == 1
                        && this.curChooseArray.get(2) == 0
                        && this.curChooseArray.get(3) == 1) {
                        this.gameEndLayer(1);
                    } else {
                        this.gameEndLayer(0);
                    }
                });
                break;
            default:
                break;
        }
    }

    chooseResultCallback(touchIndex: number) {
        this.node.getChildByName("mask").active = false;
        if (touchIndex >= 2) {
            this.curChooseEmitNode.active = true;
        } else {
            this.curChooseArray.set(this.chooseIndex, touchIndex);
            this.showDuiHua(this.chooseListConfig.chooseLog[this.chooseIndex][touchIndex]);
            let lianxiangList = this.layerNode[1].getChildByName("lianxiangList");
            let lianxiangNode = lianxiangList.children[this.chooseIndex].children[touchIndex];
            cc.tween(lianxiangNode)
                .set({ active: true, opacity: 0 })
                .to(0.2, { opacity: 255 })
                .delay(3)
                .set({ active: false, opacity: 0 })
                .start();
        }

        this.scheduleOnce(() => {
            if (this.curChooseArray.size == this.chooseListConfig.chooseLog.length) {
                // this.layerNode[1].getChildByName("纸片").active = true;
                // this.showDuiHua(this.duihuaArrayConfig[11], () => {
                //     let timerNode = this.layerNode[1].getChildByName("timer");
                //     timerNode.active = true;
                //     let timerLabel = timerNode.getComponent(cc.Label);
                //     let timerNum = 5;
                //     this.schedule(() => {
                //         timerLabel.string = timerNum.toString();
                //         if (timerNum == 0) {
                //             this.node.getChildByName("mask").active = true;
                //             if (this.curChooseEmitNode.name == "popUpNode") {
                //                 return;
                //             } else {
                //                 timerNode.active = false;
                //                 this.showDuiHua(this.duihuaArrayConfig[12], () => {
                //                     this.gameEndLayer(0);
                //                 });
                //             }
                //         }
                //         timerNum--;
                //     }, 1, 5, 0);
                // });
                if (this.curChooseArray.get(0) == 0
                    && this.curChooseArray.get(1) == 1
                    && this.curChooseArray.get(2) == 0
                    && this.curChooseArray.get(3) == 1) {
                    this.gameEndLayer(1);
                } else {
                    this.gameEndLayer(0);
                }
            }
        }, 4);
    }


    gameEndLayer(touchIndex) {
        this.node.getChildByName("mask").active = true;
        this.showDuiHua(this.duihuaArrayConfig[6], () => {
            let narratage = this.node.getChildByName("narratage");
            narratage.getChildByName("bar").getChildByName("text").getComponent(cc.Label).string = "一个小时后";
            cc.tween(narratage)
                .set({ active: true, opacity: 0 })
                .to(0.2, { opacity: 255 })
                .delay(3)
                .set({ active: false, opacity: 0 })
                .call(() => {
                    this.curLayerIndex++;
                    this.layerNode[1].active = false;
                    let node = this.layerNode[2];
                    node.active = true;
                    if (touchIndex == 1) {
                        node.getChildByName("成功").active = true;
                        this.showDuiHua(this.duihuaArrayConfig[7], () => {
                            this.endGameView(touchIndex);
                        });
                    } else {
                        let shibai = node.getChildByName("失败");
                        shibai.active = true;
                        this.showDuiHua(this.duihuaArrayConfig[8], () => {
                            this.showDuiHua(this.duihuaArrayConfig[9], () => {
                                let shuibo = shibai.getChildByName("shuibo");
                                shuibo.active = true;
                                shuibo.getComponent(sp.Skeleton).setAnimation(0, "shuibo", false);
                                this.showDuiHua(this.duihuaArrayConfig[10], () => {
                                    this.endGameView(touchIndex);
                                });
                            });
                        });
                    }
                })
                .start();
        });
    }

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

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

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

        if (curLog.effectUrl) {
            Common5.playEffectCustom("tiepixiangtaosheng", 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++;
        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();
        this.scheduleOnce(() => {

            // this.node.getChildByName("mask").active = true;
            if (touchIndex == 0) {
                Game.ins.showFail();;
            } else {
                 Game.ins.showSuccess();;
            }
        }, 3)
    }

    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) { }
}