export default class KeywordtagController {



    constructor($window, $scope, Tag, Keyword2tagService, notify) {
        'ngInject';
		this.$window = $window;
        this.$scope = $scope;
        this.Tag = Tag;
        this.Keyword2tagService = Keyword2tagService;
        this.notify = notify;
		this.keyword2tagTags = [];
		this.currentPage = 0;
		this.NUMBER_OF_DOCUMENTS_PER_PAGE = 50;
    }

    $onInit() {
		this.loading = true;
		return this.getDomainNamesFromTags()
		.then((domainNames) => {
			this.domains = domainNames;
			this.currentDomain = domainNames[0];
			return this.getTagUidsByDomainName(this.currentDomain);
		})
		.then((uids) => {
			this.domainRelatedTagUids = uids;
			return this.getKeyword2tagByDomainName(this.currentDomain);
		})
		.then((keyword2tags) => {	
			this.keyword2tags = keyword2tags;
			this.tmpKeyword2tags = angular.copy(keyword2tags);
			return this.getCountersByDomainName(this.currentDomain);
		})
		.then(() => {
			this.loading = false;
		});
    }


	getDomainNamesFromTags() {
		return this.Tag.getAllDomains();
	};

	getKeyword2tagIndexById(_id) {
		var output;
		this.keyword2tags.forEach( (elem, index) => {
			if (elem._id === _id) {
				output = index;
			}
		});
		return output;
	};

	getTagUidsByDomainName(domainName) {
		return this.Tag.getAllUidsByDomainName(domainName);
	};

	getKeyword2tagByDomainName(domainName, page) {
		return this.Keyword2tagService.queryByDomainName(domainName, this.NUMBER_OF_DOCUMENTS_PER_PAGE, page);
	};

	getCountersByDomainName(domainName) {
		return this.Keyword2tagService.count({
			domain: domainName
		}).then( (cnt) => {
			this.totalCnt = cnt.replace(/"/g, '');
			return this.Keyword2tagService.count({
				'$and': [
					{
						domain: domainName
					},{
						status: 'approved'
					}
				]
			});
		}).then( (cnt) => {
			this.approvedCnt = cnt.replace(/"/g, '');
			return this.Keyword2tagService.count({
				'$and': [
					{
						domain: domainName
					},{
						status: 'rejected'
					}
				]
			});
		}).then( (cnt) => {
			this.rejectedCnt = cnt.replace(/"/g, '');
			return this.Keyword2tagService.count({
				'$and': [
					{
						domain: domainName
					},{
						status: {
							'$exists': false
						}
					}
				]
			});
		}).then( (cnt) => {
			this.newCnt = cnt.replace(/"/g, '');
		});	
	};

	back() {
		this.$window.history.back();
	}

	setStatusFilter(statusFilter) {
		this.statusFilter = statusFilter;
	};

	removeStatusFilter() {
		this.statusFilter = null;
	};

	saveKeyword2tag(keyword2tag) {
		keyword2tag.status = 'approved';
		var index = this.getKeyword2tagIndexById(keyword2tag._id);
		return this.Keyword2tagService.update(keyword2tag)

		.then( (_keyword2tag) => {
			console.log('Successfully updated keyword2tag');
			this.this.notify.show('Successfully approved keyword ' + keyword2tag.keyword);
			this.keyword2tags[index] = _keyword2tag;
			return keyword2tag;
		})

		.catch( () => {
			this.tmpKeyword2tags[index] = angular.copy(this.keyword2tags[index]);
		});
	};

	rejectKeyword(keyword2tag) {

		keyword2tag.status = 'rejected';
		var index = this.getKeyword2tagIndexById(keyword2tag._id);

		return this.Keyword2tagService.update(keyword2tag)

		.then( (_keyword2tag) => {
			this.keyword2tags[index] = _keyword2tag;
			this.notify.show('Successfully rejected keyword ' + keyword2tag.keyword);
			return _keyword2tag;
		})

		.catch( () => {
			this.tmpKeyword2tags[index] = angular.copy(this.keyword2tags[index]);
		});
	};

	changeCurrentDomain(newCurrentDomain) {	
		if (this.currentDomain === newCurrentDomain) {
			return;
		}
		this.currentDomain = newCurrentDomain;
		this.loading = true;
		return this.getTagUidsByDomainName(this.currentDomain)

		.then( (uids) => {
			this.domainRelatedTagUids = uids;
			return this.getKeyword2tagByDomainName(this.currentDomain);
		})

		.then( (keyword2tags) => {	
			this.keyword2tags = keyword2tags;
			this.tmpKeyword2tags = angular.copy(keyword2tags);
			return this.getCountersByDomainName(this.currentDomain);
		})

		.then( () => {
			this.loading = false;
		});
	};

	addPage() {
		this.loading = true;
		this.currentPage++;
		return this.getKeyword2tagByDomainName(this.currentDomain, this.currentPage)

		.then( (keyword2tags) => {
			this.keyword2tags =  this.keyword2tags.concat(keyword2tags);
			this.tmpKeyword2tags = this.tmpKeyword2tags.concat(angular.copy(keyword2tags));
			this.loading = false;
		});
	};

}



