import { Injectable } from '@angular/core';
import { Observable ,  Observer } from 'rxjs';
import {map} from 'rxjs/operators';
import { IListItem } from '../models/list.item';
import { IItem } from '../models/item';
import { HttpClient } from "@angular/common/http";
import { AppConfig } from "../shared/app.config";

@Injectable({
  providedIn: 'root',
})
export class FolderService {

  constructor(public httpClient: HttpClient, public appConfig: AppConfig) {
  }

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

     getFolder(code: string): Observable<IListItem> {
      return this.httpClient.get<IListItem>(`${this.ApiEndpoint()}/api/v1/folders/${code}`);            
    }

    getMyRoots(): Observable<IListItem[]> {
      return this.httpClient.get<IListItem[]>(`${this.ApiEndpoint()}/api/v1/folders/my_roots`);
    }    

    getFolderParents(code: string): Observable<IListItem[]> {
      return this.httpClient.get<IListItem[]>(`${this.ApiEndpoint()}/api/v1/folders/${code}/parents`);
    }

    getChildFolders(code: string): Observable<IListItem[]> {
      return this.httpClient.get<IListItem[]>(`${this.ApiEndpoint()}/api/v1/folders/${code}/listfolders`);
    }

    getItems(code: string,skip:number, take:number): Observable<IListItem[]> {
      return this.httpClient.get<IListItem[]>(`${this.ApiEndpoint()}/api/v1/folders/${code}/list?skip=${skip}&take=${take}`);
    }

    moveItem(itemCode: string, newParentCode: string): Observable<IListItem> {
        const body = { itemCode: itemCode,newParentCode: newParentCode };
      return this.httpClient.post<IListItem>(`${this.ApiEndpoint()}/api/v1/items/move`, body);
    }

    createFolder(folder: IItem): Observable<IListItem> {
      return this.httpClient.post<IListItem>(`${this.ApiEndpoint()}/api/v1/folders/`, folder);
    }
}
