import {Util} from "./Util.js";
import {UtilGameSettings} from "./UtilGameSettings.js";

class UtilCompat {
	static init () {
		if (!game.user.isGM) return;

		const {system, isUnknownVersion, major, minor, patch, version} = Util.Versions.getSystemVersion();

		// Ignore "unknown version" systems, as we assume the user knows what they're doing
		//   (e.g. is running a test version).
		if (isUnknownVersion) return;

		const supportedSystemMeta = this._SYSTEMS_SUPPORTED[system];
		if (!supportedSystemMeta) return; // As above; ignore, and assume user competence :^)

		const isBelowMin = supportedSystemMeta.min
			? major < supportedSystemMeta.min.major
			|| (major === supportedSystemMeta.min.major && minor < supportedSystemMeta.min.minor)
			|| (major === supportedSystemMeta.min.major && minor === supportedSystemMeta.min.minor && patch < supportedSystemMeta.min.patch)
			: false;
		const isAboveMax = supportedSystemMeta.max
			? major > supportedSystemMeta.max.major
			|| (major === supportedSystemMeta.min.major && minor > supportedSystemMeta.min.minor)
			|| (major === supportedSystemMeta.min.major && minor === supportedSystemMeta.min.minor && patch > supportedSystemMeta.min.patch)
			: false;
		if (!isBelowMin && !isAboveMax) return;

		ui.notifications.warn(`Game system "${system}" version ${version} is ${[isBelowMin ? `below the minimum required version (${this._getVersionString(supportedSystemMeta.min)})` : null, isAboveMax ? `above the maximum supported version (${this._getVersionString(supportedSystemMeta.max)})` : null].filter(Boolean).join(" and ")}. Please update your system version.`)
	}

	static _getVersionString (versionDict) { return `${versionDict.major}.${versionDict.minor}.${versionDict.patch}`; }

	static isModuleActive (moduleId) { return !!game.modules.get(moduleId)?.active; }

	static isDaeActive () { return this.isModuleActive(UtilCompat.MODULE_DAE); }
	static isDragUploadActive () { return this.isModuleActive(UtilCompat._MODULE_DRAG_UPLOAD); }
	static isBetterRollsActive () { return this.isModuleActive(UtilCompat._MODULE_BETTER_ROLLS); }

	static isDaeGeneratingArmorEffects () {
		if (!this.isDaeActive()) return false;
		return !!UtilGameSettings.getSafe(UtilCompat.MODULE_DAE, "calculateArmor");
	}

	static getFeatureFlags ({isReaction}) {
		const out = {};

		if (isReaction) {
			// Support "Better NPC Sheets 5e", which is apparently incapable of determining a reaction using the standard
			//   "Action type: reaction" data :^)
			out.adnd5e = {itemInfo: {type: "reaction"}}
		}

		return out;
	}
}
UtilCompat.MODULE_DAE = "dae";
UtilCompat._MODULE_DRAG_UPLOAD = "dragupload";
UtilCompat._MODULE_BETTER_ROLLS = "betterrolls5e";
UtilCompat.MODULE_MIDI_QOL = "midi-qol";
UtilCompat.MODULE_KANKA_FOUNDRY = "kanka-foundry";

UtilCompat._SYSTEMS_SUPPORTED = {
	"dnd5e": {
		min: {
			major: 1,
			minor: 2,
			patch: 4,
		},
	},
}

export {UtilCompat};
