import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { IItemType } from '../models/item.type';
import { IItem } from '../models/item';
import { IJsonline } from '../models/json-line';
import { IItemTask } from '../models/itemTask';
import { HttpClient, HttpParams,HttpHeaders } from "@angular/common/http";
import { AppConfig } from "../shared/app.config";
import {ItemTypeService} from './itemtype.service';

@Injectable({
  providedIn: 'root',
})
export class ItemService {
  constructor(public httpClient: HttpClient, public appConfig: AppConfig,private itemTypeService:ItemTypeService) {
  }

  ApiEndpoint() {
    return this.appConfig.getConfig("apiEndPoint");
  }

  getItemTypes(): Observable<{ [id: string]: IItemType }> {
    return this.httpClient.get<{ [id: string]: IItemType }>(`${this.ApiEndpoint()}/api/v1/itemtypes/`);
  }

  getFeatures(extent: number[], crs: string, searchText?: string, searchTags?:string,startDate?:Date,endDate?:Date,itemType?:string,parentCode?:string,dataFilter?:string,level?:number): Observable<any> {
    var params = new HttpParams();
    params = params.append("crs", crs);
    if (extent) params =params.append("bbox", extent.join(","));
    if (searchText) params = params.append("q", searchText);
    if (searchTags) params = params.append("t", searchTags);
    if (startDate) params = params.append("sd", startDate.toISOString());
    if (endDate) params = params.append("ed", endDate.toISOString());
    if (itemType) {
      params = params.append("it", itemType);
      let extraAttributes = this.itemTypeService.getExtraAttributes(itemType);
      if(extraAttributes) {
        params = params.append("da", extraAttributes);
      }
    }
    if (parentCode) params = params.append("pc", parentCode);
    if (dataFilter) params = params.append("df", dataFilter);
    if (level) params = params.append("lvl", level.toString());
    return this.httpClient.get<any>(`${this.ApiEndpoint()}/api/v1/items/features/`, {params:params});
  }

  getFeature(code:string, crs: string): Observable<any> {
    var params = new HttpParams();
    params = params.append("crs", crs);
    return this.httpClient.get<any>(`${this.ApiEndpoint()}/api/v1/items/${code}/feature/`, { params: params });
  }

  getItemFeature(code:string, fid:number, crs: string): Observable<any> {
    var params = new HttpParams();
    params = params.append("crs", crs);
    return this.httpClient.get<any>(`${this.ApiEndpoint()}/api/v1/items/${code}/feature/${fid}`, { params: params });
  }

  getItem(code: string): Observable<IItem> {
    return this.httpClient.get<IItem>(`${this.ApiEndpoint()}/api/v1/items/${code}`);
  }

  getItemData(code: string,start?:number,size?:number): Observable<ArrayBuffer> {
    var headers = new HttpHeaders();
    if(start !== undefined && size !== undefined) headers=headers.set("Range",`bytes=${start}-${size-1}`);
    return this.httpClient.get(`${this.ApiEndpoint()}/api/v1/items/${code}/data`,{ headers: headers,responseType: 'arraybuffer' });
  }

  getItemByCodeAndType(code: string, itemType: string): Observable<IItem> {
    return this.httpClient.get<IItem>(`${this.ApiEndpoint()}/api/v1/items/${code}/${itemType}`);
  }

  getItemList(itemType?: string, dataFilter?: any, level?: number, atItemLocationItemCode?: string, indexed?: boolean, validToday?: boolean): Observable<IItem[]> {
    var params = new HttpParams();
    if(itemType)  params = params.append("it", itemType);   
    if(dataFilter) params = params.append("df", JSON.stringify(dataFilter));    
    if(atItemLocationItemCode) params = params.append("ail",atItemLocationItemCode);    
    if(indexed) params = params.append("ind",indexed?"true":"false");    
    if (level) params = params.append("lvl", level.toFixed());
    if (validToday) params = params.append("vt", validToday ? "true" : "false");
    return this.httpClient.get<IItem[]>(`${this.ApiEndpoint()}/api/v1/items/`, { params: params });
  }

  getChildItemList(parentcode: string, itemType: string, dataFilter?: any, level: number = 1, deep: boolean = true): Observable<IItem[]> {
    var params = new HttpParams();
    if(itemType != null) {
      params = params.append("it", itemType);
    }
    if (dataFilter != null) {
      params = params.append("df", JSON.stringify(dataFilter));
    }
    params = params.append("lvl", level.toString());
    params = params.append("deep", deep.toString());
    return this.httpClient.get<IItem[]>(`${this.ApiEndpoint()}/api/v1/items/${parentcode}/children`, { params: params });
  }

