'use strict';
/* globals  describe, beforeEach, it, expect, browser, beforeAll,afterAll */


var utils = require('../../lib/utils'),
    User = require('./insights-list.user');

describe('Insights List', function() {
    describe('Insights List - Call center data', function() {

        var browser1;
        var user1;
        var user1Waiting,
            user2Waiting;



        beforeEach(function() {
            browser1 = browser;
            user1Waiting = 5;
            user2Waiting = 5;
            user1 = new User({
                browser: browser1
            });
        });
        beforeEach(function() {
            return user1.goTo('/insights/5643407bcf53f30100411fcc')
                .then(function() {
                    // TODO: broken according to https://github.com/angular/protractor/issues/3761#issuecomment-262393110
                    // return user1.waitForVisibleElement($('.brand-name'), 3000,'Waiting for .brand-name');
                    return browser.sleep(3 * 1000);
                });
        });



        describe('Page Load', function() {
            it('when user access URL "/insights/5643407bcf53f30100411fcc", then the right insights are visualized and the right product data and for each insight the right number of linked reviews is shown', function() {
                return user1.readNumberOfLinkedCallCenterData()
                    .then(function(num) {
                        expect(num).toEqual(['0 - CC', '0 - CC', '1 - CC', '1 - CC', '5 - CC']);

                        return user1.readAuthorOfInsightByIndex(0);
                    })
                    .then(function(authorOfInsight) {
                        expect(authorOfInsight).toBe('username@wonderflow.co');
                    });
            });
        });

        describe('Access linked call center data', function() {
            it('when user clicks on the 5th linked call center data, then the url is "" and reviews are 1 and title of th review is "Bright way to limit hair regrowth" ', function() {
                return user1.clickOnLinkedCallCenterDataByIndexOfInsightInTheList(4)
                    .then(function() {
                        return user1.wait(2);
                    })
                    .then(function() {
                        return browser.getCurrentUrl();
                    })
                    .then(function(url) {
                        expect(url).toContain('/linked/5643407bcf53f30100411fcc/2016-01-14T09:05:41.473Z/callCenterDataIds');
                    })
                    .then(function() {
                        return user1.readNumberOfLinkedReviews();
                    })
                    .then(function(numberOfLinkedReviews) {
                        expect(numberOfLinkedReviews).toBe('0');
                        // TODO: here i should add 
                    })
                    .then(function() {
                        return user1.countNumberOfVisualizedLinkedReviews();
                    })
                    .then(function(numberOfReviews) {
                        expect(numberOfReviews).toBe(1);
                    })
                    .then(function() {
                        return user1.readTitleOfFirstLinkedReview();
                    })
                    .then(function(titleOfFirstLinkedCallCenterData) {
                        expect(titleOfFirstLinkedCallCenterData).toBe('Bright way to limit hair regrowth');
                    });

            });
        });

        describe('Merge 2 insights', function() {
            it('when user opens merge mode, select first 2 insights, clicks on merge button, specify the text of new insight and then press ok, then the selected insights are remove and a new insights appears with consistent data', function() {
                return user1.enableMergeMode().then(function() {
                        return user1.selectInsightToMergeByIndex(0);
                    })
                    .then(function() {
                        return user1.selectInsightToMergeByIndex(4);
                    })
                    .then(function() {
                        return user1.clickOnMergeButton();
                    })
                    .then(function() {
                        return browser.sleep(4 * 1000);
                    })
                    .then(function() {
                        return user1.readInsightsToMergeOnModalWindow();
                    })
                    .then(function(insightstoMergeOnModalWindow) {
                        expect(insightstoMergeOnModalWindow.length).toBe(2);
                        expect(insightstoMergeOnModalWindow).toContain('Customers would recommend it to others.');
                        expect(insightstoMergeOnModalWindow).toContain('Customer bought the device online and is confused on how to register for the warranty.');
                        return user1.setTextOfNewInsight('ola');
                    })
                    .then(function() {
                        return user1.clickToApplyMerge();
                    })
                    .then(function() {
                        return browser.sleep(5 * 1000);
                    })
                    .then(function() {
                        return user1.countNumberOfInsights();
                    })
                    .then(function(numberOfInsightsAfterMerge) {
                        expect(numberOfInsightsAfterMerge).toBe(4);
                        return user1.readTextOfFirstInsight();
                    })
                    .then(function(textOfFirstInsight) {
                        expect(textOfFirstInsight).toBe('ola');
                        return user1.readNumberOfLinkedCallCenterData();
                    })
                    .then(function(numberOfLinkedCallCenterData) {
                        expect(numberOfLinkedCallCenterData).toEqual(['5 - CC', '0 - CC', '1 - CC', '1 - CC']);
                        return user1.readNumbersOfLinkedReviews();
                    })
                    .then(function(numberOfLinkedReviews) {
                        expect(numberOfLinkedReviews).toEqual(['25 - R&R', '5 - R&R', '0 - R&R', '0 - R&R']);

                        return user1.readAuthorOfInsightByIndex(0);

                    })
                    .then(function(authorOfInsight) {
                        expect(authorOfInsight).toBe('wisse@wonderflow.co');
                    });
            });
        });



    });

    describe('Insights List - R&R', function() {
        var browser1;
        var user1;
        var user1Waiting,
            user2Waiting;



        beforeEach(function() {
            browser1 = browser;
            user1Waiting = 5;
            user2Waiting = 5;
            user1 = new User({
                browser: browser1
            });
        });
        beforeEach(function() {
            return user1.goTo('/insights/5441319acb96f72923560242')
                .then(function() {
                    return browser.sleep(1 * 1000);
                    // TODO: broken according to https://github.com/angular/protractor/issues/3761#issuecomment-262393110
                    // return user1.waitForVisibleElement($('.brand-name'), 3000);
                });
        });


        describe('Page Load', function() {
            it('when user access URL "/insights/5441319acb96f72923560242", then the right insights are visualized and the right product data and for each insight the right number of linked reviews is shown', function() {

                user1.readBrandName()
                    .then(function(brandName) {
                        expect(brandName).toBe('BRAUN');
                    })
                    .then(function() {
                        return user1.readProductName();
                    })
                    .then(function(v) {
                        expect(v).toBe('9 - CS');
                        return user1.readNumberOfPublishedInsights();
                    })
                    .then(function(v) {
                        expect(v).toBe('10');
                    })
                    .then(function() {
                        return user1.countNumberOfInsights();
                    })
                    .then(function(v) {
                        expect(v).toBe(11);
                        return user1.readFirst2Insights();
                    })
                    .then(function(v) {
                        expect(v).toEqual(['Customers would recommend it to others.', 'High quality cleaning system.']);
                        return user1.readNumberOfLinkedReviewsOfFirstInsight();
                    })
                    .then(function(v) {
                        expect(v).toBe('20 - R&R');
                    });
            });

        });



        xdescribe('Edit text of an insight', function() {
            var user2;
            var browser2;

            beforeAll(function() {
                browser2 = browser.forkNewDriverInstance();

                console.log('Maximize window to', 1280, 800);

                return browser2.driver.manage().window().setSize(1280, 800)
                    .then(function() {
                        return utils.login(null, null, browser2)

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

            beforeEach(function() {
                // browser2 = browser1.forkNewDriverInstance();
                user2 = new User({
                    browser: browser2
                });
                // utils.login(null, null, browser2);
                // browser2.sleep(2 * 1000);
                return user2.goTo('/insights/5441319acb96f72923560242')
                    .then(function() {
                        return browser.sleep(2 * 1000);
                    });
            });


            it('when user updates text of an insight, and clicks on save button, then the insights is updated, and a concurrent user is notified', function() {

                expect(user1.readTextOfFirstInsight()).toBe('Customers would recommend it to others.');

                return user1.setTextOfFirstInsight('ola').then(function() {
                        return user1.clickToSaveFirstInsight();
                    })
                    .then(function() {
                        return browser.sleep(user1Waiting * 1000);
                    })
                    .then(function() {
                        return user1.takeScreenshot('no-selector');
                    })
                    .then(function() {
                        expect(user1.readTextOfFirstInsight()).toBe('Customers would recommend it to others.ola');
                        return browser2.sleep(user2Waiting * 1000);
                    })
                    .then(function() {
                        expect(user2.readTextOfFirstInsight()).toBe('Customers would recommend it to others.ola');
                    });

            });
        }).pend('IT GOES IN TIMEOUT, BUT ITSELF IT DOES NOT FAIL. AN ISSUE HAS BEEN OPENED ON https://github.com/angular/protractor/issues/3761');

        describe('Link to linked reviews page', function() {
            it('when user clicks on link to access linked-reviews, then the url contains "/linked-reviews/5441319acb96f72923560242/2015-07-23T15:38:17.599Z"', function() {
                var url;
                return user1.clickOnLinkedReviewsOfFirstInsight()
                    .then(function() {
                        return browser.getCurrentUrl();
                    })
                    .then(function(url) {
                        expect(url).toContain('/linked/5441319acb96f72923560242/2015-07-23T15:38:17.599Z/reviewIds');
                    });
            });
        });

        //
        describe('Merge Mode', function() {
            it('when user enables merge mode, and he can select insights, and clicks on merge, then a modal window appears, containing the list of insights to merge', function() {
                return user1.enableMergeMode().then(function() {
                    return user1.selectFirst2Insights();
                })
                .then(function() {
                    return user1.clickOnMergeButton();
                })
                .then(function() {
                    return browser.sleep(4 * 1000);
                })
                .then(function() {
                    return user1.readInsightsToMergeOnModalWindow();
                })
                .then(function(insightstoMergeOnModalWindow) {
                    expect(insightstoMergeOnModalWindow.length).toBe(2);
                    expect(insightstoMergeOnModalWindow).toContain('Customers would recommend it to others.');
                    expect(insightstoMergeOnModalWindow).toContain('High quality cleaning system.');
                });
            });


            it('when user merges 2 insight, and specify the new text, and click on merge, then the 2 insights are removed, and a new one appears including the sum of linked reviews coming from the merged ones, and another user is notified of it', function() {
                return user1.enableMergeMode().then(function() {
                        return user1.selectFirst2Insights();
                    }).then(function() {
                        return user1.clickOnMergeButton();
                    }).then(function() {
                        return user1.setTextOfNewInsight('text of new insight');
                    }).then(function() {
                        return user1.clickToApplyMerge();
                    }).then(function() {
                        return browser.sleep(4 * 1000);
                    }).then(function() {
                        return user1.readFirstTwoInsights();
                    }).then(function(first2Insights) {
                        expect(first2Insights).toContain('text of new insight');
                        expect(first2Insights).toContain('Customers feel like you need to get used to the new razor but its worth it.');

                        return user1.readAuthorOfInsightByIndex(0);
                    })
                    .then(function(authorOfInsight) {
                        expect(authorOfInsight).toBe('wisse@wonderflow.co');
                    })
                    .then(function() {
                        return user1.readNumbersOfLinkedReviews();
                    })
                    .then(function(numberOfLinkedReviews) {
                        expect(numberOfLinkedReviews[0]).toBe('25 - R&R');
                    });
            });
        });
        describe('Publish/Unpublish of insights', function() {
            beforeEach(function() {
                return browser1.sleep(1 * 1000);
            });
            it('when user clicks to publish the first insight, then the insight gets published', function() {
                return user1.readStateOfFirstInsight().then(function(stateOfFirstInsight) {
                        expect(stateOfFirstInsight).toBe('PUBLISH');
                        return user1.readStateOfSecondInsight();
                    }).then(function(stateOfSecondInsight) {
                        expect(stateOfSecondInsight).toBe('HIDE');
                        return user1.clickToPublishFirstInsight();
                    })
                    .then(function() {
                        return browser.sleep(2 * 1000);
                    })
                    .then(function() {
                        return user1.readStateOfFirstInsight();
                    })
                    .then(function(stateOfFirstInsight) {
                        expect(stateOfFirstInsight).toBe('HIDE');
                        return user1.readStateOfSecondInsight();
                    }).then(function(stateOfSecondInsight) {
                        expect(stateOfSecondInsight).toBe('HIDE');
                    });
            });
            it('when user clicks to unpublish the first insight, then the insight gets unpublished', function() {
                return user1.readStateOfFirstInsight()
                    .then(function(v) {
                        expect(v).toBe('PUBLISH');
                        return user1.readStateOfSecondInsight()
                    })
                    .then(function(v) {
                        expect(v).toBe('HIDE');
                        return user1.clickToUnPublishSecondInsight();
                    })
                    .then(function(){
                        return browser.sleep(2*1000);
                    })
                    .then(function() {
                        return user1.readStateOfFirstInsight()
                    })
                    .then(function(v) {
                        expect(v).toBe('PUBLISH');
                        return user1.readStateOfSecondInsight();
                    })
                    .then(function(v) {
                        expect(v).toBe('PUBLISH');
                    });
            });
        });



    });
});