import { encodeData } from "@ax/helpers";
import { template } from "./config";
import { IServiceConfig, sendRequest, sendUploadRequest } from "./utils";
import { IGetFolderParams } from "@ax/types";

const SERVICES: { [key: string]: IServiceConfig } = {
	CREATE_IMAGE: {
		...template,
		endpoint: "/images",
		method: "POST",
	},
	GET_IMAGE: {
		...template,
		endpoint: "/image/",
		method: "GET",
	},
	UPDATE_IMAGE: {
		...template,
		endpoint: "/image/",
		method: "PUT",
	},
	DELETE_IMAGE: {
		...template,
		endpoint: "/image/",
		method: "DELETE",
	},
	DELETE_IMAGE_BULK: {
		...template,
		endpoint: "/image/bulk",
		method: "DELETE",
	},
	CREATE_THUMBNAIL: {
		...template,
		endpoint: ["/thumbnail/contentId/", "/contentType/"],
		method: "POST",
	},
	GET_IMAGES_FOLDER: {
		...template,
		endpoint: ["/images/site/", "/folder"],
		method: "GET",
	},
	GET_IMAGES_FOLDER_TREE: {
		...template,
		endpoint: ["/images/site/", "/folder/tree"],
		method: "GET",
	},
	MOVE_IMAGE: {
		...template,
		endpoint: ["/image/", "/folder/"],
		method: "PUT",
	},
	MOVE_IMAGE_BULK: {
		...template,
		endpoint: "/image/bulk/folder/",
		method: "PUT",
	},
	DOWNLOAD_IMAGES: {
		...template,
		endpoint: "/image/downloads/",
		method: "POST",
		responseType: "blob",
	},
	IMAGE_IN_USE: {
		...template,
		endpoint: ["/image/", "/inuse/"],
		method: "GET",
	},
	IMAGE_REPLACE: {
		...template,
		endpoint: ["/image/", "/replace"],
		method: "PUT",
	},
};

const createImage = async (data: FormData, setProgress?: (progress: number) => void) => {
	return sendUploadRequest(SERVICES.CREATE_IMAGE, data, setProgress);
};

const getImageInfo = async (imageID: number) => {
	const { host, endpoint } = SERVICES.GET_IMAGE;
	SERVICES.GET_IMAGE.dynamicUrl = `${host}${endpoint}${imageID}`;

	return sendRequest(SERVICES.GET_IMAGE);
};

const updateImage = async (imageID: number, data: any) => {
	const { host, endpoint } = SERVICES.UPDATE_IMAGE;
	SERVICES.UPDATE_IMAGE.dynamicUrl = `${host}${endpoint}${imageID}`;

	return sendRequest(SERVICES.UPDATE_IMAGE, { ...data });
};

const deleteImage = async (imageID: number) => {
	const { host, endpoint } = SERVICES.DELETE_IMAGE;
	SERVICES.DELETE_IMAGE.dynamicUrl = `${host}${endpoint}${imageID}`;

	return sendRequest(SERVICES.DELETE_IMAGE);
};

const deleteImageBulk = async (ids: number[]) => sendRequest(SERVICES.DELETE_IMAGE_BULK, { ids });

const createThumbnail = async (contentID: number, contentType: "form" | "navigation", data: FormData) => {
	const {
		host,
		endpoint: [prefix, suffix],
	} = SERVICES.CREATE_THUMBNAIL;
	SERVICES.CREATE_THUMBNAIL.dynamicUrl = `${host}${prefix}${contentID}${suffix}${contentType}`;

	return sendRequest(SERVICES.CREATE_THUMBNAIL, data);
};

const getImagesFolderContent = async (params: IGetFolderParams) => {
	const {
		host,
		endpoint: [prefix, suffix],
	} = SERVICES.GET_IMAGES_FOLDER;

	const {
		siteID,
		folderID,
		search,
		thumbHeight = 188,
		thumbWidth,
		pagination,
		page,
		itemsPerPage,
		query: filters = "",
	} = params;

	const data = {
		...(folderID && { folder: folderID }),
		...(search && { search }),
		...(thumbWidth && { thumbWidth }),
		...(thumbHeight && { thumbHeight }),
		...(pagination && { pagination }),
		...(page && { page }),
		...(itemsPerPage && { itemsPerPage }),
	};

	const queryParams = encodeData(data);
	const queryFilters = filters.length ? `${queryParams}&${filters}` : queryParams;
	const query = queryFilters.length ? `?${queryFilters}` : "";

	SERVICES.GET_IMAGES_FOLDER.dynamicUrl = `${host}${prefix}${siteID}${suffix}${query}`;

	return sendRequest(SERVICES.GET_IMAGES_FOLDER);
};

const getFoldersTree = async (siteID: number | "global") => {
	const {
		host,
		endpoint: [prefix, suffix],
	} = SERVICES.GET_IMAGES_FOLDER_TREE;

	SERVICES.GET_IMAGES_FOLDER_TREE.dynamicUrl = `${host}${prefix}${siteID}${suffix}`;

	return sendRequest(SERVICES.GET_IMAGES_FOLDER_TREE);
};

const moveImage = async (imageID: number, folderID: number | "root") => {
	const {
		host,
		endpoint: [prefix, suffix],
	} = SERVICES.MOVE_IMAGE;

	SERVICES.MOVE_IMAGE.dynamicUrl = `${host}${prefix}${imageID}${suffix}${folderID}`;

	return sendRequest(SERVICES.MOVE_IMAGE);
};

const moveImageBulk = async (ids: number[], folderID: number | "root") => {
	const { host, endpoint } = SERVICES.MOVE_IMAGE_BULK;

	SERVICES.MOVE_IMAGE_BULK.dynamicUrl = `${host}${endpoint}${folderID}`;

	return sendRequest(SERVICES.MOVE_IMAGE_BULK, { ids });
};

const downloadImages = async (imageID: number | number[], zip: boolean) => {
	const { host, endpoint } = SERVICES.DOWNLOAD_IMAGES;

	const query = zip ? "?zip=on" : "";

	SERVICES.DOWNLOAD_IMAGES.dynamicUrl = `${host}${endpoint}${imageID}${query}`;

	return sendRequest(SERVICES.DOWNLOAD_IMAGES);
};

const getImageInUse = async (imageID: number) => {
	const {
		host,
		endpoint: [prefix, suffix],
	} = SERVICES.IMAGE_IN_USE;

	SERVICES.IMAGE_IN_USE.dynamicUrl = `${host}${prefix}${imageID}${suffix}`;

	return sendRequest(SERVICES.IMAGE_IN_USE);
};

const replaceImage = async (data: FormData, imageID: number, setProgress?: (progress: number) => void) => {
	const {
		host,
		endpoint: [prefix, suffix],
	} = SERVICES.IMAGE_REPLACE;

	SERVICES.IMAGE_REPLACE.dynamicUrl = `${host}${prefix}${imageID}${suffix}`;

	return sendUploadRequest(SERVICES.IMAGE_REPLACE, data, setProgress);
};

export default {
	createImage,
	getImageInfo,
	updateImage,
	deleteImage,
	deleteImageBulk,
	createThumbnail,
	getImagesFolderContent,
	getFoldersTree,
	moveImage,
	moveImageBulk,
	downloadImages,
	getImageInUse,
	replaceImage,
};
