import { Injectable } from "@angular/core";
import { Http, Headers, Response, RequestOptions } from "@angular/http";
// tslint:disable-next-line:import-blacklist
import { Observable } from "rxjs";
import "rxjs/add/operator/map";
import "rxjs/add/operator/catch";
import { LoginData } from "app/data/LoginData";
import { HttpService } from "./http.service";

@Injectable()
export class LoginService {
  constructor(private httpService: HttpService) {}

  postLoginForm(loginData: LoginData): Observable<Response> {
    return this.httpService.post("/Login/PostLogin", loginData);
  }

  set token(token) {
    this.httpService._token = token;
  }

  set role(role) {
    this.httpService._role = role;
  }

  set id(id) {
    this.httpService._id = id;
  }

  set permissions(permissions) {
    this.httpService._permissions = permissions
      .filter(p => p.Enable)
      .map(p => `/${p.PageUrl}`);
    localStorage.setItem(
      "pages",
      JSON.stringify(this.httpService._permissions)
    );
  }

  logout() {
    this.httpService._token = "";
    localStorage.removeItem("id");
    localStorage.removeItem("role");
    localStorage.removeItem("token");
    localStorage.removeItem("name");
    localStorage.removeItem("pages");
    localStorage.removeItem("password");
  }
}
