'use strict';

var faker = require('faker');
var moment = require('moment');

describe('Flow-Test - edit a Presentation', function() {
    var presentation_list = element.all(by.repeater('presentation in presentations'));
    var init_count = 0;

    var presentation_name = faker.lorem.words();

    var future_date = faker.date.future();
    var date_from = moment(future_date);
    var date_to = moment(future_date).add(1, 'hours');

    //edit-data
    var edit_presentation_name = faker.lorem.words();

    var edit_future_date = faker.date.future();
    var edit_date_from = moment(edit_future_date);
    var edit_date_to = moment(edit_future_date).add(1, 'hours');


    beforeAll(function() {
        browser.get(browser.baseUrl);
        browser.driver.manage().window().maximize();
        //workarround: use of websockets blocking angular-syncronization
        browser.ignoreSynchronization = true;
        browser.sleep(1000);
        presentation_list.count().then(function(count) {
            init_count = count;
        });
    });

    it('should create a new presentation', function() {
        element(by.linkText('Add')).click();
        expect(browser.getCurrentUrl()).toBe(browser.baseUrl + "/edit");
        element(by.model('presentation.title')).sendKeys(presentation_name);

        element.all(by.model('presentation.from')).get(0).clear().sendKeys(date_from.format('DD.MM.YYYY'));
        element.all(by.model('presentation.from')).get(1).clear().sendKeys(date_from.format('HH:mm'));
        //workaround date+time picker
        browser.executeScript("angular.element($('#form')).scope().presentation.from = new Date('" + date_from.toDate() + "')");

        element.all(by.model('presentation.to')).get(0).clear().sendKeys(date_to.format('DD.MM.YYYY'));
        element.all(by.model('presentation.to')).get(1).clear().sendKeys(date_to.format('HH:mm'));
        //workaround date+time picker
        browser.executeScript("angular.element($('#form')).scope().presentation.to = new Date('" + date_to.toDate() + "')");

        element(by.model('presentation.template')).sendKeys("Title and List");
        element(by.buttonText('Save')).click();
        browser.sleep(1000);
    });

    it('should show new presentation in the list', function() {
        expect(browser.getCurrentUrl()).toBe(browser.baseUrl);
        expect(presentation_list.count()).toBe(init_count + 1);
        presentation_list.last(function(row) {
            expect(row.element(by.css('td:nth-child(2)')).getText()).toBe(presentation_name);
            expect(row.element(by.css('td:nth-child(3)')).getText()).toBe(date_from.format('DD.MM.YYYY - HH:mm'));
            expect(row.element(by.css('td:nth-child(4)')).getText()).toBe(date_to.format('DD.MM.YYYY - HH:mm'));
        });
    });

    it('should open the new presentation in the form', function() {
        var last_row = presentation_list.last();
        expect(last_row.element(by.linkText('Edit')).isEnabled()).toBe(true);
        last_row.element(by.linkText('Edit')).click();

        browser.sleep(1000);

        expect(browser.getCurrentUrl()).toMatch(/\/[\w]+\/edit/);
    });

    it('should show the correct values in the fields', function() {
        expect(element(by.model('presentation.title')).getAttribute('value')).toBe(presentation_name);

        //from Date
        expect(element.all(by.model('presentation.from')).get(0).getAttribute('value')).toBe(date_from.format("DD.MM.YYYY"));
        //from Time
        expect(element.all(by.model('presentation.from')).get(1).getAttribute('value')).toBe(date_from.format("HH:mm"));

        //to Date
        expect(element.all(by.model('presentation.to')).get(0).getAttribute('value')).toBe(date_to.format("DD.MM.YYYY"));
        //to Time
        expect(element.all(by.model('presentation.to')).get(1).getAttribute('value')).toBe(date_to.format("HH:mm"));

        expect(element(by.model('presentation.template')).$('option:checked').getText()).toBe("Title and List");
    });

    it('should change the data in the fields and save the changes', function() {
        element(by.model('presentation.title')).sendKeys(edit_presentation_name);

        element.all(by.model('presentation.from')).get(0).clear().sendKeys(edit_date_from.format('DD.MM.YYYY'));
        element.all(by.model('presentation.from')).get(1).clear().sendKeys(edit_date_from.format('HH:mm'));
        //workaround date+time picker
        browser.executeScript("angular.element($('#form')).scope().presentation.from = new Date('" + edit_date_from.toDate() + "')");

        element.all(by.model('presentation.to')).get(0).clear().sendKeys(edit_date_to.format('DD.MM.YYYY'));
        element.all(by.model('presentation.to')).get(1).clear().sendKeys(edit_date_to.format('HH:mm'));
        //workaround date+time picker
        browser.executeScript("angular.element($('#form')).scope().presentation.to = new Date('" + edit_date_to.toDate() + "')");

        element(by.model('presentation.template')).sendKeys("Video: Title and List");

        expect(element(by.buttonText('Save')).isEnabled()).toBe(true);
        element(by.buttonText('Save')).click();
        browser.sleep(1000);
    });

    it("should show the changed data in the list", function() {
        expect(browser.getCurrentUrl()).toBe(browser.baseUrl);
        expect(presentation_list.count()).toBe(init_count + 1);
        presentation_list.last(function(row) {
            expect(row.element(by.css('td:nth-child(2)')).getText()).toBe(edit_presentation_name);
            expect(row.element(by.css('td:nth-child(3)')).getText()).toBe(edit_date_from.format('DD.MM.YYYY - HH:mm'));
            expect(row.element(by.css('td:nth-child(4)')).getText()).toBe(edit_date_to.format('DD.MM.YYYY - HH:mm'));
        });
    });

    it('should remove the last item in the list', function() {
        var last_row = presentation_list.last();
        expect(last_row.element(by.linkText('Remove')).isEnabled()).toBe(true);
        last_row.element(by.linkText('Remove')).click();

        browser.sleep(1000);

        expect(presentation_list.count()).toBe(init_count);
    });
});