import Func from '@/Engine/ECS/Function';
import SysPage from '@/Frameworks/SysPage';
import { STORAGE_TYPE, LocalService } from '@/Frameworks/SysStorage';
import { PAGE_KEY } from '@/Project/constants';
import { FuncMainState } from '../FuncMain';

export enum FuncLoginState {
  idle,
}

export default class FuncLogin extends Func<FuncLoginState> {
  constructor(name: string) {
    super(name);
    super.initStates((sm) => {
      sm.addState(
        FuncLoginState.idle,
        this.onIdleStateIn,
        null,
        this.onIdleStateExit,
      );
    });
  }
  onIdleStateIn(): void {
    const login = (token: string) => {
      LocalService.setItem(STORAGE_TYPE.token, token);
      window.GT_APP.funcMain.changeState(FuncMainState.PlatformMenu);
    };
    SysPage.add(PAGE_KEY.Login, {
      login,
    });
  }
  onIdleStateExit(): void {
    SysPage.removeByKey(PAGE_KEY.Login);
  }
  login(token: string) {
    LocalService.setItem(STORAGE_TYPE.token, token);
    window.GT_APP.funcMain.changeState(FuncMainState.PlatformMenu);
  }
}
