import {Injectable} from '@angular/core';
import {File} from './file';
import {Observable, of} from 'rxjs';
import {HttpClient} from '@angular/common/http';
import { User } from './user';


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

  saveUrl = 'http://localhost/sfcode/backend/filesave.php';
  uploadUrl = 'http://localhost/sfcode/backend/fileupload.php';
  fileListUrl = 'http://localhost/sfcode/backend/dir_tree.php';
  deleteUrl = 'http://localhost/sfcode/backend/filedelete.php';

  constructor(private http: HttpClient) {
  }

  getFiles(): Observable<File[]> {

    const ret: File[] = [];
    const arr: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

    for (const item of arr) {
      ret.push({
        username: '',
        filename: 'lambda',
        language: '.cpp',
        text: 'Random Shit',
        path: '/'
      });
    }

    return of(ret);
  }

  upload(file): Observable<any> {
    return this.http.post(this.saveUrl, file);
  }

  upload2(file): Observable<any> {
    const formData = new FormData();
    formData.append('file', file, file.name);
    return this.http.post(this.uploadUrl, formData);
  }

  delete(file): Observable<any> {
    return this.http.post(this.deleteUrl, file);
  }

  getFileList(username: string): Observable<any> {
    return this.http.post(this.fileListUrl, {username});
  }

}
