'use strict';

class BadgeSetupController {
	constructor(RealtimeProductResource, notify) {
		'ngInject';
		this.RealtimeProductResource = RealtimeProductResource;
		this.notify = notify;
	}

	$onInit() {
		this.typeOfAnalysisValues = ['Planned One-time deep dives', 'Monthly tracking'];
		this.minDate = this.product.minDate;
		this.maxDate = new Date();
		if (!this.product.updatedOn) {
			this.product.updatedOn = new Date();
		}
	}

	updateTypeOfAnalysisOfProduct() {
		if (this.tmpProducts[this.index].typeOfAnalysis && this.tmpProducts[this.index].typeOfAnalysis !== '') {

			return this.RealtimeProductResource.setTypeOfAnalyisByProductAndTypeOfAnalysis(this.tmpProducts[this.index], this.tmpProducts[this.index].typeOfAnalysis)

			.then(function() {
				this.product = this.tmpProducts[thisindex];
				notify('Successfully updated product');
			})

			.catch(function(err) {
				console.log(err);
				notify({
					message: 'Error while updating product',
					classes: ['notify-error']
				});
				this.tmpProducts[this.index] = this.product;
			});
		} else {
			this.tmpProducts[this.index] = this.product;
			notify({
				message: 'Please specify a value for the type of analysis',
				classes: ['notify-warning']
			});
		}
	};

	updateUpdatedOnDateOfProduct() {
		if (this.tmpProducts[this.index].updatedOn && !isNaN(this.tmpProducts[this.index].updatedOn.getTime())) {

			return this.RealtimeProductResource.setUpdatedOnByProductAndDate(this.tmpProducts[this.index], this.tmpProducts[this.index].updatedOn)
				.then(function() {
					notify('Successfully updated product');
					this.product = this.tmpProducts[$scope.$index];
				})
				.catch(function(err) {
					console.log(err);
					this.tmpProducts[this.index] = this.product;
					notify({
						message: 'Error while updating prouduct',
						classes: ['notify-error']
					});
				});
		} else {
			this.tmpProducts[this.index] = this.product;
			notify({
				message: 'Error while updating product',
				classes: ['notify-error']
			});
		}
	};


	// hasProductRefresedhBadge() {
	//   var todayInMilliseconds,
	//     updatedOnInMilliseconds,
	//     moreThan25DaysPassed;
	//   if (!this.product || !this.product.typeOfAnalysis || !this.product.updatedOn) {
	//     return false;
	//   }
	//   todayInMilliseconds = new Date().getTime();
	//   updatedOnInMilliseconds = Date.parse(this.product.updatedOn);
	//   moreThan25DaysPassed = (todayInMilliseconds - updatedOnInMilliseconds) > 25 * 24 * 60 * 60 * 1000;
	//   return this._$filter('prettifyTypeOfAnalysis')(this.product.typeOfAnalysis) === 'Tracking' && !moreThan25DaysPassed;
	// };

	// hasProductNewBadge() {
	//   var todayInMilliseconds,
	//     updatedOnInMilliseconds,
	//     moreThan25DaysPassed;
	//   if (!this.product || !this.product.typeOfAnalysis || !this.product.updatedOn) {
	//     return false;
	//   }
	//   todayInMilliseconds = new Date().getTime();
	//   updatedOnInMilliseconds = Date.parse(this.product.updatedOn);
	//   moreThan25DaysPassed = (todayInMilliseconds - updatedOnInMilliseconds) > 25 * 24 * 60 * 60 * 1000;
	//   return this._$filter('prettifyTypeOfAnalysis')(this.product.typeOfAnalysis) === 'One time' && !moreThan25DaysPassed;
	// };

}

 export default BadgeSetupController;
