import Gamecfg from "../../../../common/gameCfg";
import { gameMethod } from "../../../../common/gameMethod";
import { xlsTaskMain } from "../../../../common/xlsConfig";
import { TaskMainEvent, UserEvent } from "../../../../data/const/EventConst";
import { TaskKind } from "../../../../data/const/TypeConst";
import GameDataCenter from "../../../../data/GameDataCenter";
import ResSpine from "../../../../frameWork/compment/ResSpine";
import EventMng from "../../../../manager/EventMng";
import ClickAuEffect from "../../../../utils/ClickAuEffect";
import GameMath from "../../../../utils/GameMath";
import { I18n } from "../../../../utils/I18nUtil";
import ItemIcon from "../../Common/ItemIcon";

export default class HomeTaskCom {
    taskNode: fairygui.GButton;
    isselect: fairygui.Controller;
    spine: fairygui.GComponent;
    itemIcon: ItemIcon;

    conf: xlsTaskMain;

    effect: ResSpine;
    TaskCountLabel: fairygui.GTextField;
    CountLabel: fairygui.GTextField;
    // Red: fairygui.Controller;

    constructor(node: fairygui.GButton) {
        this.taskNode = node;
        this.itemIcon = this.taskNode.getChild("ItemIcon") as ItemIcon;
        this.TaskCountLabel = this.taskNode.getChild("TaskCountLabel") as fairygui.GTextField;
        this.CountLabel = this.taskNode.getChild("CountLabel") as fairygui.GTextField;
        this.isselect = this.taskNode.getController("isselect");
        this.spine = this.taskNode.getChild("spine") as fairygui.GComponent;
        this.spine.visible = false;
        this.effect = this.spine.node.addComponent(ResSpine);
        // this.effect.setSpineData("spine", "ui/ui_effect_task_renwuwancheng/ui_effect_task_renwuwancheng", null, false, "stand", true);
    }

    onEnable() {
        this.taskNode.onClick(this.onClickTaskMain, this);
        EventMng.on(TaskMainEvent.UP_INFO, this.upTaskMain, this);
        GameDataCenter.task.sendTaskInfo();
    }

    onDisable() {
        this.taskNode.offClick(this.onClickTaskMain, this);
        EventMng.off(TaskMainEvent.UP_INFO, this.upTaskMain, this);
    }

    setData(conf: xlsTaskMain) {
        this.conf = conf;
        this.upTaskMain();
    }

    touchBlock: boolean = false;
    // 快捷任务按钮事件
    @ClickAuEffect()
    onClickTaskMain() {
        let actTaskMain = GameDataCenter.task.taskInfo;
        let conf = Gamecfg.taskMain.getItem(actTaskMain.id.toString());
        if (!conf) {
            if (CC_PREVIEW) console.log("任务不存在,id: ", actTaskMain?.id);
            return;
        }
        if (actTaskMain.cons >= conf.need) {
            if (this.touchBlock) return;
            this.touchBlock = true;
            GameDataCenter.task.sendTaskRwd(() => {
                this.touchBlock = false;
            });
        } else {
            if (this.touchBlock) return;
            // GameDataCenter.task.TaskJump(conf.kind, true);
        }
    }

    playEffect() {
        this.spine.visible = true;
        this.effect.setAnimation("stand");
    }

    upTaskMain() {
        let actTaskMain = GameDataCenter.task.taskInfo;
        //最后一个任务
        let lastTask = GameDataCenter.task.checkIsMax();
        let conf = Gamecfg.taskMain.getItem(actTaskMain?.id.toString());
        let ismax = lastTask ? true : false;
        if (lastTask == null && conf == null) {
            if (CC_PREVIEW) console.warn("当前任务找不到任何配置");
            this.taskNode.visible = false;
            return;
        } else {
            this.taskNode.visible = true;
        }
        conf = ismax ? lastTask : conf;
        let isfinish = GameDataCenter.task.curTaskIsFinish() || ismax;
        let taskKind: string = conf.kind;
        this.taskNode.title = GameDataCenter.task.getTaskDesc(taskKind, conf.need);
        let str = I18n.getI18nText("hometaskitem_count", GameDataCenter.task.checkMainTaskCount(conf, ismax));
        if (actTaskMain.cons >= conf?.need) str = "[color=#6fdf89]" + str + "[/color]";
        this.TaskCountLabel.text = str;
        //不需要播放特效了
        if (!lastTask && isfinish) {
            if (this.spine.visible == false) this.playEffect();
        } else {
            this.effect.clearTracks();
            this.spine.visible = false;
        }
        // this.Item.showItem(conf.item, param);
        this.itemIcon.setData(conf.item);
        this.isselect.selectedIndex = lastTask ? 1 : 0;
        this.CountLabel.setVar("num", GameMath.showNum(conf.item[2])).flushVars();
    }
}
