import {Injectable} from '@angular/core';
import {
  SCAuthorizationProviderType,
  SCUserConfiguration,
  userMapping,
} from '../profile/user';
import {IPAIAAuthAction} from './paia/paia-auth-action';
import {AuthActions, IAuthAction} from 'ionic-appauth';
import {TranslateService} from '@ngx-translate/core';
import {JSONFile} from '@angular/cli/utilities/json-file';
import {JSONPath} from 'jsonpath-plus';

@Injectable({
  providedIn: 'root',
})
export class AuthHelperService {
  constructor(private translateService: TranslateService) {}

  public getAuthMessage(
    provider: SCAuthorizationProviderType,
    action: IAuthAction | IPAIAAuthAction,
  ) {
    let message: string | undefined;
    switch (action.action) {
      case AuthActions.SignInSuccess:
        message = this.translateService.instant(
          `auth.messages.${provider}.logged_in_success`,
        );
        break;
      case AuthActions.SignOutSuccess:
        message = this.translateService.instant(
          `auth.messages.${provider}.logged_out_success`,
        );
        break;
    }
    return message;
  }

  getUserFromUserInfo(userInfo: JSONFile) {
    const user: SCUserConfiguration = {id: '', name: '', role: 'student'};
    for (const key in userMapping) {
      user[key as keyof SCUserConfiguration] = JSONPath({
        path: userMapping[key as keyof SCUserConfiguration] as string,
        json: userInfo,
      })[0];
    }

    return user;
  }
}
