suite('Bärneruhr', function () {
    'use strict';
    var assert = chai.assert;
    var $ = jQuery;
    var elem;

    setup(function () {
        elem = $('#u');
    });

    function cleanupCookies(id) {
        $.removeCookie('uhr-language' + id);
        $.removeCookie('uhr-mode' + id);
        $.removeCookie('uhr-status' + id);
        $.removeCookie('uhr-theme' + id);
    }

    teardown(function () {
        var uhr = elem.uhr('instance');
        if (uhr !== undefined) {
            cleanupCookies(uhr.id);
        }
        try {
            elem.uhr('destroy');
        } catch (e) {
            // just TRY to clean up, but don't DIE trying.
        }
        window.location.hash = '';
    });

    test('create and destroy widget', function () {
        var uhr = elem.uhr('instance');
        var id;
        assert.isUndefined(uhr);
        elem.uhr();
        uhr = elem.uhr('instance');
        assert.isNotNull(uhr);
        assert.isDefined(uhr);
        id = uhr.id;
        elem.uhr('destroy');
        uhr = elem.uhr('instance');
        assert.isUndefined(uhr);
        // cookies need to be cleaned up separately in this case, because in teardown(), the uhr widget doesn't exist anymore
        cleanupCookies(id);
    });

    test('default config', function () {
        var options;
        elem.uhr();
        options = elem.uhr('instance').options;
        assert.isTrue(options.autoresize);
        assert.isTrue(options.controls);
        assert.isUndefined(options.cookiePath);
        assert.isFalse(options.force);
        assert.equal(options.language, 'de_CH');
        assert.equal(options.mode, 'normal');
        assert.equal(options.status, 'on');
        assert.equal(options.theme, 'black');
    });

    test('custom config', function () {
        var options;
        var realWidth;
        var myWidth = 100;
        var myOptions = {
            autoresize: false,
            controls: false,
            cookiePath: '/foo/bar',
            force: true,
            language: 'de',
            mode: 'seconds',
            status: 'off',
            theme: 'red',
            width: myWidth + 'px'
        };
        elem.uhr(myOptions);
        options = elem.uhr('instance').options;
        assert.equal(options.autoresize, myOptions.autoresize);
        assert.equal(options.controls, myOptions.controls);
        assert.equal(options.cookiePath, myOptions.cookiePath);
        assert.equal(options.force, myOptions.force);
        assert.equal(options.language, myOptions.language);
        assert.equal(options.mode, myOptions.mode);
        assert.equal(options.status, myOptions.status);
        assert.equal(options.theme, myOptions.theme);
        assert.equal(options.width, myOptions.width);

        realWidth = elem.width();
        assert.equal(myWidth, realWidth);
    });

    test('unknown language', function () {
        var uhr;
        elem.uhr({
            language: 'klingon'
        });
        uhr = elem.uhr('instance');
        // NB: 'de' is just the first language that is included in the page. that may change!
        assert.equal(uhr.options.language, 'de_CH_genau');
    });

    test('unknown theme', function () {
        var uhr;
        elem.uhr({
            theme: 'klingon'
        });
        uhr = elem.uhr('instance');
        // NB: 'black' is the first theme that is included in the test page.
        assert.equal(uhr.options.theme, 'black');
    });

    test('URL params, short', function () {
        var options;

        window.location.hash = '#t=red&l=dk&m=seconds&s=on';

        elem.uhr();
        options = elem.uhr('instance').options;
        assert.isTrue(options.force);
        assert.equal(options.language, 'dk');
        assert.equal(options.mode, 'seconds');
        assert.equal(options.status, 'on');
        assert.equal(options.theme, 'red');
    });

    test('URL params, long', function () {
        var options;

        window.location.hash = '#theme=pink&language=de_CH&mode=normal&status=off';

        elem.uhr();
        options = elem.uhr('instance').options;
        assert.isTrue(options.force);
        assert.equal(options.language, 'de_CH');
        assert.equal(options.mode, 'normal');
        assert.equal(options.status, 'off');
        assert.equal(options.theme, 'pink');
    });
});