  getChildItemListCount(parentcode: string, itemType: string,dataFilter?: any): Observable<Number> {
    var params = new HttpParams();
    params = params.append("it", itemType);
    if (dataFilter != null) {
      params = params.append("df", JSON.stringify(dataFilter));
    }
    return this.httpClient.get<Number>(`${this.ApiEndpoint()}/api/v1/items/${parentcode}/children/count`, { params: params });
  }

  getChildItemListByExtent(parentcode: string, itemType: string, extent: number[], crs: string, dataFilter?: any, level: number = 1): Observable<IItem[]> {
    var params = new HttpParams();
    params = params.append("it", itemType);
    params = params.append("bbox", extent.join(","));
    params = params.append("crs", crs);
    if (dataFilter != null) {
      params = params.append("df", JSON.stringify(dataFilter));
    }
    params = params.append("lvl", level.toString());
    return this.httpClient.get<IItem[]>(`${this.ApiEndpoint()}/api/v1/items/${parentcode}/children`, { params: params });
  }

  getItemFeatures(code: string, extent: number[], crs: string, layerIndex?:number): Observable<any> {
    var params = new HttpParams();
    params = params.append("crs", crs);
    if(extent != null) {
      params = params.append("bbox", extent.join(","));
    }
    if(layerIndex!=null)
      return this.httpClient.get<any>(`${this.ApiEndpoint()}/api/v1/items/${code}/features/layer/${layerIndex}`, { params: params });
    else
      return this.httpClient.get<any>(`${this.ApiEndpoint()}/api/v1/items/${code}/features`, { params: params });
  }
  
  postItem(item:IItem): Observable<IItem> {
    return this.httpClient.post<IItem>(`${this.ApiEndpoint()}/api/v1/items`,item);
  }
  
  putItem(item:IItem): Observable<IItem> {
    return this.httpClient.put<IItem>(`${this.ApiEndpoint()}/api/v1/items/${item.code}`,item);
  }

  deleteItem(code: string): Observable<any> {
    return this.httpClient.delete<any>(`${this.ApiEndpoint()}/api/v1/items/${code}`);
  }

  deleteItems(itemCodes:string[]): Observable<any> {
    return this.httpClient.post<any>(`${this.ApiEndpoint()}/api/v1/items/delete`, itemCodes);
  }

  getTemporalLast(code: string): Observable<IJsonline> {
    return this.httpClient.get<any>(`${this.ApiEndpoint()}/api/v1/items/${code}/temporal/last`);
  }

  getTemporal(code: string, startDate?: Date, endDate?: Date): Observable<any> {
    var params = new HttpParams();
    if (startDate) params = params.append("sd", startDate.toISOString());
    if (endDate) params = params.append("ed", endDate.toISOString());
    return this.httpClient.get<any>(`${this.ApiEndpoint()}/api/v1/items/${code}/temporal/`, { params: params });
  }

  postItemTask(item: IItem, task: IItemTask): Observable<IItemTask> {
    return this.httpClient.post<IItemTask>(`${this.ApiEndpoint()}/api/v1/items/${item.code}/tasks`, task);
  }
  
  getItemTaskList(itemcode: string, unfinishedOnly?: boolean): Observable<IItemTask[]> {
    var params = new HttpParams();
    if (unfinishedOnly) params = params.append("unfinishedOnly", unfinishedOnly.toString());
    return this.httpClient.get<IItemTask[]>(`${this.ApiEndpoint()}/api/v1/items/${itemcode}/tasks`, { params: params });
  }

  getItemListUsingRelationship(itemType: string, relationshipItemType: string, relationshipDataFilter: any): Observable<IItem[]> {
    var params = new HttpParams();
    params = params.append("it", itemType);   
    params = params.append("rsit", relationshipItemType);   
    params = params.append("rsdf", JSON.stringify(relationshipDataFilter));
    return this.httpClient.get<IItem[]>(`${this.ApiEndpoint()}/api/v1/items/userelationship`, { params: params });
  }

  getLayerValue(itemCode: string, layerIndex:number,x:number,y:number,crs:string = "EPSG:3857"): Observable<number> {
    return this.httpClient.get<any>(`${this.ApiEndpoint()}/api/v1/items/${itemCode}/value/layer/${layerIndex}?c=${x},${y}&crs=${crs}`);
  }

}