import { AxiosInstance } from 'axios'

import { HealthStatus } from '../common/types'
import {
	FileData,
	UploadEventImageData,
	UploadEventImageResponse,
	UploadImageResponse,
	UploadProfileImageResponse,
	UploadVideoResponse,
} from './types'

export class UploadService {
	constructor(readonly client: AxiosInstance, readonly version: string) {}

	/**
	 * Returns true if the service is reachable
	 *
	 * @returns Services' online status
	 */
	async health(): Promise<HealthStatus> {
		try {
			const res = await this.client.get(`upload/health`)
			if (res.data.status === 'ok') {
				return { online: true }
			}
		} catch (e) {
			// Do nothing
		}

		return { online: false }
	}

	async uploadProfileImage(
		fileData: FileData
	): Promise<UploadProfileImageResponse> {
		const boundary = this.generateBoundary()

		const body = WriteMultipartForm(
			fileData.filename,
			fileData.content,
			boundary,
			'profile'
		)

		const response = await this.client.post(
			`${this.client.defaults.baseURL}/upload/${this.version}/profile`,
			body,
			{
				headers: {
					'Content-Type': `multipart/form-data; boundary=${boundary}`,
				},
			}
		)

		return { url: response.data.data[0].url }
	}

	async uploadImage(fileData: FileData): Promise<UploadImageResponse> {
		const boundary = this.generateBoundary()

		const body = WriteMultipartForm(
			fileData.filename,
			fileData.content,
			boundary,
			'file'
		)

		const response = await this.client.post(
			`${this.client.defaults.baseURL}/upload/${this.version}/send`,
			body,
			{
				headers: {
					'Content-Type': `multipart/form-data; boundary=${boundary}`,
				},
			}
		)

		return { url: response.data.url }
	}

	async uploadApiVideo(fileData: FileData): Promise<UploadVideoResponse> {
		const boundary = this.generateBoundary()

		const body = WriteMultipartForm(
			fileData.filename,
			fileData.content,
			boundary,
			'file'
		)

		const response = await this.client.post(
			`${this.client.defaults.baseURL}/upload/${this.version}/video/api-video`,
			body,
			{
				headers: {
					'Content-Type': `multipart/form-data; boundary=${boundary}`,
				},
			}
		)

		return { url: response.data.data[0].url }
	}

	/**
	 * Upload the event images with different formats: 1x1, 4x3, and 16x9
	 * @param uploadImageData.image1x1 Content and filename of the 1x1 event image
	 * @param uploadImageData.image4x3 Content and filename of the 4x3 event image
	 * @param uploadImageData.image16x9 Content and filename of the 16x9 event image
	 * @returns Urls of the 1x1, 4x3, and 16x9 images
	 */
	async uploadEventImage(
		uploadImageData: UploadEventImageData
	): Promise<UploadEventImageResponse> {
		const formData = new FormData()
		const blob1x1 = new Blob([uploadImageData.image1x1.content])
		formData.append('1x1', blob1x1, uploadImageData.image1x1.filename)

		const blob4x3 = new Blob([uploadImageData.image4x3.content])
		formData.append('4x3', blob4x3, uploadImageData.image4x3.filename)

		const blob16x9 = new Blob([uploadImageData.image16x9.content])
		formData.append('16x9', blob16x9, uploadImageData.image16x9.filename)

		const response = await this.client.post(
			`${this.client.defaults.baseURL}/upload/${this.version}/image`,
			formData,
			{
				headers: {
					'Content-Type': `multipart/form-data;`,
				},
			}
		)

		const data = response.data.data as { format: string; url: string }[]

		return {
			file1x1Url: data.find((item) => item.format === '1x1').url,
			file4x3Url: data.find((item) => item.format === '4x3').url,
			file16x9Url: data.find((item) => item.format === '16x9').url,
		}
	}

	/**
	 * Boundary is used to separate fields in multipart, a value that should not appear in the data.
	 * This value is added in the `Content-Type` header.
	 */
	private generateBoundary() {
		return `----CustomBoundary${Date.now()}`
	}
}

/**
 * Method creates multipart body with provided file.
 *
 * @param fileName
 * @param fileData
 * @param boundary Parameter used for the multipart boundary.
 * @returns Multipart body as Uint8Array.
 */
function WriteMultipartForm(
	fileName: string,
	fileData: Buffer,
	boundary: string,
	name: string
): Uint8Array {
	// Header contains metadata for file and the initial boundary
	const header = new Uint8Array(
		Buffer.from(
			`--${boundary}\r\nContent-Disposition: form-data; name="${name}"; filename="${fileName}"\r\nContent-Type: "application/octet-stream"\r\n\r\n`
		)
	)

	const lastBoundary = new Uint8Array(Buffer.from(`\r\n--${boundary}--`))

	const finalData = new Uint8Array(
		header.byteLength + lastBoundary.byteLength + fileData.byteLength
	)

	// Put together header, data and last boundary in one array.
	finalData.set(header, 0)
	finalData.set(fileData, header.byteLength)
	finalData.set(lastBoundary, header.byteLength + fileData.byteLength)

	return finalData
}
