import {UtilApplications} from "./UtilApplications.js";
import {SharedConsts} from "../shared/SharedConsts.js";
import {Config} from "./Config.js";
import {DataConverter} from "./DataConverter.js";
import {Vetools} from "./Vetools.js";
import {UtilActiveEffects} from "./UtilActiveEffects.js";

class DataConverterVehicleUpgrade {
	/**
	 * @param vehUpgrade
	 * @param [opts] Options object.
	 * @param [opts.isAddPermission]
	 * @param [opts.isActorItem]
	 */
	static async pGetVehicleUpgradeItem (vehUpgrade, opts) {
		opts = opts || {};
		return this._pGetVehicleUpgradeItem_other(vehUpgrade, opts);
	}

	static async _pGetVehicleUpgradeItem_other (vehUpgrade, opts) {
		const descriptionValue = this._getDescriptionValue(vehUpgrade);

		const tempAdditionalData = vehUpgrade._foundryData || {};
		const additionalData = await this._pGetAdditionalData(vehUpgrade);

		const out = {
			name: UtilApplications.getCleanEntityName(DataConverter.getNameWithSourcePart(vehUpgrade, {isActorItem: opts.isActorItem})),
			data: {
				source: DataConverter.getSourceWithPagePart(vehUpgrade),
				description: {value: descriptionValue, chat: "", unidentified: ""},

				activation: {type: "", cost: 0, condition: ""},
				duration: {value: 0, units: ""},
				target: {value: 0, units: "", type: ""},
				range: {value: 0, long: 0, units: null},
				uses: {value: 0, max: 0, per: ""},
				ability: "",
				actionType: "",
				attackBonus: 0,
				chatFlavor: "",
				critical: null,
				damage: {parts: [], versatile: ""},
				formula: "",
				save: {ability: "", dc: null},
				requirements: tempAdditionalData.requirements || "",
				recharge: {value: 0, charged: true},

				...additionalData,
			},
			permission: {default: 0},
			type: "feat",
			img: await this._pGetImagePath(vehUpgrade),
			flags: this._getVehicleUpgradeFlags(vehUpgrade, opts),
			effects: [],
		};

		if (opts.isAddPermission) out.permission = {default: Config.get("importVehicleUpgrade", "permissions")};

		return out;
	}

	static _getDescriptionValue (vehUpgrade) {
		if (!Config.get("importVehicleUpgrade", "isImportDescription")) return "";

		return DataConverter.getWithDescriptionPlugins(() => {
			return `<div>
				<p><i>${Renderer.vehicle.getUpgradeSummary(vehUpgrade)}</i></div></p>
				${Renderer.get().setFirstSection(true).render({entries: vehUpgrade.entries}, 1)}
			</div>`;
		});
	}

	static _getVehicleUpgradeFlags (vehUpgrade, opts) {
		opts = opts || {};

		const out = {
			[SharedConsts.MODULE_NAME_FAKE]: {
				page: UrlUtil.PG_VEHICLES,
				source: vehUpgrade.source,
				hash: UrlUtil.URL_TO_HASH_BUILDER[UrlUtil.PG_VEHICLES](vehUpgrade),
			},
		};

		if (opts.isAddDataFlags) {
			out[SharedConsts.MODULE_NAME_FAKE].propDroppable = "vehicleUpgrade";
			out[SharedConsts.MODULE_NAME_FAKE].filterValues = opts.filterValues;
		}

		return out;
	}

	static async _pGetImagePath (vehUpgrade) {
		return `modules/${SharedConsts.MODULE_NAME}/media/icon/gears.svg`;
	}

	static async _pGetAdditionalData (vehUpgrade) {
		return DataConverter.pGetAdditionalData_(vehUpgrade, {propBrew: "foundryVehicleUpgrade", fnLoadJson: Vetools.pGetVehicleUpgradeSideData, propJson: "vehicleUpgrade"});
	}

	static async pHasVehicleUpgradeSideLoadedEffects (actor, vehUpgrade) {
		return (await DataConverter.pGetAdditionalEffectsRaw_(vehUpgrade, {propBrew: "foundryVehicleUpgrade", fnLoadJson: Vetools.pGetVehicleUpgradeSideData, propJson: "vehicleUpgrade"}))?.length > 0;
	}

	static async pGetVehicleUpgradeItemEffects (actor, vehUpgrade, sheetItem, {additionalData} = {}) {
		const effectsRaw = await DataConverter.pGetAdditionalEffectsRaw_(vehUpgrade, {propBrew: "foundryVehicleUpgrade", fnLoadJson: Vetools.pGetVehicleUpgradeSideData, propJson: "vehicleUpgrade"});
		return UtilActiveEffects.getExpandedEffects(effectsRaw || [], {actor, sheetItem, parentName: vehUpgrade.name, additionalData});
	}
}

export {DataConverterVehicleUpgrade};
