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";

class DataConverterBackground {
	// TODO(Future) expand/replace this as Foundry allows
	/**
	 * @param bg
	 * @param [opts] Options object.
	 * @param [opts.isAddPermission]
	 * @param [opts.filterValues] Pre-baked filter values to be re-used when importing this from the item.
	 */
	static getBackgroundItem (bg, opts) {
		opts = opts || {};

		const originalData = DataConverter.getCleanOriginalData(bg);

		const out = {
			name: UtilApplications.getCleanEntityName(DataConverter.getNameWithSourcePart(bg)),
			type: "feat",
			data: {
				description: {
					// TODO also import fluff here?
					value: Config.get("importBackground", "isImportDescription") ? DataConverter.getWithDescriptionPlugins(() => `<div>${Renderer.get().setFirstSection(true).render({type: "entries", entries: bg.entries})}</div>`) : "",
					chat: "",
					unidentified: "",
				},
				source: DataConverter.getSourceWithPagePart(bg),

				// region unused
				damage: {parts: []},
				activation: {type: "", cost: 0, condition: ""},
				duration: {value: null, units: ""},
				target: {value: null, units: "", type: ""},
				range: {value: null, long: null, units: ""},
				uses: {value: 0, max: 0, per: null},
				ability: null,
				actionType: "",
				attackBonus: 0,
				chatFlavor: "",
				critical: null,
				formula: "",
				save: {ability: "", dc: null},
				requirements: "",
				recharge: {value: null, charged: false},
				consume: {type: "", target: "", amount: null},
				// endregion
			},
			flags: {
				[SharedConsts.MODULE_NAME_FAKE]: {
					page: UrlUtil.PG_BACKGROUNDS,
					source: bg.source,
					hash: UrlUtil.URL_TO_HASH_BUILDER[UrlUtil.PG_BACKGROUNDS](bg),
					data: {
						background: originalData,
					},
					filterValues: opts.filterValues,
				},
			},
			effects: [],
			img: `modules/${SharedConsts.MODULE_NAME}/media/icon/farmer.svg`,
		};

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

		return out;
	}

	static async pGetBackgroundFeatureItem (bg, featureEntry, actor, opts) {
		const fauxEntry = this.getFauxBackgroundFeature(bg, featureEntry);

		return DataConverter.getItemActorPassive(
			featureEntry,
			{
				mode: "player",
				img: `modules/${SharedConsts.MODULE_NAME}/media/icon/mighty-force.svg`,
				fvttType: "feat",
				source: bg.source,
				actor,
				additionalData: await this._pGetFeatureAdditionalData(fauxEntry),
			},
		)
	}

	static getFauxBackgroundFeature (bg, entry) {
		return {
			source: bg.source,
			backgroundName: bg.name,
			backgroundSource: bg.source,
			srd: bg.srd || bg._baseSrd,
			...MiscUtil.copy(entry),
		};
	}

	static async _pGetFeatureAdditionalData (bgFeature) {
		return DataConverter.pGetAdditionalData_(
			bgFeature,
			{
				propBrew: "foundryBackgroundFeature",
				fnLoadJson: Vetools.pGeBackgroundSideData,
				propJson: "backgroundFeature",
				fnMatch: (ent, entAdd) => entAdd.name === ent.name && entAdd.source === ent.source && entAdd.backgroundName === ent.backgroundName && entAdd.backgroundSource === ent.backgroundSource,
			},
		);
	}
}

export {DataConverterBackground};
