import { ITableRow } from "../../Engine/CatanEngine/TableV3/Core/ITableRow";
import { TableBase } from "../../Engine/CatanEngine/TableV3/Core/TableBase";
import CSAudio from "../../Engine/Utils/Audio/CSAudio";
import CSSettingsSDV3 from "../../FormTable/CSSettingsV3";

export class ClipsHandleBySoundTable {
	private _soundTable: TableBase<SoundTableRow> = null;
	public get SoundTable(): TableBase<SoundTableRow> { return this._soundTable; }
	private _useCloumStr: string = "PathZnTw";
	public set UseCloumStr(value: string) { this._useCloumStr = value; }
	public get UseCloumStr() { return this._useCloumStr; }

	constructor(id: number) {
		this._soundTable = CSSettingsSDV3.Sound;
	}

	public HandlerClips(clips: cc.AudioClip[]): void {
		let nameToClip: Map<string, cc.AudioClip> = new Map<string, cc.AudioClip>();
		for (let clip of clips) {
			if (clip != null) {
				nameToClip.set(clip.name, clip);
			}
		}
		// 音樂對應表id
		let idToClip: Map<number, cc.AudioClip> = new Map<number, cc.AudioClip>();
		let idToPath: Map<number, string> = new Map<number, string>();
		for (let row of this._soundTable.Rows) {
			let id: number = row.Id;
			let path: string = row[this._useCloumStr];
			if (path == null) {
				continue;
			}
			let fileName: string = cc.path.basename(path);
			if (nameToClip.has(fileName)) {
				if (!idToClip.has(id)) {
					idToClip.set(id, nameToClip.get(fileName));
					idToPath.set(id, path);
				}
			}
		}
		CSAudio.Instance.AddClipsInfo(idToClip, idToPath);
	}

}

export class SoundTableRow implements ITableRow {
	/** 編號 */
	Id: number;
	/** 音效=1, 音樂 = 2 */
	Type: number;
	/** 繁體中文音檔位置(預設音檔位置) */
	PathZnTw: string;
	PathEn: string;
}