import axios from 'axios'

const apiClient = axios.create({
	baseURL: 'https://server.riezel.com/api/v1',
	withCredentials: false,
	timeout: 120000,
	headers: {
		Accept: 'application/json',
		'Content-Type': 'application/json'
	}
})

export function getPhotoLogList() {
	return apiClient.get('/photologs/')
}

export function getPhotoLog(id) {
	return apiClient.get('/photolog/'+id+'/')
}

export function addNewPhotoLog(title, date) {
	return apiClient.post('/addphotolog/', {
		title,
		date: date,
		render_date: true,
		start_slide_image: null,
		slides: []
	})
}

export function updatePhotoLog({id, title, date, render_date, start_slide_image, slides}) {
	return apiClient.put('/updatephotolog/'+id+'/', {
		title,
		date,
		render_date,
		start_slide_image,
		slides
	})
}

export function deletePhotoLog(id) {
	return apiClient.delete('/deletephotolog/'+id+'/')
}

export function getPhotoGroups() {
	return apiClient.get('/photogroups/')
}

export function getPhotosByGroup(group_id) {
	return apiClient.get('/photos/?photogroup='+group_id)
}

export function getPhotoLogPDF(id) {
	return apiClient.get('/generatephotolog/'+id+'/')
}

export function addNewPhotoGroup(name, date) {
	return apiClient.post('/addphotogroup/', {
		name,
		date
	})
}

export function addNewPhoto(original_image, cropped_image=null, group_id=null, ocr_text=null, legacy_id=null) {
	const data = new FormData()

	data.append('original_image', original_image, original_image.name)

	if (cropped_image !== null)
		data.append('cropped_image', cropped_image, cropped_image.name)
	if (group_id !== null)
		data.append('group', group_id)
	if (ocr_text !== null)
		data.append('ocr_text', ocr_text)
	if (legacy_id !== null)
		data.append('legacy_id', legacy_id)

	const config = {
		headers: { 'Content-Type': 'multipart/form-data' }
	}
	
	return apiClient.post('/addphoto/', data, config)
}

export function updatePhoto(photo) {
	return apiClient.put('/updatephoto/'+photo.id+'/', photo)
}