import {EMail, UserId, XToken} from '@caj/configs/types';
import {VersionType} from '@caj/helper/version';
import {MyUserAccount} from '@caj/user/types';
import {APP_VERSION} from './appNonSaveVar';

export enum ThemeMode {
  Darkest = 0,
  Dark = 1,
  Light = 2,
}

export function applyUpdateChanges(appVar: any): Promise<void> {
  return new Promise<void>(function (resolve, reject) {
    appVar.version += 1;

    console.log(appVar.version);

    function finish() {
      resolve();
    }

    switch (appVar.version) {
      case 3: {
        let appVarF = appVar as PREFERENCES_VARS;

        appVar = appVarF;
        finish();
        break;
      }

      case 2: {
        let appVarF = appVar;

        appVar = appVarF;
        finish();
        break;
      }

      default: {
        finish();
        break;
      }
    }
  });
}

//these variables may be changed by the user and will be saved in storage
export interface RegisterProcess {
  isRegistering: false | 'stepTwo' | 'stepFinal';
  XToken: XToken | undefined;
  EMail: EMail;
}

export interface PREFERENCES_VARS {
  dbek: number; //db encryption key
  version: VersionType;
  theme: ThemeMode;
  RegisterProcess: RegisterProcess;
  selectedAccount: UserId | 'none';
  accounts: {[key: UserId]: MyUserAccount};
}

export const preferences_vars_default: PREFERENCES_VARS = {
  dbek: -5,
  version: APP_VERSION, //version of datatypes in storage
  theme: ThemeMode.Dark,
  RegisterProcess: {
    isRegistering: false,
    XToken: undefined,
    EMail: '',
  },
  selectedAccount: 'none',
  accounts: {},
};
