import Gamecfg from "../../../common/gameCfg";
import { gameMethod } from "../../../common/gameMethod";
import { xlsTaskMain } from "../../../common/xlsConfig";
import EventMng from "../../../manager/EventMng";
import { MsgItemChange_s } from "../../../shared/item/server/MsgItemChange_s";
import { ResTaskInfo } from "../../../shared/task/PtlTaskInfo";
import { I18n } from "../../../utils/I18nUtil";
import { TaskMainEvent } from "../../const/EventConst";
import { TaskKind } from "../../const/TypeConst";
import GameDataCenter from "../../GameDataCenter";
import { TaskModelData } from "./TaskModelData";

export default class TaskModel extends TaskModelData {
	readonly GuanQiaNum = 5;
	readonly TaskPopCloseCd: number = 5;
	readonly TaskPopTime: number = 0.2;

	lastPopTaskId: number = null;
	//引导手指的id
	curHandTaskId = 0;
	//最后一次记录任务id
	private _lastTaskId: number = 0;

	constructor() {
		super("task");
	}

	onRegister(): void {
        GameDataCenter.gameServer.ListenMsg("item/server/ItemChange_s", this.onTaskChange, this);
    }

	onTaskChange(msg: MsgItemChange_s) {
        for (let key in msg) {
            if (Object.prototype.hasOwnProperty.call(msg, key)) {
                this.taskInfo[key] = msg[key];
            }
        }
        EventMng.emit(TaskMainEvent.UP_INFO);
    }
	
	setTaskInfo(res: ResTaskInfo) {
		this.taskInfo = res;
		this._lastTaskId = res.id;
	}

	checkMainTaskCount(conf: xlsTaskMain, ismax: boolean = false) {
		let actTaskMain = GameDataCenter.task.taskInfo;
		let max = this.checkMax(conf.kind, conf.need);
		let num = this.checkNum(conf.kind, actTaskMain?.cons, conf.need);
		let cons = ismax ? max : Math.min(num, max);
		return `${cons}/${max}`;
	}

	/**
	 * 任务当前进度值
	 * @param kind 任务类型
	 * @param val 当前进度值
	 * @param need 任务需求值
	 * @returns
	 */
	checkNum(kind: string, val: number, need: number) {
		let num = 0;
		switch (kind) {
			default:
				num = val;
				break;
		}
		return num;
	}

	/**
	 * 任务进度的最大值
	 * @param kind 任务类型
	 * @param need 任务需求值
	 * @returns
	 */
	checkMax(kind: string, need: number) {
		let max = 1;
		switch (kind) {
			default:
				max = need;
				break;
		}
		return max;
	}

	/**
	 * 获得任务类型的任务进度
	 * @param kind 任务类型
	 * @param val 当前进度值
	 * @param need 任务需求值
	 * @returns  x/y
	 */
	getTaskCount(kind: string, val: number, need: number) {
		let max = this.checkMax(kind, need);
		let num = this.checkNum(kind, val, need);
		let cons = Math.min(num, max);
		return `${cons}/${max}`;
	}

	/**
	 * 获得任务类型对应的任务描述
	 * @param kind 任务类型
	 * @param need 任务需求值
	 * @returns 任务描述
	 */
	getTaskDesc(kind: string | number, need: number) {
		switch (kind) {
			default:
				return I18n.getTaskDesc(kind, need);
		}
	}

	//当前任务是否完成
	public curTaskIsFinish() {
		let actTaskMain = GameDataCenter.task.taskInfo;
		let conf = Gamecfg.taskMain.getItem(actTaskMain?.id.toString());
		if (actTaskMain.cons >= conf?.need) {
			return true;
		} else {
			return false;
		}
	}
	//指定任务是否完成
	public TaskIdIsFinish(id: string) {
		let actTaskMain = GameDataCenter.task.taskInfo;
		if (actTaskMain?.id.toString() != id) {
			return false;
		}
		let conf = Gamecfg.taskMain.getItem(actTaskMain?.id.toString());
		if (!gameMethod.isEmpty(conf) && actTaskMain?.cons >= conf.need) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测当前是否是最后一个任务
	 * 如果是，则返回最后一个任务
	 * @returns
	 */
	checkIsMax(): xlsTaskMain {
		let id = GameDataCenter.task.taskInfo?.id ?? 0;
		let conf = Gamecfg.taskMain.getItem(id + "");
		if (conf) return null;
		let last = Gamecfg.taskMainByNextid.getItem(id + "");
		return last;
	}

	//发送消息
	async sendTaskInfo() {
        let ret = await GameDataCenter.gameServer.ReqApi("task/TaskInfo", {});
        if (ret?.res) {
            this.setTaskInfo(ret.res);
            EventMng.emit(TaskMainEvent.UP_INFO);
        }
    }

	async sendTaskRwd(callback?: Function) {
        let ret = await GameDataCenter.gameServer.ReqApi("task/TaskRwd", {});
        if (ret?.res) {
            this.setTaskInfo(ret.res);
			callback && callback();
            EventMng.emit(TaskMainEvent.UP_INFO);
        }
    }
}
