class manageReviewTitlesController {

    constructor(RealtimeProductResource, $log, notify, $sce) {
        'ngInject';
        this.RealtimeProductResource = RealtimeProductResource;
        this.$log = $log;
        this.notify = notify;
        this.$sce = $sce;
    } 

    $onInit() {
        this.skus = [];
        this.selectSku = {};
        this.newSku = false;

    }

    searchBusiness(query) {
        console.log(query);
        if(query){
            this.RealtimeProductResource.searchForProductsByProductNamePart(query)
            .then((businesses) => {
                this.businesses = businesses;
            });
        }
    };

    removeSKUFromReviewTitle(skuTitlePair) {
        this.RealtimeProductResource.addReviewTitle2SkuByProductAndReviewTitleAndSku(this.product, skuTitlePair.title, "", skuTitlePair.sku)
            .then((doc) => {
                this.notify.show('Product updated!','/img/notify.png');
            })
            .catch((err) => {
                this.$log.info(err);
            })
            .finally(() => {
                this.onUpdate();
            })
    };

    // not used
    changeSKUToReviewTitle(skuTitlePair) {
        this.RealtimeProductResource.addReviewTitle2SkuByProductAndReviewTitleAndSku(this.product, skuTitlePair.title, skuTitlePair.selected, skuTitlePair.sku)
            .then((doc) => {
                this.notify.show('Product updated!','/img/notify.png');
            })
            .catch((err) => {
                this.$log.info(err);
            })
            .finally(() => {
                this.onUpdate();
            })
    };

    moveReviewTitle(skuTitlePair){
        const destinationProduct = skuTitlePair.selectedForMove;
        const titleToBeMoved = skuTitlePair.title;
        this.RealtimeProductResource.addReviewTitleByProductAndReviewTitle(destinationProduct, titleToBeMoved)
        .then(() => this.RealtimeProductResource.removeReviewTitleByProductAndReviewTitle(this.product, titleToBeMoved))
        .then(() => this.RealtimeProductResource.deleteReviewTitle2SkuByProductAndReviewTitleAndSku(this.product, titleToBeMoved, skuTitlePair.sku))        
        .catch((err) => {
            this.$log.info(err);
        })
        .finally(() => {
            this.onUpdate();
        })
    }

    removeReviewTitle(skuTitlePair){
        const titleToBeMoved = skuTitlePair.title;
        this.RealtimeProductResource.removeReviewTitleByProductAndReviewTitle(this.product, titleToBeMoved)
        .then(() => this.RealtimeProductResource.deleteReviewTitle2SkuByProductAndReviewTitleAndSku(this.product, titleToBeMoved, skuTitlePair.sku))      
        .catch((err) => {
            this.$log.info(err);
        })
        .finally(() => {
            this.onUpdate();
        })
    }

    addReviewTitle(product, reviewTitle) {
        this.RealtimeProductResource.addReviewTitleByProductAndReviewTitle(product, reviewTitle)
            .then((newProduct) => {
                product.reviewTitles = newProduct.reviewTitles;
                this.$log.info('Product updated!');
                this.notify.show('Product updated','/img/notify.png');
            });
    };

    addSKUToReviewTitle(skuTitlePair) {

        this.RealtimeProductResource.addSkuByProductAndSku(this.product, skuTitlePair.selected)
        .then( () => this.RealtimeProductResource.addReviewTitle2SkuByProductAndReviewTitleAndSku(this.product, skuTitlePair.title, skuTitlePair.selected, skuTitlePair.sku) )
        .then((doc) => {
            this.notify.show('Product updated!','/img/notify.png');
        })
        .catch((err) => {
            this.$log.info(err);
        })
        .finally(() => {
            this.onUpdate();
        });
    };


    trustAsHtml(value) {
        return this.$sce.trustAsHtml(value);
    };

    skuSelected(skuTitlePair) {
        if (!this.isNewSku(skuTitlePair)) {
            this.changeSKUToReviewTitle(skuTitlePair, this.selectSku.selected)
        }
    }

    isNewSku(skuTitlePair) {
        let isNew = false;
        if (skuTitlePair.selected && (this.product.skus.indexOf(skuTitlePair.selected) === -1) ) {
            isNew = true;
        }
        return isNew;
    }

    getSkues(search) {
     var newSkues = this.product.skus ? this.product.skus.slice() : [];
      if (search && newSkues.indexOf(search) === -1) {
        newSkues.unshift(search);
      }
      return newSkues;
    }
}

export default manageReviewTitlesController;