import { createStore } from 'vuex'
import { 
	getPhotoLogList,
	deletePhotoLog,
	addNewPhotoLog,
	getPhotoLog,
	updatePhotoLog,
	getPhotoGroups,
	getPhotosByGroup
} from '@/api'

export default createStore({
	state: {
		photoLogList: [],
		photoLogs: [],
		photoGroups: [],
		photos: []
	},
	mutations: {
		SET_PHOTO_LOG_LIST(state, newPhotoLogList) {
			state.photoLogList = newPhotoLogList
		},
		SET_PHOTO_LOG(state, photoLog) {
			let log_index = state.photoLogs.findIndex(log => log.id === photoLog.id)
			if (log_index > -1) {
				state.photoLogs[log_index] = photoLog
			} else {
				state.photoLogs.push(photoLog)
			}
		},
		REMOVE_PHOTO_LOG(state, id) {
			let log_index = state.photoLogList.findIndex(log => log.id === id)
			state.photoLogList.splice(log_index, 1)
		},
		SET_PHOTO_GROUPS(state, groups) {
			state.photoGroups = groups
		},
		SET_PHOTOS_IN_GROUP(state, {group_id, photos}) {
			if (group_id in state.photos) {
				state.photos.group_id = photos
			} else {
				state.photos = {...state.photos, [group_id]: photos}
			}
		}
	},
	actions: {
		loadPhotoLogList({commit}) {
			return new Promise((resolve, reject) => {
				getPhotoLogList().then((response) => {
					commit('SET_PHOTO_LOG_LIST', response.data)
					resolve()
				}).catch((error) => {
					console.log(error)
					reject()
				})
			})
		},
		loadPhotoLog({commit}, id) {
			return new Promise((resolve, reject) => {
				getPhotoLog(id).then((response) => {
					commit('SET_PHOTO_LOG', response.data)
					resolve()
				}).catch((error) => {
					console.log(error)
					reject()
				})
			})
		},
		deletePhotoLog({commit}, id) {
			deletePhotoLog(id).then(() => {
				commit('REMOVE_PHOTO_LOG', id)
			}).catch((error) => {
				console.log(error)
			})
		},
		addNewPhotoLog({dispatch}, {title, date}) {
			return new Promise((resolve, reject) => {
				addNewPhotoLog(title, date).then((response) => {
					dispatch('loadPhotoLogList')
					resolve(response.data.id)
				}).catch((error) => {
					console.log(error)
					reject(error)
				})
			})
		},
		updatePhotoLogDetails({commit}, photolog) {
			if (photolog !== null) {
				commit('SET_PHOTO_LOG', photolog)
				return updatePhotoLog(photolog)
			}
		},
		loadPhotoGroups({commit}) {
			return new Promise((resolve, reject) => {
				getPhotoGroups().then((response) => {
					commit('SET_PHOTO_GROUPS', response.data)
					resolve(response.data)
				}).catch((error) => {
					reject(error)
				})
			})
		},
		loadPhotosInGroup({commit}, group_id) {
			return new Promise((resolve, reject) => {
				getPhotosByGroup(group_id).then((response) => {
					commit('SET_PHOTOS_IN_GROUP', {group_id, photos: response.data})
					resolve(response.data)
				}).catch((error) => {
					reject(error)
				})
			})
		},
		loadPhotosInAllGroups({dispatch, getters}) {
			for (const index in getters.photoGroups) {
				const group_id = getters.photoGroups[index].id
				dispatch('loadPhotosInGroup', group_id)
			}
		}
	},
	getters: {
		photoLogById: (state) => (id) =>  {
			let log = state.photoLogList.filter(log => log.id == id)
			if (log.length > 0) {
				return log[0]
			}
			return null
		},
		photoLogDetailsById: (state) => (id) =>  {
			let log = state.photoLogs.filter(log => log.id == id)
			if (log.length > 0) {
				return log[0]
			}
			return null
		},
		photoGroups (state) {
			return state.photoGroups
		},
		photos (state) {
			return state.photos
		}
	},
	modules: {},
})
