/*
Representation of a theme.

@copyright (c) Copyright 2013-2017 Dorian Pula
@license AGPL v3
@author Dorian Pula [dorian.pula@amber-penguin-software.ca]
*/

import {UserInfo} from './user_info';


class ThemeManager {
  static getRegisteredThemes() {
    return ['daytime', 'evening'];
  }

  static switchTheme() {
    let currentTheme = ThemeManager.getCurrentTheme();
    let nextTheme = ThemeManager.getNextTheme();

    let registeredThemes = ThemeManager.getRegisteredThemes();
    if (registeredThemes.indexOf(nextTheme) === -1) {
      console.error(`Selected theme '${nextTheme}' is not one of the known themes: '${registeredThemes}'`);
      nextTheme = registeredThemes[0];
    }

    if (currentTheme !== nextTheme) {
      console.log(`Adding '${nextTheme}' and removing '${currentTheme}'`);
      document.body.classList.remove(currentTheme);
      document.body.classList.add(nextTheme);
    }

    UserInfo.setUserInfo('theme', nextTheme);
    return {
      theme: nextTheme,
      alternativeTheme: ThemeManager.getNextTheme(),
      editorTheme: ThemeManager.getEditorTheme()
    };
  }

  static getCurrentTheme() {
    // TODO: Make function more friendly to server-side rendering via use of redux stores instead.
    return UserInfo.getUserInfo('theme') || (typeof document !== 'undefined' && document.body.classList[0]) || 'evening';
  }

  static getEditorTheme() {
    return ThemeManager.getCurrentTheme() === 'daytime' ? 'default' : 'monokai';
  }

  static getNextTheme() {
    return ThemeManager.getCurrentTheme() === 'daytime' ? 'evening' : 'daytime';
  }
}

export {ThemeManager};
