import {  buildUrl, URL_PREFIX } from  './../../before-each';
/* global buildUrl */
'use strict';

describe('Directive: tagSelection', function() {

    var element = null;
    var tagSelectionData = null;
    var $httpBackend = null;
    var Model = null;
    var HostFactory = null;
    var tagService = null;
    var $q = null;
    var getUrlByCollectionName;

    beforeEach(inject(function($rootScope, $compile, _$httpBackend_, _Model_, _HostFactory_, _TagSelectionData_, _Tag_, _$q_) {
        var scope;
        getUrlByCollectionName = function(collectionName){
            return buildUrl(collectionName);
        }

        $httpBackend = _$httpBackend_;
        Model = _Model_;
        HostFactory = _HostFactory_;
        tagSelectionData = _TagSelectionData_;
        tagService = _Tag_;
        $q = _$q_;
        scope = $rootScope.$new();

        scope.review = JSON.parse(JSON.stringify(tagSelectionData.review));
        scope.product = {
            tagsDomain: 'male grooming',
            language: 'en',
            selectedText: ''
        };

        tagService.getAllTags = function() {
            var defer = $q.defer();
            defer.resolve(tagSelectionData.tagsInput);
            return defer.promise;
        };

        var model = new Model({
            scheme: HostFactory.getScheme(),
            host: HostFactory.getHost(),
            dbName: HostFactory.getDbUrlComponent(),
            collectionName: 'tags'
        });

        var queryUrl = buildUrl('tags', {
            languages: {
                $in: scope.languages
            },
            domain: scope.tagsDomain
        }, {
            sort: {
                name: 1
            }
        }, model);
        $httpBackend.whenGET(queryUrl).respond(200, tagSelectionData.tagsInput);
        $httpBackend.whenGET('views/directives/tag-selection.html').respond(200, '<div></div>')

        element = $compile('<tag-selection tags-domain="product.tagsDomain" language="product.language" review="review" origin="free-text" selectedText="product.selectedText"></tag-selection>')(scope);
        scope.$digest();
    }));

    it('Should have `tagsDomain` field in it’s scope and initialize it with proper value', function() {
        var isolatedScope = element.isolateScope();
        expect(isolatedScope.tagsDomain).to.exist;
        expect(isolatedScope.tagsDomain).to.eql('male grooming');
    });

    it('Should have a `language` field in its scope, which represents the language of the review', function() {
        var isolatedScope = element.isolateScope();
        expect(isolatedScope.language).to.exist;
    });

    it('Should have `categories`, `uids`, `uidsKeys` and `categoriesKeys` fields in its scope set to the right values', function() {
        var isolatedScope = element.isolateScope();


        expect(isolatedScope.categories).to.eql(tagSelectionData.expectedCategories);
        expect(isolatedScope.uids).to.eql(tagSelectionData.expectedUidTree);
        expect(isolatedScope.uidsKeys).to.eql(tagSelectionData.uidsKeys);
        expect(isolatedScope.categoriesKeys).to.deep.eql(tagSelectionData.categoriesKeys);
    });

    it('Should have `visualizationType` field in its scope inizialized to ‘tree’', function() {
        var isolatedScope = element.isolateScope();
        expect(isolatedScope.visualizationType).to.eql('tree');
    });

    it('Should have `addFeature2Sentiment` function which, given a category, an “uid”, a “sentiment” and a “name”  save an annotation to the current review. After saving the review, the review includes the new annotation', function() {

        var isolatedScope = element.isolateScope();

        var category = 'Overall Performance';
        var uid = 'Ergonomics';
        var sentiment = 1;
        var synonym = 'blades are sharp';

        var query, options;

        isolatedScope.selectedText = {
            text: 'blades are sharp',
            start: 219,
            offset: 20
        };

        query = buildUrl('tag', {
            'domain': 'male grooming',
            'languages': {
                '$in': ['en']
            },
            'name': 'blades are sharp',
            'type': {
                '$ne': 'company-generated'
            }
        }, {
            'upsert': true
        });


        var query_ = {
            '+$push': {
                feature2sentiment: {
                    category: 'Overall Performance',
                    id: '_bladesaresharp_219_20_Ergonomics_OverallPerformance_',
                    name: 'blades are sharp',
                    offset: 20,
                    sentiment: 1,
                    start: 219,
                    syn: 'blades are sharp',
                    text: 'blades are sharp',
                    uid: 'Ergonomics',
                    version: 'v2'
                        // TODO: here also the field origin should be tested
                }
            }
        };

        $httpBackend.whenPUT( buildUrl('text_reviews_for_analytics/5425f6340b751fc2ca270113'), {
            'options':  {},
            'query':    null,
            'update':   {
                '+$push': {
                    feature2sentiment: {
                        category: 'Overall Performance',
                        id: '_bladesaresharp_219_20_Ergonomics_OverallPerformance_',
                        name: 'blades are sharp',
                        offset: 20,
                        sentiment: 1,
                        start: 219,
                        syn: 'blades are sharp',
                        text: 'blades are sharp',
                        uid: 'Ergonomics',
                        version: 'v2'
                            // TODO: here also the field origin should be tested
                    }
                }
            }
        }  )
        .respond(200, tagSelectionData.review2);
        $httpBackend.whenPUT( buildUrl('tags'), {
            'options':  {
                'upsert': true
            },
            'query':    {
                '+$and': [
                    {
                        'type': {
                            '+$ne': 'company-generated'
                        }
                    },
                    {
                        'type': {
                            '+$ne': 'ana-generated'
                        }
                    }
                ],
                'domain': 'male grooming',
                'languages': {
                    '+$in': ["en"]
                },
                'name': 'blades are sharp'
            },
            'update':   {
                '+$set': {
                    name: 'blades are sharp',
                    type: 'user-generated',
                    domain: 'male grooming',
                    syn: 'blades are sharp',
                    uid: 'Ergonomics',
                    category: 'Overall Performance'
                },
                '+$push': {
                    languages: 'en'
                }
            }
        }).respond(200, {
            name: 'blades are sharp',
            type: 'user-generated',
            domain: 'male grooming',
            syn: 'blades are sharp',
            uid: 'Ergonomics',
            category: 'Overall Performance',
            languages: ['en']
        });

        isolatedScope.addFeature2Sentiment(category, uid, sentiment, synonym); 
        
        $httpBackend.flush();

        expect(isolatedScope.categories).to.deep.eql(tagSelectionData.expectedCategoriesAfterAddingNewTag);
        expect(isolatedScope.uids).to.eql(tagSelectionData.expectedUidTreeAfterAddingNewTag);


    });



    it('Should have `addFeature2Sentiment` function which, given an “uid”, a “sentiment” and a “name” and an undefined “category”, it saves a review that includes the annotation created by the input parameters including the calculated category', function() {

        var isolatedScope = element.isolateScope();

        var category = 'Overall Performance';
        var uid = 'Ergonomics';
        var sentiment = 1;
        var synonym = 'blades are sharp';

        var query, options;

        isolatedScope.selectedText = {
            text: 'blades are sharp',
            start: 219,
            offset: 20
        };

        query = buildUrl('tag', {
            'domain': 'male grooming',
            'languages': {
                '$in': ['en']
            },
            'name': 'blades are sharp',
            'type': {
                '$ne': 'company-generated'
            }
        }, {
            'upsert': true
        });


        var query_ = {
            '+$push': {
                feature2sentiment: {
                    category: 'Overall Performance',
                    id: '_bladesaresharp_219_20_Ergonomics_OverallPerformance_',
                    name: 'blades are sharp',
                    offset: 20,
                    sentiment: 1,
                    start: 219,
                    syn: 'blades are sharp',
                    text: 'blades are sharp',
                    uid: 'Ergonomics',
                    version: 'v2'
                        // TODO: here also the field origin should be tested
                }
            }
        };

        $httpBackend.whenPUT( buildUrl('text_reviews_for_analytics/5425f6340b751fc2ca270113'), {
            'options':  {},
            'query':    null,
            'update':   {
                '+$push': {
                    feature2sentiment: {
                        category: 'Overall Performance',
                        id: '_bladesaresharp_219_20_Ergonomics_OverallPerformance_',
                        name: 'blades are sharp',
                        offset: 20,
                        sentiment: 1,
                        start: 219,
                        syn: 'blades are sharp',
                        text: 'blades are sharp',
                        uid: 'Ergonomics',
                        version: 'v2'
                            // TODO: here also the field origin should be tested
                    }
                }
            }
        }  )
        .respond(200, tagSelectionData.review2);
        $httpBackend.whenPUT( buildUrl('tags'), {
            'options':  {
                'upsert': true
            },
            'query':    {
                '+$and': [
                    {
                        'type': {
                            '+$ne': 'company-generated'
                        }
                    },
                    {
                        'type': {
                            '+$ne': 'ana-generated'
                        }
                    }
                ],
                'domain': 'male grooming',
                'languages': {
                    '+$in': ["en"]
                },
                'name': 'blades are sharp'
            },
            'update':   {
                '+$set': {
                    name: 'blades are sharp',
                    type: 'user-generated',
                    domain: 'male grooming',
                    syn: 'blades are sharp',
                    uid: 'Ergonomics',
                    category: 'Overall Performance'
                },
                '+$push': {
                    languages: 'en'
                }
            }
        }).respond(200, {
            name: 'blades are sharp',
            type: 'user-generated',
            domain: 'male grooming',
            syn: 'blades are sharp',
            uid: 'Ergonomics',
            category: 'Overall Performance',
            languages: ['en']
        });

        isolatedScope.addFeature2Sentiment(category, uid, sentiment, synonym); 
        
        $httpBackend.flush();

        isolatedScope.review.feature2sentiment.forEach(function(el) {
            delete el.$$hashKey;
        });
        
        console.log(isolatedScope.review);
        console.log(tagSelectionData.review2);
        expect(isolatedScope.review).to.be.deep.eql(tagSelectionData.review2);


    });


});