import DataUtils, {
	REQUEST_TYPE_POST,
	CONTENT_TYPE_JSON
} from '../../../../../brain//tools/utilities/DataUtils';
import DateUtils from '../../../../../brain/tools/utilities/DateUtils';
import * as DataEvent from '../../../../../brain/tools/events/DataEvent';
import DBUtils from '../../../../../brain/tools/utilities/DBUtils';
export default class NavActions {
	//--------------------------
	// constructor
	//--------------------------
	constructor() {
		this.dataUtils = new DataUtils();
		this.dateUtils = new DateUtils();
		this.dbutils = new DBUtils();
	}
	//--------------------------
	// methods
	//--------------------------
	save() {
		let self = this;
		let navData = [];
		let items = document.getElementById('nav-pages').children;
		for (let index = 0; index < items.length; index++) {
			navData.push({
				title: items[index].getElementsByTagName('label')[0].innerHTML,
				id: items[index].id,
				slug: items[index].getAttribute('data-slug'),
				uuid: items[index].getAttribute('data-uuid')
			});
		}
		return new Promise(function(resolve, reject) {
			self.dataUtils
				.request(
					'/api/settings/nav-sync',
					DataEvent.SETTINGS_UPDATED,
					REQUEST_TYPE_POST,
					CONTENT_TYPE_JSON,
					navData
				)
				.then(response => {
					resolve(response);
				})
				.catch(err => {
					reject(err);
				});
		});
	}
	removeItem(id) {
		let self = this;
		this.dbutils.getPost(id).then(post => {
			post.post.page = 'false';
			self.dbutils.modify(id, post.post).then(() => {
				document.getElementById('nav-pages').removeChild(document.getElementById(id));
				self.save();
			});
		});
	}
	//--------------------------
	// event handlers
	//--------------------------
}
