import { Injectable } from "@angular/core";
import { HttpService } from "app/service/http.service";
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 { TableQueryData } from "../data/TableQueryData";
import { SiteInformationData } from "app/data/SiteInformationData";
import { CompanyInformationData } from "../data/CompanyInformationData"
import { ReasonData } from "app/data/ReasonData";

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

  getSiteInformations(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("/SiteInformation" + query)
      .catch(this.handleErrorObservable);
  }

  postSiteInformations(siteInformationData: SiteInformationData): Observable<any> {
    return (
      this.httpService
        .post("/siteInformation", siteInformationData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  getCompanyInformation(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("/companyInformation" + query)
      .catch(this.handleErrorObservable);
  }

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

  postCompanyInformation(companyInformationData: CompanyInformationData): Observable<any> {
    return (
      this.httpService
        .post("/companyInformation", companyInformationData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  putCompanyInformationData(companyInformationData: CompanyInformationData): Observable<any> {
    return (
      this.httpService
        .put("/companyInformation/" + companyInformationData.id , companyInformationData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  putSiteData(siteInformationData: SiteInformationData): Observable<any> {
    return (
      this.httpService
        .put("/siteInformation/" + siteInformationData.Id , siteInformationData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

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


  postReasonData(reasonData: ReasonData): Observable<any> {
    return (
      this.httpService
        .post("/notPunish", reasonData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }


  getReasonPageList(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("/notPunish" + query)
      .catch(this.handleErrorObservable);
  }


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


  putReasonData(reasonData: ReasonData): Observable<any> {
    return (
      this.httpService
        .put("/notPunish/" + reasonData.Id , reasonData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

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

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

  getCompanyAllInfo(): Observable<any> {
    return (
      this.httpService
        .get("/companyInformation/all")
        // .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);
  }
}
