import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core';
import {FileService} from '../file.service';
import {File} from '../file';

@Component({
  selector: 'app-save-file',
  templateUrl: './save-file.component.html',
  styleUrls: ['./save-file.component.scss']
})
export class SaveFileComponent implements OnInit {

  isActive = false;
  @Output() savedFile = new EventEmitter<boolean>();
  @Input() file: File;
  isUploading = false;

  constructor(public fileService: FileService) { }

  ngOnInit(): void {  }

  setState(value: boolean): void {
    const text = document.getElementById('filenameInput') as HTMLTextAreaElement;
    this.isActive = value;
    if (value) {
      text.focus();
    }
  }

  submitFile(): void {
    this.isUploading = true;
    this.fileService.upload(this.file)
      .subscribe((response) => {
        this.isUploading = false;
        this.savedFile.emit(true);
        this.isActive = false;
      }, (error) => {
        console.log(error);
        this.isUploading = false;
      });
  }
}
