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/Game";
import CccGame from "../../../WordGame/gameComScript/CccGame/CccGame";
import logPrefabScript from "../../../WordGame/gameComScript/logPrefabScript";
// import HunLiZhaoPianConfig from "./HunLiZhaoPianConfig";


const { ccclass, property } = cc._decorator;

@ccclass
export default class NiZhenShiELe extends WordGameBaseComponent {
    @property(cc.Node)
    layerNode: cc.Node[] = [];

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

    //当前层级下标
    curLayerIndex = 0;
    //当前进度
    curSchedule: number = 0;

    //文本配置
    titleArrayConfig: string[] = [];
    duihuaArrayConfig: any[] = [];
    chadianArrayConfig: any[] = [];
    wrongAnswerConfig: string[] = [];
    answersIndexConfig: number[] = [];

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

        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle, 'script/NiZhenShiELeConfig', (assest) => {
            this.jsonData = assest.json;
            this.initParameters();
            this.initComponent();
        })

    }

    //初始化参数
    initParameters() {
        this.curLayerIndex = 0;
        this.curSchedule = 0;

        this.titleArrayConfig = this.jsonData.titleArray;
        this.duihuaArrayConfig = this.jsonData.duihuaArray;
        this.chadianArrayConfig = this.jsonData.chadianArray;
        this.wrongAnswerConfig = this.jsonData.wrongAnswer;
        this.answersIndexConfig = this.jsonData.answersIndex;

        Common.Type = 0
        Common.subLevel = this.curLayerIndex;
        Common.GameSubTipConfigs = this.jsonData.tipsArray;
        Common.GameSubAnswerConfigs = this.jsonData.answersArray;

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

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

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

        EventMgr.onEvent_custom(ryw_Event.timeOut, (tab) => {
            this.endGameView(0);
        }, this);
    }

    //初始化组件
    initComponent() {
        for (const node of this.layerNode) {
            node.active = false;
        }
        let node = this.layerNode[this.curLayerIndex];
        node.active = true;

        Game.ins.setGameTitle(this.titleArrayConfig[0]);
        //Common5.playMusicCustom(Common5.selectGameInfo.bundle, 'sound/背景音');

        this.refreshLayer();
    }

    refreshLayer() {
        let node = this.layerNode[this.curLayerIndex];
        node.active = true;

        if (this.curLayerIndex == 0) {
            this.node.getChildByName("inputBlock").active = true;
            this.showDuiHua(this.duihuaArrayConfig[20], () => {
                this.node.getChildByName("inputBlock").active = false;
            });
        } else if (this.curLayerIndex == 1) {
            for (const iterator of this.getLayerChild('money1').children) {
                let callbacks = CccGame.onNodeTouchMoreCheckEvent(iterator, this.getLayerChild('chadianNodeList1').children);
                callbacks.setSuccessListener((data_) => {
                    this.nodeCheckSuccessCallback1(data_.targetNode);
                });
            }

            for (const iterator of this.getLayerChild('money2').children) {
                let callbacks = CccGame.onNodeTouchMoreCheckEvent(iterator, this.getLayerChild('chadianNodeList2').children);
                callbacks.setSuccessListener((data_) => {
                    this.nodeCheckSuccessCallback2(data_.targetNode);
                });
            }

            for (const iterator of this.getLayerChild('money3').children) {
                let callbacks = CccGame.onNodeTouchMoreCheckEvent(iterator, this.getLayerChild('chadianNodeList3').children);
                callbacks.setSuccessListener((data_) => {
                    this.nodeCheckSuccessCallback3(data_.targetNode);
                });
            }

            this.node.getChildByName("inputBlock").active = true;
            this.showDuiHua(this.duihuaArrayConfig[21], () => {
                this.node.getChildByName("inputBlock").active = false;
            });
        } else if (this.curLayerIndex == 2) {
            this.showDuiHua(this.duihuaArrayConfig[18], () => {
                this.showDuiHua(this.duihuaArrayConfig[19], () => {
                    this.endGameView(1);
                });
            });
        }
    }

    nodeCheckSuccessCallback1(targetNode: cc.Node) {
        let checkLogic = {
            轮椅背面女主: () => {
                this.getLayerChild('左边场景1', '场景二轮椅背面女主').active = false;
                this.getLayerChild('左边场景1', '站起来女').active = true;
                this.getLayerChild('chadianNodeList1', '站起来女').active = true;
                this.showDuiHua(this.duihuaArrayConfig[11]);
            },
            站起来女: () => {
                const callback = () => {
                    // let index = 0;
                    // const money = this.getLayerChild('money3').children;
                    // this.schedule(() => {
                    //     let curIndex = index, startPosi = money[index].getPosition();
                    //     cc.tween(money[curIndex])
                    //         .set({ active: true, position: cc.v3(25, 500, 0) })
                    //         .to(0.5, { position: startPosi })
                    //         .start();
                    //     index++;
                    // }, 0.2, 0, 0);
                    const money = this.getLayerChild('money3').children;
                    let startPosi = money[0].getPosition();
                    cc.tween(money[0])
                        .set({ active: true, position: cc.v3(25, 500, 0) })
                        .to(0.5, { position: startPosi })
                        .start();
                };
                this.showDuiHua(this.duihuaArrayConfig[12], () => {
                    cc.tween(this.getLayerChild('左边场景1', '站起来女'))
                        .to(0.3, { x: -500 })
                        .to(0.3, { x: -100 })
                        .call(callback)
                        .start();
                });
            },
        }

        console.log("[GameReport]++++++++++++++++++++++>" + targetNode.checkNode.name);
        checkLogic[targetNode.checkNode.name]?.();
    }

    nodeCheckSuccessCallback2(targetNode: cc.Node) {
        let checkLogic = {
            变成豪华客厅: () => {
                this.getLayerChild('右边场景1', '右边场景2').active = true;
                this.getLayerChild('chadianNodeList2', '男生穿着豪华西装').active = true;
                this.showDuiHua(this.duihuaArrayConfig[2]);
            },
            男生穿着豪华西装: () => {
                this.getLayerChild('右边场景1', '男主底图').active = false;
                this.getLayerChild('右边场景1', '男主西装底图').active = true;
                this.getLayerChild('chadianNodeList2', '老三穿着貂皮大衣').active = true;
                this.showDuiHua(this.duihuaArrayConfig[3]);
            },
            老三穿着貂皮大衣: () => {
                this.getLayerChild('右边场景1', '大衣').active = true;
                this.getLayerChild('chadianNodeList2', '老三带着宝石项链').active = true;
                this.showDuiHua(this.duihuaArrayConfig[4]);
            },
            老三带着宝石项链: () => {
                this.getLayerChild('右边场景1', '宝石项链').active = true;
                this.getLayerChild('chadianNodeList2', '最近有个大项目').active = true;
                this.showDuiHua(this.duihuaArrayConfig[5]);
            },
            最近有个大项目: () => {
                this.getLayerChild('chadianNodeList2', '将钱拖给男生').active = true;
                this.showDuiHua(this.duihuaArrayConfig[6]);
            },
            将钱拖给男生: () => {
                this.getLayerChild('chadianNodeList2', '家居全部消失').active = true;
                this.showDuiHua(this.duihuaArrayConfig[7]);
            },
            家居全部消失: () => {
                this.getLayerChild('右边场景1', '右边场景3').active = true;
                this.getLayerChild('右边场景1', '凳子2').active = true;
                this.getLayerChild('chadianNodeList2', '男生跟老三穿的破破烂烂的').active = true;
                this.showDuiHua(this.duihuaArrayConfig[8]);
            },
            男生跟老三穿的破破烂烂的: () => {
                for (const iterator of this.getLayerChild('右边场景1').children) {
                    if (['右边场景3', '凳子1', '穷老三', '男跪'].includes(iterator.name)) {
                        iterator.active = true;
                    } else {
                        iterator.active = false;
                    }
                }
                this.showDuiHua(this.duihuaArrayConfig[9], () => {
                    this.showDuiHua(this.duihuaArrayConfig[10], () => {
                        this.getLayerChild('右边场景1', '穷老三').active = false;
                        this.determine();
                    });
                });
            },
        }

        console.log("[GameReport]++++++++++++++++++++++>" + targetNode.checkNode.name);
        checkLogic[targetNode.checkNode.name]?.();
    }

    nodeCheckSuccessCallback3(targetNode: cc.Node) {
        let checkLogic = {
            带回来一个男朋友: () => {
                const callback_money = () => {
                    let index = 3;
                    const money = this.getLayerChild('money3').children;
                    this.schedule(() => {
                        let curIndex = index, startPosi = money[index].getPosition();
                        cc.tween(money[curIndex])
                            .set({ active: true, position: cc.v3(200, 700, 0) })
                            .to(0.5, { position: startPosi })
                            .call(() => {
                                if (curIndex >= money.length - 1) {
                                    this.showDuiHua(this.duihuaArrayConfig[14], () => {
                                        this.getLayerChild('chadianNodeList3', '女生穿上婚纱').active = true;
                                        this.getLayerChild('chadianNodeList3', '男生穿着西装').active = true;
                                        this.getLayerChild('chadianNodeList3', '变成婚堂').active = true;
                                    });
                                }
                            })
                            .start();
                        index++;
                    }, 0.2, 4, 0);
                };
                this.showDuiHua(this.duihuaArrayConfig[13], () => {
                    cc.tween(this.getLayerChild('左边场景1', '站起来女'))
                        .to(0.3, { x: -500 })
                        .call(() => {
                            cc.tween(this.getLayerChild('左边场景1', '男朋友正常装扮'))
                                .set({ active: true, x: -500 })
                                .to(0.3, { x: 52 })
                                .start();
                        })
                        .to(0.3, { x: -100 })
                        .call(callback_money)
                        .start();
                });
            },
            女生穿上婚纱: () => {
                this.getLayerChild('左边场景1', '站起来女').active = false;
                this.getLayerChild('左边场景1', '婚纱').active = true;
                this.showDuiHua(this.duihuaArrayConfig[15], () => {
                    this.determine();
                });
            },
            男生穿着西装: () => {
                this.getLayerChild('左边场景1', '男朋友正常装扮').active = false;
                this.getLayerChild('左边场景1', '男朋友西装').active = true;
                this.showDuiHua(this.duihuaArrayConfig[17], () => {
                    this.determine();
                });
            },
            变成婚堂: () => {
                this.getLayerChild('左边场景1', '左边场景2').active = true;
                this.showDuiHua(this.duihuaArrayConfig[16], () => {
                    this.determine();
                });
            },
        }

        console.log("[GameReport]++++++++++++++++++++++>" + targetNode.checkNode.name);
        checkLogic[targetNode.checkNode.name]?.();
    }

    //触摸回调
    normalTouchCallback(targetNode: cc.Node) {
        console.log("+++++++++++++++++++++>" + targetNode.name);

        const itemLogic = {
            离婚协议书: () => {
                this.showDuiHua(this.duihuaArrayConfig[0], () => {
                    let index = 0;
                    const money = this.getLayerChild('money').children;
                    const callback_animation = () => {
                        cc.tween(this.getLayerChild('扇巴掌'))
                            .set({ active: true, position: cc.v3(1000, 330, 0) })
                            .to(0.3, { position: cc.v3(375, 330, 0) })
                            .call(() => {
                                Common5.playEffectCustom(Common5.selectGameInfo.bundle, 'sound/打巴掌');
                                CccGame.playAnimation2(this.getLayerChild('扇巴掌'), '扇巴掌');
                                this.scheduleOnce(() => {
                                    this.getLayerChild('巴掌印').active = true;
                                    this.showDuiHua(this.duihuaArrayConfig[1], () => {
                                        this.nextLayer();
                                    });
                                }, 1);
                            })
                            .start();
                    };
                    this.schedule(() => {
                        let curIndex = index, startPosi = money[index].getPosition();
                        cc.tween(money[curIndex])
                            .set({ active: true, position: cc.v3(100, 800, 0) })
                            .to(0.5, { position: startPosi })
                            .call(() => {
                                if (curIndex >= money.length - 1) {
                                    callback_animation();
                                }
                            })
                            .start();
                        index++;
                    }, 0.2, money.length - 1, 0);
                });
            },
            男跪: () => {
                this.nextLayer();
            },
        }
        const targetNameList = ['离婚协议书', '男跪'];

        if (targetNameList.includes(targetNode.name)) {
            itemLogic[targetNode.name]?.();
        }
    }

    //判定
    determine() {
        this.curSchedule++;
        if (this.curSchedule >= 4) {
            this.getLayerChild('chadianNodeList4').active = true;
        }
    }

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

        if (curLog.effectUrl) {
            Common5.playEffectCustom(Common5.selectGameInfo.bundle, 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_
            cc.tween(qiPao)
                .set({ active: true, scale: 0 })
                .to(0.2, { scale: 1 })
                .delay(delayTime_)
                .set({ 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_);
    }

    //获取当前场景下的节点
    getLayerChild(...path: string[]) {
        let child = this.layerNode[this.curLayerIndex];
        if (path.length > 1) {
            for (const name of path) {
                child = child.getChildByName(name);
            }
            return child;
        }
        return child.getChildByName(path[0]);
    }

    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("inputBlock").active = true;
        Game.ins.stopTime();
        if (touchIndex == 0) {
            Game.ins.showFail();
        } else {
            Game.ins.showSuccess();
        }
    }

    //解锁提示
    unlockLevel(sublevel) {
        Game.ins.tipUnlock(sublevel)
    }

    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;
    }

    onClick(event) {
        let target = event.target as cc.Node;
        console.log("+++++++++++++++++++++>" + target.name);
        Common5.playEffect("点击音效");
        switch (target.name) {
            case "BtnAddTimes":
            default:
                break;
        }
    }

    //update(dt) { }
}