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 GameReport from "../../../../FrameWork/Report/ZyZyReport";
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 logPrefabScript from "../../../WordGame/gameComScript/logPrefabScript";
// import YiLiaoFeiYongConfig from "./YiLiaoFeiYongConfig";

const { ccclass, property } = cc._decorator;

@ccclass
export default class YiLiaoFeiYongScript extends WordGameBaseComponent {

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

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

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

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

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

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

    //当前层级下标
    curLayerIndex = 0;

    //当前进度
    curSchedule: number = 0;
    curCost: number = 0;

    //文本配置
    titleArrayConfig: string[] = [];
    duihuaArrayConfig: any[] = [];
    chadianArrayConfig: any[] = [];
    isshowSuccess: boolean = false;

    start() {
        super.start();
        DaDianScript.userEnterDaDian();
        this.openTouchEvent(this.node.getChildByName('bg'));
        // this.initParameters();
        // this.initComponent();
        
        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle,'script/YiLiaoFeiYongConfig',(assest)=>{
            //this.jsonData = JSON.stringify(assest.json)

            this.jsonData =assest.json

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

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

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

        this.titleArrayConfig = this.jsonData.titleArray//YiLiaoFeiYongConfig.getInstance().getGameConfig('titleArray') as string[];
        this.chadianArrayConfig = this.jsonData.chadianArray//YiLiaoFeiYongConfig.getInstance().getGameConfig('chadianArray') as any[];
        this.duihuaArrayConfig = this.jsonData.duihuaArray//YiLiaoFeiYongConfig.getInstance().getGameConfig('duihuaArray') as any[];
        Common.Type = 0
        Common.subLevel = 0;
        Common.GameSubTipConfigs = this.jsonData.tipsArray//YiLiaoFeiYongConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = this.jsonData.answersArray//YiLiaoFeiYongConfig.getInstance().getGameConfig('answersArray');

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

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

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


    //初始化组件
    initComponent() {
        let lab = this.node.getChildByName("标题").getChildByName("lab");
        lab.getComponent(cc.Label).string = this.titleArrayConfig[0];
        this.valueTextNode.getComponent(cc.Label).string = "价格:" + this.curCost;

        let title = this.titleArrayConfig[0];
        Game.ins.setGameTitle(title)
        for (const iterator of this.props.children) {
            let callbacks = CccGame.onNodeTouchMoreCheckEvent(iterator, this.checkNodeList.children);
            callbacks.setSuccessListener((data_) => {
                this.nodeCheckSuccessCallback(data_.targetNode);
            });
        }

        this.node.getChildByName("mask").active = true;
        this.showDuiHua(this.duihuaArrayConfig[0], () => {
            this.node.getChildByName("mask").active = false;
        });

   
    }


    nodeCheckSuccessCallback(targetNode: any) {
  
   
        console.log("[GameReport]++++++++++++++++++++++>" + targetNode.checkNode.name);
        GameReport.BtnsReport(targetNode.checkNode.name, Common5.selectGameInfo.titleUrl);

        let index = this.checkNodeList.children.indexOf(targetNode.checkNode);
        this.hideNodeList[index].active = false;
        this.showNodeList[index].active = true;

        let log = this.chadianArrayConfig[index].log;
        if (log && log[0]) {
            // this.node.getChildByName("mask").active = true;
            this.showDuiHua(log[0], () => {
                if (log[1]) {
                    this.showDuiHua(log[1], () => {
                        this.node.getChildByName("mask").active = false;
                        this.determine();
                    });
                } else {
                    this.node.getChildByName("mask").active = false;
                    this.determine();
                }
            });
        } else {
            this.determine();
        }

        // let point = targetNode.checkNode.convertToWorldSpace(cc.Vec2.ZERO);
        // let pos = this.node.getChildByName("layerNode").convertToNodeSpaceAR(point);
        let sPos = targetNode.checkNode.getPosition();
        let valueNode = this.node.getChildByName("价格框");
        let epos = valueNode.getPosition() as any;
        cc.tween(this.money)
            .set({ position: sPos, active: true })
            .to(0.5, { position: epos })
            .set({ active: false })
            .call(() => {
                let cost = valueNode.getChildByName("cost");
                let pos = this.valueTextNode.getPosition() as any;
                cost.getComponent(cc.Label).string = "+" + this.chadianArrayConfig[index].cost;
                cc.tween(cost)
                    .set({ position: pos, active: true })
                    .to(0.5, { y: pos.y + 50 })
                    .set({ active: false })
                    .call(() => {
                        this.curCost += this.chadianArrayConfig[index].cost;
                        this.valueTextNode.getComponent(cc.Label).string = "价格:" + this.curCost;
                    })
                    .start();
            })
            .start();
    }

    normalTouchCheckCallback(targetNode: any) {
        console.log("targetNodeName+++++++++++++++++++++>" + targetNode.name);
        switch (targetNode.name) {
            case '窗户':
                break;
            default:
                break;
        }
    }

    determine() {
        this.curSchedule++;
        if (this.curSchedule == this.chadianArrayConfig.length) {
            Game.ins.stopTime();
            this.node.getChildByName("mask").active = true;
            this.showDuiHua(this.duihuaArrayConfig[1], () => {
                this.endGameView(1);
            });
        }
    }

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

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

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

        let node = this.node.getChildByName('layerNode');
        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_);
    }

    endGameView(touchIndex) {
        this.node.getChildByName("mask").active = true;
        Game.ins.stopTime();
        if (touchIndex == 0) {
            this.scheduleOnce(() => {
                Game.ins.showFail();
            }, 3);
        } else {
            if(this.isshowSuccess){
                return
            }
            this.isshowSuccess = true
            
            this.scheduleOnce(() => {
                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;
    }

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