import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';


@Injectable()
export class AuthenticationService {
    static hostname: string = "http://localhost";
    // This property is specifically added for BA tools becauce it uses in service passing of authentication calls which are 
    // caught by the MVC WCF handler rather than being passed to the main service in picture
    static istools: boolean = false; 
    public userObj: any = {};
    constructor(private http: Http) { }

    login(username: string, password: string) {
        let authUrl = AuthenticationService.hostname + '/PulseServices/Authenticate.svc/Login';
        if (AuthenticationService.istools == true) {
            authUrl = AuthenticationService.hostname + '/PulseServices/Authenticate.@@@/Login';
        }
        return this.http.post(authUrl, { username: username, password: password })
            .map((response: Response) => {
                // login successful if there's a response cookie in the response
                let result = response.json();
                if (result.LoginResult && result.LoginResult.success) {
                    // store user details and cookies in local storage to keep user logged in between page refreshes
                    this.userObj = JSON.stringify(result.LoginResult);
                    localStorage.setItem('currentUser', this.userObj);

                }
                return result.LoginResult;
            });
    }
    getAuthenticationObject() {
        return JSON.parse(localStorage.getItem('currentUser'));
    }

    isAuthenticated(): boolean {
        if (localStorage.getItem('currentUser')) {
            // logged in so return true
            return true;
        }
        else {
            return false;
        }
    }
    forgotPassword(email: string) {
        let authUrl = AuthenticationService.hostname + '/PulseServices/Authenticate.svc/ForgotPassword';
        if (AuthenticationService.istools == true) {
            authUrl = AuthenticationService.hostname + '/PulseServices/Authenticate.@@@/ForgotPassword';
        }
        return this.http.post(authUrl, { email: email })
            .map((response: Response) => {
                // login successful if there's a response cookie in the response
                let result = response.json();
                if (result.ForgotPasswordResult && result.ForgotPasswordResult.success) {
                    //do nothing
                }
                return result.ForgotPasswordResult;
            });
    }

    logout() {
        // remove user from local storage to log user out
        localStorage.removeItem('currentUser');
    }
}

export interface AuthenticationServiceConfig {
    hostname: string,
    istools : boolean
}

export function ConfigureAuthenticationService(configuration: AuthenticationServiceConfig) {
    AuthenticationService.hostname = configuration.hostname;
    if(configuration.istools)
       AuthenticationService.istools = configuration.istools;
}