import {Injectable} from '@angular/core';
import {Problem} from './problem';
import {Observable, of, throwError} from 'rxjs';

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

  runStatus = 'Compiling...';
  result = 0;

  constructor() {
  }

  run(): void {
    const output = `25
abcd
150`;
    const statusArray = ['Uploading...', 'Compiling...', 'Running...', `Done!

        <div class="code">${output}</div>`];

    this.result = 0;
    let time = 0;
    for (const status of statusArray) {
      setTimeout(() => {
        this.runStatus = status;
      }, time);
      time += 2000;
    }

    setTimeout(() => {
      this.result = 1;
    }, 6000);
  }

  compileProblemFile(prob: Problem): Observable<string> {
    if (Math.floor(Math.random() * 2) === 1) {return of('done'); }
    else {return throwError('error'); }
  }

  tryTestcase(prob: Problem, index: number): Observable<boolean> {
    return of(Math.floor(Math.random() * 2) === 1);
  }
}
