import { Injectable } from "@angular/core";
import { Component, OnInit } from "@angular/core";
import { Http, Headers } from "@angular/http";
@Injectable()
export class HelperService {
  status: any[];
  failureInfo: any[];
  lightNo: any[];
  RoleCode: any[];
  RolePermission: any[];
  UserCode: any[];
  GroupCode: any[];
  VillageInfo: any[];
  FinishedInfo: any[];
  MaintenanceProviderInfo: any[];
  _token = "";
  _role = "";
  _id = "";
  _permissions: string[] = [];
  // _url = "https://localhost:44306/api";

  _url = "https://172.16.1.49:8443/violation/api";
  // _url = "https://carviolation.tycg.gov.tw:8443/violation/api";


  constructor(private http: Http) {
    this.setToken(localStorage.getItem("token")),
      this.setPermission(JSON.parse(localStorage.getItem("pages"))),
      this.setRole(JSON.parse(localStorage.getItem("role")));
  }

  setToken(token) {
    this._token = token;
  }

  setRole(role) {
    this._role = role;
  }

  setId(id) {
    this._id = id;
  }

  setPermission(permissions) {
    this._permissions = permissions;
  }

  createAuthorizationHeader(headers: Headers) {
    if (this._token) {
      headers.append("Authorization", "Bearer " + this._token);
    }
  }

  setRoleCodeData() {
    const headers = new Headers();
    this.createAuthorizationHeader(headers);
    this.http.get(this._url + "/Role/GetRoleInfoList", { headers: headers }).subscribe(res => {
      this.RoleCode = res.json().Result;
    });
  }

  setUserCodeData() {
    const headers = new Headers();
    this.createAuthorizationHeader(headers);
    this.http.get(this._url + "/User/GetUserNameList", { headers: headers }).subscribe(res => {
      this.UserCode = res.json().Result;
    });
  }

  setGroupName() {
    const headers = new Headers();
    this.createAuthorizationHeader(headers);
    this.http.get(this._url + "/Group/GetGroupNameList", { headers: headers }).subscribe(res => {
      this.GroupCode = res.json().Result;
    });
  }

  getStatusText(id: number) {
    return this.status.filter(x => x.Id === id)[0].StatusName;
  }

  getFailureText(id: number) {
    return this.failureInfo.filter(x => x.Id === id)[0].FailureInfo;
  }

  getRoleCodeText(id: number) {
    return this.RoleCode.filter(x => x.Id === id)[0].RoleName;
  }

  getUserCodeText(id: number) {
    const users = this.UserCode.filter(x => x.Id === id);
    if (users && users.length > 0) {
      return users[0].UserName;
    }
  }

  getGroupCodeText(id: number) {
    return this.GroupCode.filter(x => x.Id === id)[0].StationName;
  }

  getVillageCodeText(villageNo: number) {
    const villages = this.VillageInfo.filter(x => x.VillageNo === villageNo);
    if (villages && villages.length > 0) {
      return villages[0].VillageName;
    }
  }

  getFinishedCodeText(id: number) {
    return this.FinishedInfo.filter(x => x.id === id)[0].FinishedItem;
  }

  getMaintenanceProviderInfo(id: number) {
    console.log(id);
    return this.MaintenanceProviderInfo.filter(x => x.id === id)[0].ProviderName;
  }
}
