'use strict';
import DataUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../utilities/DataUtils';
import Dexie from 'dexie';
import * as DataEvent from '../events/DataEvent';
export var COUNT;
export var FINAL_KEY;
export default class DBUtils {
	//--------------------------
	// constructor
	//--------------------------
	constructor() {
		/**
		 * NOTE: DB ERRORS REPLICATE.
		 * WHEN FIXING A BUG, FIX DATA WITH JSON BACKUP
		 */
		this.dataUtils = new DataUtils();
		this.db = new Dexie('fipamo_posts');
		this.db.version(1).stores({
			postList: 'id,post'
		});
		this.db.postList.toArray(array => {
			COUNT = array.length;
			FINAL_KEY = 0;
			if (COUNT != 0) FINAL_KEY = array[COUNT - 1].id;
		});
	}
	//--------------------------
	// methods
	//--------------------------
	modify(id, postData) {
		let self = this;
		let freshID;
		return new Promise(function(resolve, reject) {
			if (id == null) {
				self.db.postList
					.put(postData)
					.then(fresh => {
						freshID = fresh;
					})
					.catch(e => {
						let err = {
							message: 'PUT ERROR',
							error: e
						};

						return err;
					});
			} else {
				//console.log("UPDATED", postData);
				self.db.postList
					.update(Number(id), {
						post: postData
					})
					.then(() => {})
					.catch(e => {
						let err = {
							message: 'UPDATE ERROR',
							error: e
						};
						return err;
					});
			}
			self.db.postList.toArray(array => {
				self.syncRemote(array, freshID)
					.then(response => {
						resolve({
							response
						});
					})
					.catch(err => {
						reject({
							err
						});
					});
			});
		});
	}
	syncLocal(array) {
		let self = this;
		return new Promise(function(resolve, reject) {
			self.db.postList.clear().then(() => {
				self.db.postList
					.bulkAdd(array)
					.then(() => {
						self.db.postList.toArray(() => {
							let event = DataEvent.LOCAL_DB_READY;
							resolve({
								event
							});
						});
					})
					.catch(Dexie.BulkError, e => {
						reject({
							e
						});
					});
			});
		});
	}
	syncRemote(db, newPostId) {
		let self = this;
		return new Promise(function(resolve, reject) {
			self.dataUtils
				.request(
					'/api/post/sync',
					DataEvent.POSTS_SYNCED,
					REQUEST_TYPE_POST,
					CONTENT_TYPE_JSON,
					db
				)
				.then(response => {
					let bounce = {
						message: response,
						newPost: newPostId
					};
					resolve(bounce);
				})
				.catch(err => {
					reject(err);
				});
		});
	}
	getPost(id) {
		let self = this;
		if (id == null) {
			return new Promise(function(resolve, reject) {
				self.db.postList
					.toArray(array => {
						resolve(array);
					})
					.catch(err => {
						reject(err);
					});
			});
		} else {
			return new Promise(function(resolve, reject) {
				self.db.postList
					.get(Number(id))
					.then(obj => {
						resolve(obj);
					})
					.catch(err => {
						reject(err);
					});
			});
		}
	}
	archivePost(id, archive) {
		let self = this;
		return new Promise(function(resolve, reject) {
			self.db.postList
				.update(Number(id), {
					post: archive
				})
				.then(() => {
					self.db.postList.toArray(array => {
						self.syncRemote(array, null)
							.then(response => {
								resolve({
									response
								});
							})
							.catch(err => {
								reject({
									err
								});
							});
					});
				})
				.catch(() => {
					//console.log('ERROR', e);
				});
		});
	}
	//--------------------------
	// event handlers
	//--------------------------
}
