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 WordGameBaseComponent from "../../../../FrameWork/Base/WordGameBaseComptent";
import DaoJiShiRoll from "../../../WordGame/gameComScript/DaoJiShiRoll";
import DuiHuaScript from "../../../WordGame/guizeGame/comScript/DuiHuaScript";
import DaDianScript from "../../../../FrameWork/Base/DaDianScript";
import Game from "../../../../Scripts/WenZiRes/ui/Game";
import DaoJiShiGameConfig from "./DaoJiShiGameConfig";

const { ccclass, property } = cc._decorator;

@ccclass
export default class DaoJiShiGameScript extends WordGameBaseComponent {

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

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

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

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

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

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

    //当前进度
    curSchedule: Map<string, true> = null;
    isGuanDeng: boolean = false;

    //文本配置
    duihuaArrayConfig: any[] = [];
    resultDuihuaArrayConfig: any[] = [];

    //计时
    Timer = 0;
    bundle = null
    start() {
        this.bundle = Common5.gameConfig.zmGameConfig[Common5.selectGameNum].bundle
        super.start();
        DaDianScript.userEnterDaDian();
        this.openTouchEvent(this.node.getChildByName('bg'));
        this.initParameters();
        this.initComponent();
        Common5.playMusic('倒计时游戏背景音');
    }


    //初始化参数
    initParameters() {
        this.curSchedule = new Map();
        this.isGuanDeng = false;
        this.duihuaArrayConfig = DaoJiShiGameConfig.getInstance().getGameConfig('duihuaArray') as any[];
        this.resultDuihuaArrayConfig = DaoJiShiGameConfig.getInstance().getGameConfig('resultDuihuaArray') as any[];

        Common.Type = 0
        Common.subLevel = 0;
        Common.GameSubTipConfigs = DaoJiShiGameConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = DaoJiShiGameConfig.getInstance().getGameConfig('answersArray');

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

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

    //初始化组件
    initComponent() {
        // let lab = this.node.getChildByName("标题").getChildByName("lab");
        // lab.getComponent(cc.Label).string = DaoJiShiGameConfig.getInstance().getGameConfig('titleArray');

        this.btn_start.getComponent(cc.Button).enabled = false;
        this.btn_stop.getComponent(cc.Button).enabled = false;

        let scr: DaoJiShiRoll = this.daoJiShiRoll.getComponent("DaoJiShiRoll");
        scr.setIsStartTimer(false, { callFunc: this.showEndGameResultUI, target: this });

        let sishen = this.node.getChildByName("sishen"),
            labelBar = sishen.getChildByName("labelBar"),
            text = labelBar.getChildByName("text").getComponent(cc.Label);
        text.string = DaoJiShiGameConfig.getInstance().getGameConfig('npcTips')[0];
        cc.tween(sishen)
            .set({ active: true, opacity: 0 })
            .to(2, { opacity: 255 })
            .call(() => {
                labelBar.active = true;
                Common5.playEffectCustom(this.bundle, "sound/你的生命只剩15秒的时间");
            })
            .start();
    }

    //移动触摸回调
    touchMoveCallback(targetNode) {
        console.log("++++++++++++++++:" + targetNode.name);
        this.curSchedule.set(targetNode.name, true);

        if (targetNode.name == "guandeng") {
            this.isGuanDeng = true;
            return;
        }

        if (!this.isGuanDeng) {
            this.mask.active = true;
            let scr: DaoJiShiRoll = this.daoJiShiRoll.getComponent("DaoJiShiRoll");
            scr.setIsStartTimer(false);
            this.btn_start.active = false;
            this.btn_stop.active = false;
            this.scheduleOnce(() => {
                this.failShow(0);
            }, 0.5);
            return;
        }

        switch (targetNode.name) {
            case "jingzi":
                this.showDuihua(0);
                break;
            case "hua1":
                this.showDuihua(1);
                break;
            case "shuitong":
                this.showDuihua(2);
                break;
            case "fenmowan":
                this.showDuihua(3);
                break;
            default:
                break;
        }
    }

    //显示对话
    showDuihua(stepIndex: number, func?: Function) {
        this.DuiHuaPrefab.active = true;
        let script_: DuiHuaScript = this.DuiHuaPrefab.getComponent('DuiHuaScript');
        let duihua = this.duihuaArrayConfig[stepIndex];
        script_.setDuihuaArray(this.bundle,duihua, func);
    }

    //显示结果对话
    showDuihuaResult(duihuaIndex, func?: Function) {
        this.DuiHuaPrefab.active = true;
        let script_: DuiHuaScript = this.DuiHuaPrefab.getComponent('DuiHuaScript');
        let duihuaArray = this.resultDuihuaArrayConfig[duihuaIndex];
        script_.setDuihuaArray(this.bundle,duihuaArray, func);
    }

    //失败展示
    failShow(curResultIndex, isshowDuihua = true, callFunc?: Function) {
        Common5.playEffect("人物跳脸");
        let node = this.resultLayer[curResultIndex]
        let renwuNode = node.getChildByName('renwuNode')
        renwuNode.scale = 0.01 * renwuNode.scale;
        let zhuahengBg = node.getChildByName('zhuahengBg')
        node.active = true
        cc.tween(renwuNode)
            .to(0.3, { scale: renwuNode.scale * 100 }, { 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(curResultIndex);
                }
                if (callFunc) {
                    callFunc()
                }
                this.scheduleOnce(() => {
                    Game.ins.showFail();
                }, 2)
            })
            .start();
    }

