import { LanguageManager } from "@/FormTableExt/Manage/Language/LanguageManager";
import { ITableRow } from "../../Engine/CatanEngine/TableV3/Core/ITableRow";
import { TableBase } from "../../Engine/CatanEngine/TableV3/Core/TableBase";
import CSSettingsSDV3 from "../CSSettingsSDV3";

declare module "../CSSettingsSDV3" {
	export default interface CSSettingsV3 {
		/**
		 * 共用String表欄位值
		 * @param formId
		 */
		CommonString(formId: number): string;
		/**
		 * 取得機台CSSettingsV3的COMMON表名
		 * @param slotID
		 */
		CheckSlotFormNameStr(slotID: number): string;
		/**
		 * 各機台String表欄位值
		 * @param slotID
		 * @param formId
		 */
		SlotString(slotID: number, formId: number): string;
		/**
		 * 各機台FIXED表的前後端共用欄位值
		 * @param slotID
		 * @param formId
		 */
		SlotFixedBvalue(slotID: number, formId: number): number;
		/**
		 * 各機台FIXED表的前端欄位值
		 * @param slotID
		 * @param formId
		 */
		SlotFixedCvalue(slotID: number, formId: number): number;
		/**
		 * 各機台WheelClient表的SymbolList欄位值
		 * @param slotID
		 * @param formId
		 */
		SlotWheelClientSymbolList(slotID: number, formId: number): JSON;
		/**
		 * 取得圖標表
		 * @param slotID
		 */
		SlotSymbolTable(slotID: number): TableBase<ITableRow>;
		/**
		 * 取得連續圖
		 * @param slotID
		 */
		SlotSymbolContinuousPic(slotID: number, formId: number): JSON;
		/**
		 * PLAYTABL數值設定
		 * @param slotID
		 * @param formId
		 */
		SlotPaytableBvalue(slotID: number, formId: number): number;
		/**
		 * 判斷表存不存在
		 * @param jsonFileName
		 * @param sheetName
		 */
		CheckTableExist(settingName: string, classSheetName: string): any;
		CheckTableIdExist(table: any, id: number): any;
		CheckGetValue(con: any, keyStr: string): any;
	}
}

CSSettingsSDV3.prototype.CommonString = function (formId: number): string {
	let table = CSSettingsSDV3.String;
	let con = CSSettingsSDV3.prototype.CheckTableIdExist(table, formId);
	let value: string = CSSettingsSDV3.prototype.CheckGetValue(con, LanguageManager.GetMsgId());
	if (value) {
		value = value.replace("\\n", "\n");
		return value;
	}
	return "";
};

CSSettingsSDV3.prototype.CheckSlotFormNameStr = function (slotID: number): string {
	let con = CSSettingsSDV3.Slotset[slotID];
	if (con) {
		let str = con.NameSetting;
		return str;
	}
	cc.warn("Slotset沒有這個ID:" + slotID);
	return null;
};

CSSettingsSDV3.prototype.CheckTableExist = function (settingName: string, classSheetName: string): any {
	let table = CSSettingsSDV3[settingName];
	if (table) {
		if (classSheetName !== "") {
			table = table[classSheetName];
		}
	}
	if (table) {
		return table;
	}
	cc.warn("未知的表:" + settingName + "," + classSheetName);
	return null;
};

CSSettingsSDV3.prototype.CheckTableIdExist = function (table: any, id: number): any {
	if (table) {
		let con = table[id];
		if (con != null && con !== undefined) {
			return con;
		}
		cc.warn(table["__proto__"]["constructor"]["name"] + "未知的ID:" + id);
	}
	return null;
};

CSSettingsSDV3.prototype.CheckGetValue = function (con: any, keyStr: string): any {
	if (con) {
		let con2 = con[keyStr];
		if (con2 != null && con2 !== undefined) {
			return con2;
		}
		cc.warn(con["__proto__"]["constructor"]["name"] + "取不到ID" + con["Id"] + "欄位的KEY值:" + keyStr);
	}
	return null;
};

