import { Injectable } from '@angular/core';
import { Router } from '@angular/router';

import { Effect, Actions, ofType } from '@ngrx/effects';
import { Action } from '@ngrx/store';

import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';

import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/exhaustMap';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take';

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

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

import { Member } from '@overflow/commons-typescript/model/member';
import { MemberService } from '../../service/member.service';

import {
  Signin,
  SigninSuccess,
  SigninFailure,
  SigninRedirect,
  SigninCookie,
  SigninCookieSuccess,
  SigninCookieFailure,

  ActionType,
} from './auth.action';

@Injectable()
export class Effects {
  private _returnURL: any;

  constructor(
    private actions$: Actions,
    private memberService: MemberService,
    private router: Router
  ) { }

  @Effect()
  signin$: Observable<Action> = this.actions$
    .ofType(ActionType.Signin)
    .map((action: Signin) => action.payload)
    .switchMap(payload => {
      this._returnURL = payload.returnURL;
      return this.memberService.signin(payload.email, payload.password);
    })
    .map((result: any) => {
      const authToken = result['authToken'];
      const domainMember = result['domainMember'];
      return new SigninSuccess({authToken: authToken, domainMember: domainMember});
    })
    .catch((error: RESTClientError) => {
      return of(new SigninFailure(error));
    });

  @Effect({ dispatch: false })
  signinSuccess$ = this.actions$
    .ofType(ActionType.SigninSuccess)
    .do(() => {
      this.router.navigateByUrl(this._returnURL);
    });

  @Effect({ dispatch: false })
  signinRedirect$ = this.actions$
    .ofType(ActionType.SigninRedirect, ActionType.Signout)
    .map((action: SigninRedirect) => action.payload)
    .do(returnURL => {
      this.router.navigate(['/auth/signin'], {queryParams: {returnURL: returnURL}});
    });


  @Effect()
  signinCookie$: Observable<Action> = this.actions$
    .ofType(ActionType.SigninCookie)
    .map((action: SigninCookie) => action.payload)
    .switchMap((payload) => {
      this._returnURL = payload.returnURL;
      return this.memberService.signin_cookie(payload.authToken);
    })
    .map((domainMember: DomainMember) => {
      return new SigninCookieSuccess(domainMember);
    })
    .catch((error: RESTClientError) => {
      return of(new SigninFailure(error));
    });

  @Effect({ dispatch: false })
  signinCookieSuccess$ = this.actions$
    .ofType(ActionType.SigninCookieSuccess)
    .do(() => {
      this.router.navigateByUrl(this._returnURL);
    });

  @Effect({ dispatch: false })
  signinCookieFailure$ = this.actions$
    .ofType(ActionType.SigninCookieFailure)
    .map(() => {
      return new SigninRedirect(this._returnURL);
    });
}
