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 { AnnounceData } from "app/data/AnnounceData";
import { CarData } from "app/data/CarData";
import { SiteEventTypesData } from "app/data/SiteEventTypesData";
import { MalfunctionData } from "app/data/MalfunctionData";
import { EventTypeReportData } from "app/data/EventTypeReportData";

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

  postAnnounceData(announceData: AnnounceData): Observable<any> {
    return (
      this.httpService
        .post("/announce", announceData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

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

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


  putAnnounceData(announceData: AnnounceData): Observable<any> {
    return (
      this.httpService
        .put("/announce/" + announceData.Id, announceData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }


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

  getEventCountByThisYear(): Observable<any> {
    return (
      this.httpService
        .get("/event/count/thisYear")
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  getEventCountByThisMonth(): Observable<any> {
    return (
      this.httpService
        .get("/event/count/thisMonth")
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }


  getEventCountByInterval(startDatetime: string, endDatetime: string): Observable<any> {
    return (
      this.httpService
        .get("/event/count/interval?startDatetime=" + startDatetime + "&endDatetime=" + endDatetime)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

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

  getPunishEvent(eventType: string, startTime: string, endTime: string, site: string, status: string, time: string, speed: string, method: string): Observable<any> {
    let query =
      "?startTime=" + (startTime ? startTime : "") + "&";
    query += "endTime=" + (endTime ? endTime : "") + "&";
    query += "site=" + (site ? site : "") + "&";
    query += "status=" + (status ? status : "") + "&";
    query += "time=" + (time ? time : "") + "&";
    query += "speed=" + (speed ? speed : "") + "&";
    query += "method=" + (method ? method : "");

    return (
      this.httpService
        .get("/event/eventType/" + eventType + query)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }



  getPunishEventExport(eventType: string, startTime: string, endTime: string, site: string, status: string, time: string, speed: string, method: string): Observable<any> {
    let query =
      "?startTime=" + (startTime ? startTime : "") + "&";
    query += "endTime=" + (endTime ? endTime : "") + "&";
    query += "site=" + (site ? site : "") + "&";
    query += "status=" + (status ? status : "") + "&";
    query += "time=" + (time ? time : "") + "&";
    query += "speed=" + (speed ? speed : "") + "&";
    query += "method=" + (method ? method : "");

    return (
      this.httpService
        .getDownload("/event/eventType/" + eventType + query)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  putCheckPunish(eventId: number, law: string): Observable<any> {
    return (
      this.httpService
        .put("/event/" + eventId + "/checkPunish?law=" + law, null)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }


  putMuticheckPunish(law: string, eventId: any): Observable<any> {
    const query = "?law=" + (law ? law : "");

    return (
      this.httpService
        .put("/event/muti/checkPunish" + query, eventId)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  putNopunish(eventId: number, reason: string): Observable<any> {
    return (
      this.httpService
        .put("/event/" + eventId + "/nopunish?reason=" + reason, null)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }



  putCancel(eventId: number): Observable<any> {
    return (
      this.httpService
        .put("/event/" + eventId + "/cancel", null)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

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


  getEventById(id: string): Observable<any> {
    return this.httpService
      .get("/event/" + id)
      // .map(this.extractData)
      .catch(this.handleErrorObservable);
  }

  putEventVehicle(eventId: number, vehicleType: string, vehicleNumber: string): Observable<any> {
    let query =
      "?vehicleType=" + (vehicleType ? vehicleType : "") + "&";
    query += "vehicleNumber=" + (vehicleNumber ? vehicleNumber : "");
    return (
      this.httpService
        .put("/event/" + eventId + "/vehicle" + query, null)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  postReportSiteEventTypes(method: string, siteEventTypesData: SiteEventTypesData): Observable<any> {
    return (
      this.httpService
        .post("/report/site/eventTypes?method=" + method, siteEventTypesData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }


  postReportMalfunction(method: string, malfunctionData: MalfunctionData): Observable<any> {
    return (
      this.httpService
        .post("/report/malfunction?method=" + method, malfunctionData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  postReportEventType1(method: string, eventTypeData: EventTypeReportData): Observable<any> {
    return (
      this.httpService
        .post("/report/eventType/1?method=" + method, eventTypeData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  postReportEventType2(method: string, eventTypeData: EventTypeReportData): Observable<any> {
    return (
      this.httpService
        .post("/report/eventType/2?method=" + method, eventTypeData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  postReportEventType34(method: string, eventTypeData: EventTypeReportData): Observable<any> {
    return (
      this.httpService
        .post("/report/eventType/34?method=" + method, eventTypeData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  postReportEventType6(method: string, eventTypeData: EventTypeReportData): Observable<any> {
    return (
      this.httpService
        .post("/report/eventType/6?method=" + method, eventTypeData)
        // .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);
  }
}
