/**
 * First task.
 *
 * User should be able to:
 *     - set task name
 *     - start time if task name was provided
 *     - pause/resume time
 *     - stop time
 */

casper.test
    .begin(
        'User should be able to see start button when type task name in an input field',
        2,
        function suite(test) {
            casper.start("http://localhost:9000/", function () {
                test.assertExists('form.description', "Task input field is found");
            });

            casper.then(function () {
                this.fill('form.description', {
                    'task-name': 'functional test'
                }, false);
            });

            casper.then(function () {
                this.wait(300, function () {
                    test.assertVisible('button.start', 'Start button is visible');
                });
            });

            casper.run(function () {
                test.done();
            });
        }
    );

casper.test
    .begin(
        'Hide Start button if user deleted the last letter from input field',
        2,
        function suite(test) {
            casper.start("http://localhost:9000/", function () {
                this.fill('form.description', {
                    'task-name': 'test'
                }, false);
            });

            casper.then(function () {
                this.wait(300, function () {
                     test.assertVisible('button.start', 'Start button is visible');
                });
            });

            casper.then(function () {
                this.fill('form.description', {
                    'task-name': ''
                }, false);
            });

            casper.then(function () {
                this.wait(300, function () {
                     test.assertNotVisible('button.start', 'Start button is hidden');
                });
            });

            casper.run(function () {
                test.done();
            });
        }
    );

casper.test
    .begin(
        'Application should start count time if user clicks start button',
        5,
        function suite(test) {
            casper.start("http://localhost:9000/", function () {
                this.fill('form.description', {
                    'task-name': 'functional test'
                }, false);
            });

            casper.then(function () {
                this.wait(300, function () {
                    this.click('button.start');
                });
            });

            casper.then(function () {
                this.wait(300, function () {
                    test.assertNotVisible('button.start', 'Start button is hidden');
                    test.assertVisible('button.pause', 'Pause button visible');
                    test.assertVisible('button.stop', 'Stop button visible');
                    test.assertVisible('.time-box', 'Time box visible');
                });
            });

            casper.then(function () {
                this.wait(1700, function () {
                    test.assertSelectorHasText('.time-box', '00:00:03', 'Time count started');
                });
            });

            casper.run(function () {
                test.done();
            });
        }
    );

casper.test
    .begin(
        'Disable/Enable stop button and pause/resume time count if user clicks on pause button',
        6,
        function suite(test) {
            casper.start("http://localhost:9000/", function () {
                this.fill('form.description', {
                    'task-name': 'functional test'
                }, false);
            });

            casper.then(function () {
                this.wait(300, function () {
                    this.click('button.start');
                });
            });

            casper.then(function () {
                this.wait(300, function () {
                    this.click('button.pause');
                });
            });

            casper.then(function () {
                this.wait(1700, function () {
                    test.assertExists('button.stop.disabled', "Stop button is disabled");
                    test.assertExists('button.pause.active', "Pause button is activated");
                    test.assertSelectorHasText('.time-box', '00:00:01', 'Time count paused');
                });
            });

            casper.then(function () {
                this.click('button.pause');
            });

            casper.then(function () {
                this.wait(300, function () {
                    test.assertNotExists('button.stop.disabled', "Stop button is enabled");
                    test.assertNotExists('button.pause.active', "Pause button is deactivated");
                    test.assertSelectorHasText('.time-box', '00:00:02', 'Time count resumed');
                });
            });

            casper.run(function () {
                test.done();
            });
        }
    );

casper.test
    .begin(
        'Disable/Enable stop button and pause/resume time count if user clicks on pause button',
        6,
        function suite(test) {
            casper.start("http://localhost:9000/", function () {
                this.fill('form.description', {
                    'task-name': 'functional test'
                }, false);
            });

            casper.then(function () {
                this.wait(300, function () {
                    this.click('button.start');
                });
            });

            casper.then(function () {
                this.wait(1700, function () {
                    this.click('button.stop');
                });
            });

            casper.then(function () {
                test.assertNotVisible('button.pause', 'Pause button is hidden');
                test.assertNotVisible('button.stop', 'Stop button is hidden');
                test.assertNotVisible('.time-box', 'Time box is hidden');
                test.assertVisible('button.report', 'Report button is visible');
                test.assertVisible('button.remove.report', 'Remove report button is visible');
                test.assertSelectorHasText('input', '', 'Input field clear');
            });

            casper.run(function () {
                /**
                 * Fix for a problem "Unsafe JavaScript attempt to access frame"
                 */
                setTimeout(function () {
                    test.done();
                    phantom.exit();
                }, 0);
            });
        }
    );