/*
User preferences via local storage

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


if (!process.env.BROWSER_ENV) {
  if (typeof localStorage === 'undefined' || localStorage === null) {
    const LocalStorage = require('node-localstorage').LocalStorage;
    // TODO: Change out mechanism to use other persistance setup.
    const FileSystem = require('fs');
    const UUID = require('node-uuid');
    let tempDirPath = '/tmp/rookeries';
    try {
      FileSystem.statSync(tempDirPath).isDirectory();
    } catch (e) {
      FileSystem.mkdirSync(tempDirPath);
    }
    global.localStorage = new LocalStorage(`${tempDirPath}/scratch-${UUID.v4()}`);
  }
}


export class UserInfo {
  static getInitialInfo() {
    return {
      theme: 'evening',
      editor_theme: 'monokai',
      full_name: 'Stranger',
      auth_token: ''
    }
  }

  static getUserInfo(info) {
    // console.log(`Getting user info '${info}'`);
    let storageKey = `user_${info}`;
    let infoItem = localStorage.getItem(storageKey);
    if (infoItem === undefined) {
      infoItem = UserInfo.getInitialInfo()[info];
    }

    return infoItem;
  }

  static setUserInfo(info, value) {
    // console.log(`Setting user info '${info}' to '${value}'`);
    localStorage.setItem(`user_${info}`, value);
  }

  static removeUserInfo(info) {
    // console.log(`Clearing user info '${info}'`);
    localStorage.removeItem(`user_${info}`);
  }
}
