import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {IListItem} from '../models/list.item';
import {IItem} from '../models/item';
import { HttpClient } from '@angular/common/http';
import {AppConfig} from '../shared/app.config';
import {ItemService} from './item.service';

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

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

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

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

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

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

  getFolderParents(code: string): Observable<IListItem[]> {
    return this.itemService.getBreadcrumbs(code);
  }

  getChildFolders(code: string): Observable<IListItem[]> {
    const folderItemTypes = "FOLDER,FTP_FOLDER";
    return this.itemService.getChildItemList(code, folderItemTypes);
  }

  getItems(code: string,skip:number, take:number): Observable<IListItem[]> {
    return this.itemService.getChildItemList(code, null, null, 1, true, null, null, skip, 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);
  }
}
