import { Action } from '@ngrx/store';

import { RESTClientError } from '@loafer/ng-rest/protocol';

import { DomainMember } from '@overflow/commons-typescript/model/domain';
import { Member } from '@overflow/commons-typescript/model/member';

export enum ActionType {
  Signin = '[member.auth] Signin',
  SigninSuccess = '[member.auth] SigninSuccess',
  SigninFailure = '[member.auth] SigninFailure',
  SigninRedirect = '[member.auth] SigninRedirect',

  SigninCookie = '[member.auth] SigninCookie',
  SigninCookieSuccess = '[member.auth] SigninCookieSuccess',
  SigninCookieFailure = '[member.auth] SigninCookieFailure',

  Signout = '[member.auth] Signout',
  SignoutSuccess = '[member.auth] SignoutSuccess',
  SignoutFailure = '[member.auth] SignoutFailure',
}

export class Signin implements Action {
  readonly type = ActionType.Signin;

  constructor(public payload: {email: string, password: string, returnURL: string}) {}
}

export class SigninSuccess implements Action {
  readonly type = ActionType.SigninSuccess;

  constructor(public payload: {authToken: string, domainMember: DomainMember}) {}
}

export class SigninFailure implements Action {
  readonly type = ActionType.SigninFailure;

  constructor(public payload: RESTClientError) {}
}

export class SigninRedirect implements Action {
  readonly type = ActionType.SigninRedirect;
  constructor(public payload: string) {}
}

export class SigninCookie implements Action {
  readonly type = ActionType.SigninCookie;

  constructor(public payload: {authToken: string, returnURL: string}) {}
}

export class SigninCookieSuccess implements Action {
  readonly type = ActionType.SigninCookieSuccess;

  constructor(public payload: DomainMember) {}
}

export class SigninCookieFailure implements Action {
  readonly type = ActionType.SigninCookieFailure;

  constructor(public payload: RESTClientError) {}
}

export class Signout implements Action {
  readonly type = ActionType.Signout;
}

export class SignoutSuccess implements Action {
  readonly type = ActionType.SignoutSuccess;
}

export class SignoutFailure implements Action {
  readonly type = ActionType.SignoutFailure;

  constructor(public payload: RESTClientError) {}
}

export type Actions =
  | Signin
  | SigninSuccess
  | SigninFailure
  | SigninRedirect
  | SigninCookie
  | SigninCookieSuccess
  | SigninCookieFailure
  | Signout
  | SignoutSuccess
  | SignoutFailure
;
