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


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

  saveUrl = 'http://localhost/sfcode/backend/filesave.php';
  uploadUrl = 'http://localhost/sfcode/backend/fileupload.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);
  }

}
