let currentStyle;
let loader;

let styleSwitchDelay = 200;

function useStyle(style) {
    style.use();
    setTimeout(function() {
        if (currentStyle) {
            currentStyle.unuse();
        }
        currentStyle = style;
    }, styleSwitchDelay);
}

function setSkinLoader(l) {
    loader = l;
}

function loadSkin(brand) {
    //TODO Catch skin load error and load default
    return new Promise(function(resolve, reject) {
        if (!loader) {
            throw new Error('Skin loader have not been configured yet, check startup sequence');
        }
        resolve(loader((brand || '').toString()));
    }).then(function(skin) {
        useStyle(skin);
        return skin;
    });
}

export {useStyle, setSkinLoader, loadSkin};
