import { readdir } from 'fs';
import * as path from 'path'
import { Undecked } from './Core';
import { Log } from './Logger';

declare var Undecked: Undecked;

export class Icons {
	materialPath: string;

	icons: { id: string; name: string }[];

	iconIDs: string[];

	constructor() {
		this.materialPath = path.join(__filename, '..', '..', '..', 'Static', 'materialicons');

		this.icons = [];
		this.iconIDs = [];
	}

	load(callback: () => void) {
		Log('info', 'Loading icons');
		readdir(path.join(this.materialPath, 'black'), (err, files) => {
			if (err) throw err;

			for (let i = 0; i < files.length; i++) {
				if (files[i].includes('.png') && !files[i].includes('_low')) {
					var fileID = files[i].replace('.png', '');
					this.iconIDs.push(fileID);
					this.icons.push({ id: fileID, name: fileID.replace(/_/g, ' ') });
				}
			}

			Log('info', `Loaded ${files.length} icon(s)`);
			callback();
		});
	}

	getList(): { id: string; name: string }[] {
		return this.icons;
	}

	getPath(iconID: string, style: 'black' | 'white') {
		if (this.iconIDs.includes(iconID)) return path.join(this.materialPath, style, `${iconID}.png`);
		console.log(`Invalid icon '${iconID}'`);
		return null;
	}
}

// export interface Icons {
// 	load: (callback: () => void) => void;

// 	getList: () => { id: string; name: string }[];
// 	getPath: (iconID: string, style: 'black' | 'white') => string;
// }
