import { create_default_layout, pluck } from "./utils";
import { watch, ref, inject, computed, nextTick } from "vue";

export function getStore(print_format_name) {
	// variables
	let letterhead_name = ref(null);
	let print_format = ref(null);
	let letterhead = ref(null);
	let pagetype = ref(null);
	let meta = ref(null);
	let layout = ref(null);
	let dirty = ref(false);
	let edit_letterhead = ref(false);

	// methods
	function fetch() {
		return new Promise((resolve) => {
			jingrow.model.clear_pg("Print Format", print_format_name);
			jingrow.model.with_pg("Print Format", print_format_name, () => {
				let _print_format = jingrow.get_pg("Print Format", print_format_name);
				jingrow.model.with_pagetype(_print_format.pg_type, () => {
					meta.value = jingrow.get_meta(_print_format.pg_type);
					print_format.value = _print_format;
					layout.value = get_layout();
					nextTick(() => (dirty.value = false));
					edit_letterhead.value = false;
					resolve();
				});
			});
		});
	}
	function update({ fieldname, value }) {
		print_format.value[fieldname] = value;
	}
	function save_changes() {
		jingrow.dom.freeze(__("Saving..."));

		layout.value.sections = layout.value.sections
			.filter((section) => !section.remove)
			.map((section) => {
				section.columns = section.columns.map((column) => {
					column.fields = column.fields
						.filter((df) => !df.remove)
						.map((df) => {
							if (df.table_columns) {
								df.table_columns = df.table_columns.map((tf) => {
									return pluck(tf, [
										"label",
										"fieldname",
										"fieldtype",
										"options",
										"width",
										"field_template",
									]);
								});
							}
							return pluck(df, [
								"label",
								"fieldname",
								"fieldtype",
								"options",
								"table_columns",
								"html",
								"field_template",
							]);
						});
					return column;
				});
				return section;
			});

		print_format.value.format_data = JSON.stringify(layout.value);

		jingrow
			.call("jingrow.client.save", {
				pg: print_format.value,
			})
			.then(() => {
				if (letterhead.value && letterhead.value._dirty) {
					return jingrow
						.call("jingrow.client.save", {
							pg: letterhead.value,
						})
						.then((r) => (letterhead.value = r.message));
				}
			})
			.then(() => fetch())
			.always(() => {
				jingrow.dom.unfreeze();
			});
	}
	function reset_changes() {
		fetch();
	}
	function get_layout() {
		if (print_format.value) {
			if (typeof print_format.value.format_data == "string") {
				return JSON.parse(print_format.value.format_data);
			}
			return print_format.value.format_data;
		}
		return null;
	}
	function get_default_layout() {
		return create_default_layout(meta.value, print_format.value);
	}
	function change_letterhead(_letterhead) {
		return jingrow.db.get_pg("Letter Head", _letterhead).then((pg) => {
			letterhead.value = pg;
		});
	}

	// watch
	watch(layout, () => {
		dirty.value = true;
	});
	watch(print_format, () => {
		dirty.value = true;
	});

	return {
		letterhead_name,
		print_format,
		letterhead,
		pagetype,
		meta,
		layout,
		dirty,
		edit_letterhead,
		fetch,
		update,
		save_changes,
		reset_changes,
		get_layout,
		get_default_layout,
		change_letterhead,
	};
}

export function useStore() {
	// inject store
	let store = ref(inject("$store"));

	// computed
	let print_format = computed(() => {
		return store.value.print_format;
	});
	let layout = computed(() => {
		return store.value.layout;
	});
	let letterhead = computed(() => {
		return store.value.letterhead;
	});
	let meta = computed(() => {
		return store.value.meta;
	});

	return { print_format, layout, letterhead, meta, store };
}
