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


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



describe('Linked Reviews', function() {
    var browser1;
    var user1;
    var user1Waiting,
        user2Waiting;


    beforeEach(function() {
        user1 = new User({
            browser: browser1
        });
    });
    beforeEach(function() {
        browser1 = browser;
        user1Waiting = 5;
        user2Waiting = 5;
    });
    beforeEach(function() {

        return user1.goTo('/linked/5441319acb96f72923560242/2015-07-23T15:38:17.599Z/reviewIds')
            .then(function() {
                console.log('Waiting 5 seconds...');
                return browser1.sleep(5 * 1000);
            });
    });



    describe('Page Load', function() {
        it('when user accesses URL "/linked/?productId=5441319acb96f72923560242&createdAt=2015-07-23T15:38:17.599Z", then the right insight and right linkedReviews are shown', function() {
            console.log('Read Insight Text...');

            return user1.takeScreenshot('going to read insights text')
                .then(function() {
                    return user1.readInsightText()
                })
                .then(function(insightText) {
                    expect(insightText).toBe('Customers would recommend it to others.');
                    return user1.readNumberOfLinkedReviews()
                })
                .then(function(numberOfLinkedReviews) {
                    expect(numberOfLinkedReviews).toBe('20');
                    return user1.countNumberOfVisualizedLinkedReviews();
                })
                .then(function(numberOfVisualizedLinkedReviews) {
                    expect(numberOfVisualizedLinkedReviews).toBe(10);
                    return user1.readTitleOfFirstLinkedReview();
                })
                .then(function(titleOfFirstLinkedReview) {
                    expect(titleOfFirstLinkedReview).toBe('Braun Activator 8595 (8000\'er Serie ca.2002) vs. Series 9 9090cc 2015');

                })
        });
    });

    describe('Unlink of reviews', function() {
        beforeEach(function() {
            browser.sleep(2 * 1000);
        });


        xit('when user clicks to unlink the first insight, then the browser ask for a confirmation', function() {

            // utils.login(null, null, browser1);
            // browser1.get(utils.getEndPoint() + '/#/linked/?productId=5441319acb96f72923560242&createdAt=2015-07-23T15:38:17.599Z');
            // // // console.log('Waiting 5 secs.');
            // browser1.sleep(5 * 1000);
            // browser.waitForAngular();

            // utils.login(null, null, browser1);
            // browser1.get(utils.getEndPoint() + '/#/linked/?productId=5441319acb96f72923560242&createdAt=2015-07-23T15:38:17.599Z');
            // console.log('Waiting 5 secs.');
            // browser1.sleep(5 * 1000);
            // browser.waitForAngular();


            return user1.clickToUnlinkLinkedReview()
                .then(function() {
                    return browser1.switchTo().alert();
                })
                .then(function(confirmDialog) {
                    return confirmDialog.getText();
                }).then(function(confirmDialogText) {
                    expect(confirmDialogText).toBe('Are you sure to unlink this review from this insight?  \n\nOnce this review will be unlinked, there will be no way to recover it in case the unlinking was wrong.');
                    browser1.switchTo().alert().accept();
                });

        });
        it('when a user clicks to unlink the first review, and the user confirms, then the review is removed from the list', function() {


            return user1.clickToUnlinkLinkedReview()
                .then(function() {
                    return browser1.switchTo().alert();
                })
                .then(function(confirmDialog) {
                    confirmDialog.accept();
                    return browser.sleep(2 * 1000);
                })
                .then(function() {
                    return user1.countNumberOfVisualizedLinkedReviews();
                })
                .then(function(numberOfVisualizedLinkedReviews) {
                    expect(numberOfVisualizedLinkedReviews).toBe(10);
                    return user1.readTitleOfFirstLinkedReview();
                })
                .then(function(titleOfFirstLinkedReview) {
                    expect(titleOfFirstLinkedReview).toBe('Absolut empfehlenswert');
                });

        });
    });
    describe('Unlink of reviews - Concurrent', function() {
        var user2,
            browser2;

        beforeAll(function() {
            browser2 = browser.forkNewDriverInstance();
            return utils.login(null, null, browser2)
                .then(function() {
                    return browser2.sleep(2 * 1000);

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

        beforeEach(function() {
            // browser2 = browser1.forkNewDriverInstance();
            user2 = new User({
                browser: browser2
            });
            // utils.login(null, null, browser2);
            // browser2.sleep(2 * 1000);
            console.log('Going to linked reviews...');

            return browser2.sleep(2 * 1000)
                .then(function() {
                    return user2.goTo('/linked/5441319acb96f72923560242/2015-07-23T15:38:17.599Z/reviewIds')
                })
                .then(function() {
                    return browser2.sleep(2 * 1000);
                });
        });

        xit('when a user clicks to unlink the first review, and the user confirms, then also other users are notified', function() {

            return user1.readTitleOfFirstLinkedReview()
                .then(function(val) {
                    expect(val).toBe('Braun Activator 8595 (8000\'er Serie ca.2002) vs. Series 9 9090cc 2015');
                    return user1.clickToUnlinkLinkedReview();
                })
                .then(function() {
                    var confirmDialog;

                    confirmDialog = browser1.switchTo().alert();
                    return confirmDialog.accept();

                })
                .then(function() {
                    return browser.sleep(2 * 1000);
                })
                .then(function() {
                    return user1.countNumberOfVisualizedLinkedReviews();
                })
                .then(function(count) {
                    expect(count).toBe(10);
                    return browser.sleep(10 * 1000);
                })
                .then(function() {
                    return user1.readTitleOfFirstLinkedReview();

                })
                .then(function(text) {
                    expect(text).toBe('Absolut empfehlenswert');

                })
                .then(function() {
                    return user2.readTitleOfFirstLinkedReview();
                })
                .then(function(text) {
                    expect(text).toBe('Absolut empfehlenswert');
                });



        }).pend('When executed together with all other test, login on second browser goes in timeout waiting for reviews. But on 22/11/2016 alone it is working fine');
    });

    describe('Next Page', function() {
        it('when user clicks on next page, then the browser loads more reviews', function() {
            browser.sleep(5 * 1000);
            browser.waitForAngular();
            expect(user1.countNumberOfVisualizedLinkedReviews()).toBe(10);

            user1.clickOnNextPage();
            browser.sleep(5 * 1000);
            user1.countNumberOfVisualizedLinkedReviews().then(function(val) {
                expect(val).toBe(19);
            });
            // expect(user1.countNumberOfVisualizedLinkedReviews()).toBe(19);
        });
        it('when linked reviews are finished, then the next page button is not shown anymore', function() {
            browser.sleep(5 * 1000).then(function() {
                return user1.countNumberOfVisualizedLinkedReviews();
            }).then(function(numberOfVisualizedLinkedReviews) {
                expect(numberOfVisualizedLinkedReviews).toBe(10);
                return user1.clickOnNextPage();
            }).then(function() {
                expect(user1.isNextPageButtonDisplayed()).toBe(false);
            });
        });
    });



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

    // 	beforeEach(function() {
    // 		browser2 = browser1.forkNewDriverInstance();
    // 		user2 = new User({
    // 			browser: browser2
    // 		});
    // 		utils.login(null, null, browser2);
    // 		browser2.sleep(2 * 1000);
    // 		browser2.get(utils.getEndPoint() + '/#/insights-list?productId=5441319acb96f72923560242');
    // 	});
    // 	afterEach(function() {
    // 		browser2.quit();
    // 	});

    // 	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.');


    // 		// user1.setTextOfFirstInsight('ola');

    // 		// user1.clickToSaveFirstInsight();

    // 		// browser.sleep(user1Waiting * 1000);

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

    // 		// browser2.sleep(user2Waiting * 1000);

    // 		// expect(user2.readTextOfFirstInsight()).toBe('Customers would recommend it to others.ola');

    // 	});
    // });
    // fdescribe('Link to linked reviews page', function() {
    // 	it('when user clicks on link to access linked-reviews, then the url contains "/linked/?productId=5441319acb96f72923560242&createdAt=2015-07-23T15:38:17.599Z"', function() {
    // 		user1.clickOnLinkedReviewsOfFirstInsight();
    // 		expect(browser.executeScript('return window.location.hash;')).toContain('#/linked/?productId=5441319acb96f72923560242&createdAt=2015-07-23T15:38:17.599Z');
    // 	});
    // });



});