#include common.js

Umi.UI.Title = (function() {
    let activeFlash = null;

    const setTitle = function(text) {
        document.title = text;
    };

    const clearTitle = function() {
        if(activeFlash !== null) {
            clearInterval(activeFlash);
            activeFlash = null;
            setTitle(futami.get('title'));
        }
    };

    const flashTitle = function(titles, interval, repeat) {
        if(interval === undefined) interval = 500;
        if(repeat === undefined) repeat = 5;

        let round = 0;
        const target = titles.length * repeat;

        clearTitle();
        setTitle(titles[0]);

        activeFlash = setInterval(function() {
            if(round >= target) {
                clearTitle();
                setTitle(futami.get('title'));
                return;
            }

            ++round;
            setTitle(titles[round % titles.length]);
        }, interval);
    };

    return {
        Set: setTitle,
        Clear: clearTitle,
        Flash: flashTitle,
    };
})();
