export default class ProductReviewTitleSkuController {

    constructor($log, $rootScope, $state, notify, $stateParams, RealtimeProductResource) {
        'ngInject';
        this.$log = $log;
        this.notify = notify;
        this.$state = $state;
        this.$rootScope = $rootScope;
        this.$stateParams = $stateParams;
        this.RealtimeProductResource = RealtimeProductResource;
        this.ProductEventNamesByHttpMethods = this.RealtimeProductResource.getRestEventNames();
    }

    $onInit() {
        this.productId = this.$state.params.productId;
        console.log(this.productId);

        this.productFullName = "Product"

        this.filters = {
            skus: "",
            titles: ""
        };

        this.registerEvents();
        this.loadProduct();

    }

    registerEvents() {
        this.$rootScope.$on(this.ProductEventNamesByHttpMethods.PUT, (e, product) => {
            if(product._id === this.productId) {
                this.loadProduct();
            }
        });
    }

    loadProduct() {
        return this.RealtimeProductResource.getOneById(this.productId)
            .then((_product) => {
                this.product = _product;
                this.product.fullName = _product.brandName + " " + _product.productName; 
                this.initializeProductReviewTitles();
            })
    }

    initializeProductReviewTitles() {
        this.product.reviewTitlesInfo = (this.product.reviewTitlesInfo) ? this.product.reviewTitlesInfo : [];

        this.product.reviewTitles = (this.product.reviewTitles) ? this.product.reviewTitles : [];
        this.product.reviewTitles.forEach((reviewTitle) => {
            const pair = this.product.reviewTitlesInfo.find(pair => pair.title === reviewTitle);
            if (!pair) {
                this.product.reviewTitlesInfo.push({
                    title: reviewTitle
                });
            }
        });
    }

    addSku(sku) {
        this.RealtimeProductResource.addSkuByProductAndSku(this.product, sku)
            .then((doc) => {
                this.notify.show('Product updated!','/img/notify.png');
                this.$log.info('Successfully updated', doc);
            })
            .catch((err) => {
                this.$log.info(err);
            })
            .finally(() => {
                this.skuToAdd = "";
                this.loadProduct();
            });
    };

    addReviewTitle(reviewTitleToAdd) {
        this.RealtimeProductResource.addReviewTitleByProductAndReviewTitle(this.product, reviewTitleToAdd)
            .then((doc) => {
                this.notify.show('Product updated!','/img/notify.png');
                this.$log.info('Successfully updated', doc);
            })
            .catch((err) => {
                this.$log.info(err);
            })
            .finally(() => {
                this.reviewTitleToAdd = "";
                this.loadProduct();
            });
    }


    goBack() {
        this.$state.go('catalog');
    }

}