import DataUtils, {
	REQUEST_TYPE_POST,
	CONTENT_TYPE_JSON
} from '../../../../../brain//tools/utilities/DataUtils';
import StringUtils from '../../../../../brain//tools/utilities/StringUtils';
import DateUtils from '../../../../../brain/tools/utilities/DateUtils';
import DBUtils from '../../../../../brain/tools/utilities/DBUtils';
var uuidv4 = require('uuid/v4');
import * as DataEvent from '../../../../../brain/tools/events/DataEvent';
export default class PostActions {
	//--------------------------
	// constructor
	//--------------------------
	constructor() {
		this.dataUtils = new DataUtils();
		this.dateUtils = new DateUtils();
		this.dbUtils = new DBUtils();
	}
	//--------------------------
	// methods
	//--------------------------
	update(id, data, files, lastKey) {
		let self = this;
		let freshData;
		return new Promise(function(resolve, reject) {
			let txt = document.createElement('textarea');
			txt.innerHTML = document.getElementById('edit-post-text').innerHTML;
			let html = txt.value;
			html = html.replace(/<\/?span[^>]*>/g, ''); //removes highightjs styling
			html = html.replace(/<\/?br[^>]*>/g, '\n'); //convert back to encoded line break for storage
			data.title = document.getElementById('post_title').value;
			data.slug = new StringUtils().cleanString(document.getElementById('post_title').value);
			data.plaintext = html;
			data.html = html;
			data.created = document.getElementById('post-date').value;
			data.tags = document.getElementById('post_tags').value;
			data.page = document.getElementById('option-page').getAttribute('data-active');
			data.featured = document.getElementById('option-feature').getAttribute('data-active');
			data.published = document
				.getElementById('option-published')
				.getAttribute('data-active');
			if (files.length != 0) {
				for (var i = 0; i < files.length; i++) {
					var file = files[i];
					// Check the file type.
					if (!file.type.match('image.*')) {
						continue;
					}
					data.feature =
						'/content/blog-images/' +
						self.dateUtils.getDate('year', new Date()) +
						'/' +
						self.dateUtils.getDate('month', new Date()) +
						'/' +
						file.name;
				}
			} else {
				if (typeof data.feature == 'undefined') data.feature = '';
			}
			if (id == null) {
				freshData = {
					id: lastKey + 1,
					post: {
						uuid: uuidv4(),
						title: data.title,
						slug: data.slug,
						plaintext: data.plaintext,
						html: data.html,
						feature: data.feature,
						created: data.created,
						tags: data.tags,
						page: data.page,
						featured: data.featured,
						published: data.published,
						deleted: '',
						author: 'user'
					}
				};
			} else {
				freshData = data;
			}
			self.dbUtils
				.modify(id, freshData)
				.then(response => {
					resolve(response);
					if (id != null)
						freshData.page == 'true'
							? self.updateNav(true, id, freshData)
							: self.updateNav(false, id, freshData);
				})
				.catch(err => {
					reject(err);
				});
		});
	}
	deletePost(id, body) {
		let self = this;
		body.deleted = new Date().toString();
		return new Promise(function(resolve, reject) {
			self.dbUtils
				.archivePost(id, body)
				.then(response => {
					//console.log(response);
					resolve(response);
				})
				.catch(err => {
					//console.log(err);
					reject(err);
				});
		});
	}
	updateNav(add, id, post) {
		var self = this;
		this.dataUtils
			.request('/api/settings/nav', DataEvent.SETTINGS_LOADED)
			.then(response => {
				let menu = JSON.parse(response.request['response']);
				let item = {
					id: id,
					uuid: post.uuid,
					title: post.title,
					slug: post.slug
				};
				if (add) {
					menu.push(item);
				} else {
					for (let index = 0; index < menu.length; index++) {
						if (menu[index].id == id) {
							menu.splice(index, 1);
						}
					}
				}
				self.dataUtils
					.request(
						'/api/settings/nav-sync',
						DataEvent.SETTINGS_UPDATED,
						REQUEST_TYPE_POST,
						CONTENT_TYPE_JSON,
						menu
					)
					.then(() => {
						//console.log(response);
					})
					.catch(() => {
						//console.log(err);
					});
			})
			.catch(() => {
				//console.log(err);
			});
	}
	//--------------------------
	// event handlers
	//--------------------------
}
