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 { UserData } from "../data/UserData";
import { GroupData } from "../data/GroupData";
import { RoleData } from "../data/RoleData";
import { PageData } from "../data/PageData";
import { PremissionData } from "../data/PremissionData";
import { HttpService } from "app/service/http.service";
import { NewPwdMailData } from "app/data/NewPwdMailData";
import { TableQueryData } from "app/data/TableQueryData";

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

  postUserData(userdata: UserData): Observable<any> {
    return (
      this.httpService
        .post("/User/PostUserDataList", userdata)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  getUserData(term: string, page?: number, id?: number): Observable<any> {
    const queryWord = "?term=" + (term ? term : "") + "&";
    const queryPage = "page=" + (page ? page : "") + "&";
    const queryId = "id=" + (id ? id : "") + "&";
    return this.httpService
      .get("/User/GetUserDataList" + queryWord + queryPage + queryId)
      .catch(this.handleErrorObservable);
  }

  getUserbyId(id: number): Observable<any> {
    return this.httpService
      .get("/user/" + id)
      // .map(this.extractData)
      .catch(this.handleErrorObservable);
  }

  getSiteInformations(
    tableQueryData: TableQueryData,
    id?: number
  ): Observable<any> {
    let query =
      "?term=" + (tableQueryData.Term ? tableQueryData.Term : "") + "&";
    query += "page=" + (tableQueryData.Page ? tableQueryData.Page : "") + "&";
    query +=
      "order=" + (tableQueryData.Order ? tableQueryData.Order : "") + "&";
    query +=
      "isAsc=" + (tableQueryData.IsAsc ? tableQueryData.IsAsc : "") + "&";

    return this.httpService
      .get("/SiteInformation" + query)
      .catch(this.handleErrorObservable);
  }

  getSiteInformationAll(): Observable<any> {
    return this.httpService
      .get("/siteInformation/all")
      // .map(this.extractData)
      .catch(this.handleErrorObservable);
  }

  getSiteInformationAllByUser(): Observable<any> {
    return this.httpService
      .get("/siteInformation/user")
      // .map(this.extractData)
      .catch(this.handleErrorObservable);
  }


  getUserSiteInfo(): Observable<any> {
    return this.httpService
      .get("/user/GetUserSiteInfo")
      // .map(this.extractData)
      .catch(this.handleErrorObservable);
  }


  getUserPageData(
    tableQueryData: TableQueryData,
    id?: number
  ): Observable<any> {
    let query =
      "?term=" + (tableQueryData.Term ? tableQueryData.Term : "") + "&";
    query += "page=" + (tableQueryData.Page ? tableQueryData.Page : "") + "&";
    query +=
      "order=" + (tableQueryData.Order ? tableQueryData.Order : "") + "&";
    query +=
      "isAsc=" + (tableQueryData.IsAsc ? tableQueryData.IsAsc : "") + "&";
    const queryId = "id=" + (id ? id : "") + "&";
    return this.httpService
      .get("/User/GetUserDataList" + query + queryId)
      .catch(this.handleErrorObservable);
  }

  getUserDatabyId(id: number): Observable<UserData[]> {
    return this.httpService
      .get("/User/GetUserDataById/" + id)
      .map(this.extractData)
      .catch(this.handleErrorObservable);
  }

  getExistUserName(account: string): Observable<any> {
    return (
      this.httpService
        .get("/User/GetExistUser?account=" + account)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  putUserData(userData: UserData): Observable<any> {
    return (
      this.httpService
        .put("/user/PutUserData/" + userData.Id, userData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  putUserStatus(id: number, status: number): Observable<any> {
    const statusKey = "?status=" + status;
    return (
      this.httpService
        .put("/user/PutUserStatus/" + id + statusKey, null)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  putUserPasswordById(id: number): Observable<any> {
    return (
      this.httpService
        .put("/user/PutUserPassword/" + id, null)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  deleteUserData(id: string): Observable<any> {
    return (
      this.httpService
        .delete("/user/DeleteUserData?id=" + id)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  putUserPassword(userData: UserData): Observable<any> {
    return (
      this.httpService
        .put("/User/PutUserPassword", userData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  postRoleDataList(roleData: RoleData): Observable<any> {
    return (
      this.httpService
        .post("/Role/PostRoleDataList", roleData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  getRoleDataList(tableQueryData: TableQueryData): Observable<any> {
    let query =
      "?term=" + (tableQueryData.Term ? tableQueryData.Term : "") + "&";
    query += "page=" + (tableQueryData.Page ? tableQueryData.Page : "") + "&";
    query +=
      "order=" + (tableQueryData.Order ? tableQueryData.Order : "") + "&";
    query +=
      "isAsc=" + (tableQueryData.IsAsc ? tableQueryData.IsAsc : "") + "&";
    return this.httpService
      .get("/Role/GetRoleDataList" + query)
      .catch(this.handleErrorObservable);
  }

  postPageListData(pageData: PageData): Observable<any> {
    return (
      this.httpService
        .post("/System/PostPageListData", pageData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  putPageData(pageData: PageData): Observable<any> {
    return (
      this.httpService
        .put("/System/PutPageData/" + pageData.Id, pageData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  getPageDataList(tableQueryData: TableQueryData): Observable<any> {
    let query =
      "?term=" + (tableQueryData.Term ? tableQueryData.Term : "") + "&";
    query += "page=" + (tableQueryData.Page ? tableQueryData.Page : "") + "&";
    query +=
      "order=" + (tableQueryData.Order ? tableQueryData.Order : "") + "&";
    query +=
      "isAsc=" + (tableQueryData.IsAsc ? tableQueryData.IsAsc : "") + "&";
    return (
      this.httpService
        .get("/System/GetPageDataList" + query)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  deletePageData(id: string): Observable<any> {
    return (
      this.httpService
        .delete("/System/DeletePageData/" + id)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }
  getAllPagePermission(roleId: number): Observable<any[]> {
    return this.httpService
      .get("/Role/GetAllPagePermission/" + roleId)
      .map(this.extractData)
      .catch(this.handleErrorObservable);
  }

  putPageRoleStatus(premissionData: PremissionData): Observable<any[]> {
    console.log(`current enable ${premissionData.Enable}`);
    const body = { ...premissionData, Enable: !premissionData.Enable };
    console.log("body", body);
    return this.httpService
      .put("/Role/PutPageRoleStatus/", body)
      .map(this.extractData)
      .catch(this.handleErrorObservable);
  }

  sendNewPwdMail(newPwdMailData: NewPwdMailData): Observable<any> {
    return this.httpService
      .post("/Account/SendNewPwdMail", newPwdMailData)
      .catch(this.handleErrorObservable);
  }

  postGroupData(groupData: GroupData): Observable<any> {
    return (
      this.httpService
        .post("/Group/PostGroupData", groupData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  putGroupData(groupData: GroupData): Observable<any> {
    return this.httpService
      .put("/Group/PutGroupData", groupData)
      .catch(this.handleErrorObservable);
  }

  getGroupData(tableQueryData: TableQueryData): Observable<any> {
    let query =
      "?term=" + (tableQueryData.Term ? tableQueryData.Term : "") + "&";
    query += "page=" + (tableQueryData.Page ? tableQueryData.Page : "") + "&";
    query +=
      "order=" + (tableQueryData.Order ? tableQueryData.Order : "") + "&";
    query +=
      "isAsc=" + (tableQueryData.IsAsc ? tableQueryData.IsAsc : "") + "&";
    return this.httpService
      .get("/Group/GetGroupDataList" + query)
      .catch(this.handleErrorObservable);
  }

  deleteGroupData(id: string): Observable<any> {
    return (
      this.httpService
        .delete("/Group/DeleteGroupData/" + id)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  getGroupMember(
    term: string,
    page?: number,
    groupId?: number
  ): Observable<any> {
    let query = "?term=" + (term ? term : "") + "&";
    query += "page=" + (page ? page : "") + "&";
    query += "groupId=" + (groupId ? groupId : "") + "&";
    return (
      this.httpService
        .get("/Group/GetGroupMemberList" + query)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  private extractData(res: Response) {
    const body = res.json();
    return body.Result || [];
  }
  private handleErrorObservable(error: Response | any) {
    console.error(error.message || error);
    return Observable.throw(error.message || error);
  }
}
