'use strict';
var utils = require('../../lib/utils'),
User = require('./review-title-sku.user');


fdescribe('AdminApp - Review title SKU', function() {

  var browser1, browser2, element2;

  var user1, user2;
  var user1Waiting, user2Waiting;
  var helper;



  beforeAll(function() {
    browser1 = browser;
    browser2 = browser1.forkNewDriverInstance();
    return utils.login(null, null, browser2);
  });

  beforeEach(function() {
    helper = require('../../lib/css-selector-builder');
    user1 = new User({ browser: browser1 });
    user2 = new User({ browser: browser2 });

    element2 = browser2.element;

    user1Waiting = 5 * 1000;
    user2Waiting = 5 * 1000;
  });


beforeEach(function() {
    user1.gotToReviewTitleSku('5415e36606199ce8296741cc');
    user2.gotToReviewTitleSku('5415e36606199ce8296741cc');
});

afterAll(function() {
    return browser2.quit();
});

    describe('When a user adds a new sku...', function() {

        var skusCounter;

        beforeEach(function() {
            var skus = element.all(by.repeater('sku in $ctrl.product.skus | filter: $ctrl.filterParam '));
            skus.count().then(function(count) {
                skusCounter = count;
            });

            var skuToAddInput = user1.browser.findElement(protractor.By.model('$ctrl.skuToAdd'));
            skuToAddInput.sendKeys('testSKU');
            user1.clickByCssSelector('.manage-skus-top > form > div.form-group.add-sku > button');
            browser.sleep(user1Waiting);
        });

        it('the new sku should be added to the skus talbe', function() {
            var skus = element.all(by.repeater('sku in $ctrl.product.skus | filter: $ctrl.filterParam '));
            expect(skus.count()).toEqual(skusCounter+1);
        });

        it('the new sku should be added to the skus talbe of another user', function() {
            var skus = element2.all(by.repeater('sku in $ctrl.product.skus | filter: $ctrl.filterParam '));
            expect(skus.count()).toEqual(skusCounter+1);
        });

    })

    describe('When a user clicks on a select to attach an existing SKU to a title...', function() {
        
        var lastRow, cell, input, skusCounter, container;
        
        beforeEach(function() {
            var skus = element.all(by.repeater('sku in $ctrl.product.skus | filter: $ctrl.filterParam '));
            skus.count().then(function(count) {
                skusCounter = count;
            });
            
            var reviewTitleToAdd = user1.browser.findElement(protractor.By.model('$ctrl.reviewTitleToAdd'));
            reviewTitleToAdd.sendKeys('test review title');
            user1.clickByCssSelector('.manage-review-top > form > div.form-group.add-review > button')
            .then(() => {
                lastRow = element.all(by.repeater("pair in $ctrl.product.reviewTitlesInfo | orderBy:'title' | filter:{ title: $ctrl.filterParam }")).last();
                cell = lastRow.element(by.css('.col-sku'));
                container  = lastRow.element(by.css('.col-sku .ui-select-container'));
                input = new utils.UiSelect(cell.element(by.css('.col-sku .ui-select-container')));
                // input.sendKeys('a'); 
                return input.performClick();
            });
        });

        it('should display available skus', function() {
                var skus = element.all(by.repeater('sku in $ctrl.product.skus | filter: $ctrl.filterParam '));
                input.getChoicesCount().then((count) => {
                    expect(count).toEqual(skusCounter);
                });
        });

        it('the remove sku button of the review title should not be active', function() {
            expect(cell.element(by.buttonText("Remove")).getAttribute('class')).toMatch('not-active');
        });

        describe('When a user attaches an existing sku to a review title...', function() {

            beforeEach(function() {
                input.pickChoice(0);
                browser.sleep(user1Waiting);
            });

            it('the review title sku select should be hidden', function() {
                expect(container.getAttribute('class')).toMatch('ng-hide');
            });   

            it('the review title sku select should be visible', function() {
                expect(cell.element(by.css('.sku')).getAttribute('class')).not.toMatch('ng-hide');
            });    

            it('the remove sku button of the review title should be active', function() {
                expect(cell.element(by.buttonText("Remove")).getAttribute('class')).not.toMatch('not-active');
            });

        });


    });

    describe('When a user attaches a new sku to a review title...', function() {
                
        var lastRow, cell, input, container;

        describe('When a user write a non match query inside the search sku select...', function() {

            beforeEach(function() {
               
                var reviewTitleToAdd = user1.browser.findElement(protractor.By.model('$ctrl.reviewTitleToAdd'));
                reviewTitleToAdd.sendKeys('test review title');
                user1.clickByCssSelector('.manage-review-top > form > div.form-group.add-review > button')
                .then(() => {
                    lastRow = element.all(by.repeater("pair in $ctrl.product.reviewTitlesInfo | orderBy:'title' | filter:{ title: $ctrl.filterParam }")).last();
                    cell = lastRow.element(by.css('.col-sku'));
                    container  = lastRow.element(by.css('.col-sku .ui-select-container'));
                    input = new utils.UiSelect(cell.element(by.css('.col-sku .ui-select-container')));
                    return input.sendKeys("xyz");
                });
            });

            it('there should be only one item in the select', function() {
                input.getChoicesCount().then((count) => {
                    expect(count).toEqual(1);
                });
            });

            describe('When a user clicks on the non match query inside the search sku select...', function() {
                
                beforeEach(function() {
                    input.pickChoice(0);
                });

                it('the add button should become available', function() {
                    expect(cell.element(by.buttonText("Add")).getAttribute('class')).not.toMatch('not-active');
                });


                describe('When a user clicks on the add button...', function() {
                    
                    var skusCounter;

                    beforeEach(function() {
                        var skus = element.all(by.repeater('sku in $ctrl.product.skus | filter: $ctrl.filterParam '));
                        skus.count().then(function(count) {
                            skusCounter = count;
                        });
                        cell.element(by.buttonText("Add")).click();
                    });

                    it('the review title sku select should be hidden', function() {
                        browser.sleep(user1Waiting);
                        expect(container.getAttribute('class')).toMatch('ng-hide');
                    });   

                    it('the review title sku select should be visible', function() {
                        browser.sleep(user1Waiting);
                        expect(cell.element(by.css('.sku')).getAttribute('class')).not.toMatch('ng-hide');
                    });    

                    it('the remove sku button of the review title should be active', function() {
                        browser.sleep(user1Waiting);
                        expect(cell.element(by.buttonText("Remove")).getAttribute('class')).not.toMatch('not-active');
                    });

                    it('the remove sku button of the review title should be active', function() {
                        browser.sleep(user1Waiting);
                        expect(cell.element(by.buttonText("Remove")).getAttribute('class')).not.toMatch('not-active');
                    });

                    it('the new sku should be added to the skus talbe', function() {
                        browser.sleep(user1Waiting);
                        var skus = element.all(by.repeater('sku in $ctrl.product.skus | filter: $ctrl.filterParam '));
                        expect(skus.count()).toEqual(skusCounter+1);
                    });
                });

            });

        });

    });

    describe('When a user unlink a review title from a product...', function() {
        var reviewTitlesCounter, reviewTitles, lastRow, cell;
        beforeEach(function() {

            //count review titles
            reviewTitles = element.all(by.repeater("pair in $ctrl.product.reviewTitlesInfo | orderBy:'title' | filter:{ title: $ctrl.filterParam }"));
            reviewTitles.count().then(function(count) {
                reviewTitlesCounter = count;
            });
            //click remove button on last reviewtitle
            lastRow = element.all(by.repeater("pair in $ctrl.product.reviewTitlesInfo | orderBy:'title' | filter:{ title: $ctrl.filterParam }")).last();
            cell = lastRow.element(by.css('.col-linked-prod'));
            cell.element(by.buttonText("Remove")).click();
            browser.sleep(user1Waiting);
        });

        it('the unlinked reviewtitle shound now be not displayed in the table', function() {
            expect(reviewTitles.count()).toEqual(reviewTitlesCounter-1);
        });

    });


    describe('The user adds a new review title for a product...', function() {
        
        var reviewTitlesCounter, reviewTitles, lastRow, cell, input, container;
        var reviewTitleContent = 'zzzz test review title';
        
        beforeEach(function() {
            //cont reviewtitle
            reviewTitles = element.all(by.repeater("pair in $ctrl.product.reviewTitlesInfo | orderBy:'title' | filter:{ title: $ctrl.filterParam }"));
            reviewTitles.count()
                .then(function(count) {
                    reviewTitlesCounter = count;
                });
            //add reviewtitle
            var reviewTitleToAdd = user1.browser.findElement(protractor.By.model('$ctrl.reviewTitleToAdd'));
            reviewTitleToAdd.sendKeys(reviewTitleContent);
            user1.clickByCssSelector('.manage-review-top > form > div.form-group.add-review > button');
            browser.sleep(user1Waiting);
        });

        it('the added reviewtitle shound now be displayed in the table', function() {
            expect(reviewTitles.count()).toEqual(reviewTitlesCounter+1);
        });

        describe('The user wants to move that review title from productA to productB...', function() {
            //productB id: 5415e31506199ce8296741cb
            beforeEach(function() {
                lastRow = element.all(by.repeater("pair in $ctrl.product.reviewTitlesInfo | orderBy:'title' | filter:{ title: $ctrl.filterParam }")).last();
                cell = lastRow.element(by.css('.col-move-prod'));
                container  = lastRow.element(by.css('.col-move-prod .ui-select-container'));
                input = new utils.UiSelect(cell.element(by.css('.col-move-prod .ui-select-container')));

            });

            it('move button should not be clickable...', function() {
                expect(cell.element(by.buttonText("Move")).getAttribute('class')).toMatch('not-active');
            });

            describe('he types the product name in the select and pick it...', function() {

                beforeEach(function() {
                    input.sendKeys("AT890");
                    browser.sleep(user1Waiting);
                    input.pickChoice(0);
                });
                it('move button should become clickable...', function() {
                    expect(cell.element(by.buttonText("Move")).getAttribute('class')).not.toMatch('not-active');
                });

                describe('he clicks on the move button...', function() {

                    beforeEach(function() {
                        cell.element(by.buttonText("Move")).click();
                    });
                    it('the moved reviewtitle shound not be displayed in the table of productA', function() {
                        browser.sleep(user1Waiting);
                        expect(reviewTitles.count()).toEqual(reviewTitlesCounter);
                    });

                    describe('he navigates to the ProductB', function() {

                        beforeEach(function() {
                            user1.gotToReviewTitleSku('5415e31506199ce8296741cb');
                        });
                        it('the moved reviewtitle shound not be displayed in the table of productB', function() {
                            reviewTitles = element.all(by.repeater("pair in $ctrl.product.reviewTitlesInfo | orderBy:'title' | filter:{ title: $ctrl.filterParam }"));
                            lastRow = element.all(by.repeater("pair in $ctrl.product.reviewTitlesInfo | orderBy:'title' | filter:{ title: $ctrl.filterParam }")).last();
                            cell = lastRow.element(by.css('.col-review-title'));
                            browser.sleep(user1Waiting);
                            cell.getText().then(function(text){
                                expect(text).toBe(reviewTitleContent);
                            });

                        });
                    });
                });
            });
        });
    });
});