    //结果展示
    showEndGameResultUI() {
        this.btn_start.active = false;
        this.btn_stop.active = false;
        this.mask.active = false;

        this.node.getChildByName("Layer").getChildByName("zhanli").active = false;
        this.node.getChildByName("Layer").getChildByName("daodi").active = true;

        let index = -1,
            size = this.curSchedule.size,
            jiaoyin = this.node.getChildByName("Layer").getChildByName("jiaoyin");
        let animationEvent = () => {
            index++;
            if (index < jiaoyin.children.length) {
                cc.tween(jiaoyin.children[index])
                    .set({ active: true })
                    .delay(0.5)
                    .call(animationEvent)
                    .start();
            }
            else {
                let result = size >= 6 ? "result1" : "result2";
                this.endGameView(result);
            }
        }
        animationEvent();
    }

    endGameView(result) {
        let endLayer = this.node.getChildByName('endLayer')
        endLayer.active = true;

        let resultlayer = endLayer.getChildByName(result)
        resultlayer.active = true
        if (result == 'result1') {
            Common5.playEffectCustom(this.bundle, "sound/凶手被逮捕");
            this.scheduleOnce(() => {
                Game.ins.showSuccess();
            }, 7.0);
        } else {
            Common5.playEffectCustom(this.bundle, "sound/仍在追查");
            this.scheduleOnce(() => {
                Game.ins.showFail();
            }, 5.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;
    }

    onTimeBtn(event) {
        Common5.playEffect("点击音效");
        let target = event.target;
        if (target.name == "start") {
            this.mask.active = false;
            let scr: DaoJiShiRoll = this.daoJiShiRoll.getComponent("DaoJiShiRoll");
            scr.setIsStartTimer(true);
            this.btn_start.active = false;
            this.btn_stop.active = true;
        } else if (target.name == "stop") {
            this.mask.active = true;
            let scr: DaoJiShiRoll = this.daoJiShiRoll.getComponent("DaoJiShiRoll");
            scr.setIsStartTimer(false);
            this.btn_start.active = true;
            this.btn_stop.active = false;
        } else if (target.name == "labelBar") {
            target.parent.active = false;
            this.btn_start.getComponent(cc.Button).enabled = true;
            this.btn_stop.getComponent(cc.Button).enabled = true;
        }
    }

    //update(dt) { }
}