CSSettingsSDV3.prototype.SlotString = function (slotID: number, formId: number): string {
	let formName = CSSettingsSDV3.prototype.CheckSlotFormNameStr(slotID);
	let table = CSSettingsSDV3.prototype.CheckTableExist(formName, "String");
	let con = CSSettingsSDV3.prototype.CheckTableIdExist(table, formId);
	let value: string = CSSettingsSDV3.prototype.CheckGetValue(con, LanguageManager.GetMsgId());
	if (value) {
		value = value.replace("\\n", "\n");
		return value;
	}
	return "";
};

CSSettingsSDV3.prototype.SlotFixedBvalue = function (slotID: number, formId: number): number {
	let formName = CSSettingsSDV3.prototype.CheckSlotFormNameStr(slotID);
	let table = CSSettingsSDV3.prototype.CheckTableExist(formName, "Fixed");
	let con = CSSettingsSDV3.prototype.CheckTableIdExist(table, formId);
	let value: string = CSSettingsSDV3.prototype.CheckGetValue(con, "BValue");
	if (value) {
		let num: number = Number.parseFloat(value);
		return num;
	}
	return 0;
};
CSSettingsSDV3.prototype.SlotFixedCvalue = function (slotID: number, formId: number): number {
	let formName = CSSettingsSDV3.prototype.CheckSlotFormNameStr(slotID);
	let table = CSSettingsSDV3.prototype.CheckTableExist(formName, "Fixed");
	let con = CSSettingsSDV3.prototype.CheckTableIdExist(table, formId);
	let value: string = CSSettingsSDV3.prototype.CheckGetValue(con, "CValue");
	if (value) {
		let num: number = Number.parseFloat(value);
		return num;
	}
	return 0;
};

CSSettingsSDV3.prototype.SlotWheelClientSymbolList = function (slotID: number, formId: number): JSON {
	let formName = CSSettingsSDV3.prototype.CheckSlotFormNameStr(slotID);
	let table = CSSettingsSDV3.prototype.CheckTableExist(formName, "WheelClient");
	let con = CSSettingsSDV3.prototype.CheckTableIdExist(table, formId);
	let value: JSON = CSSettingsSDV3.prototype.CheckGetValue(con, "SymbolList");
	if (value) {
		return value;
	}
	return null;
};

CSSettingsSDV3.prototype.SlotSymbolTable = function (slotID: number): TableBase<ITableRow> {
	let formName = CSSettingsSDV3.prototype.CheckSlotFormNameStr(slotID);
	let table = CSSettingsSDV3.prototype.CheckTableExist(formName, "Symbol");
	if (table) {
		return table;
	}
	return null;
};

CSSettingsSDV3.prototype.SlotSymbolContinuousPic = function (slotID: number, formId: number): JSON {
	let formName = CSSettingsSDV3.prototype.CheckSlotFormNameStr(slotID);
	let table = CSSettingsSDV3.prototype.CheckTableExist(formName, "Symbol");
	let con = CSSettingsSDV3.prototype.CheckTableIdExist(table, formId);
	let value: JSON = CSSettingsSDV3.prototype.CheckGetValue(con, "ContinuousPic");
	if (value) {
		return value;
	}
	return null;
};

CSSettingsSDV3.prototype.SlotPaytableBvalue = function (slotID: number, formId: number): number {
	let formName = CSSettingsSDV3.prototype.CheckSlotFormNameStr(slotID);
	let table = CSSettingsSDV3.prototype.CheckTableExist(formName, "Paytable");
	let con = CSSettingsSDV3.prototype.CheckTableIdExist(table, formId);
	let value: string = CSSettingsSDV3.prototype.CheckGetValue(con, "PayNormal");
	if (value) {
		let num: number = Number.parseFloat(value);
		return num;
	}
	return 0;
};