import { Component, OnInit } from '@angular/core';
import {Observable, Observer} from 'rxjs';
import { FetcherService } from '../fetcher.service';
import { GET_GIT_FILES_API, POST_FILE_CHANGE_API } from '../urls';
import { getGitFilesResponse, postFileChangesResponse } from '../responses';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { CookieService } from 'ngx-cookie-service';


export interface entry{
  id: number;
  name: string;
  commitMessage: string;
  commitDate: Date;
  type: string;
  url: string;
}

export interface Section {
  id: number;
  name: string;
  commitDate: Date;
  url: string;
}

@Component({
  selector: 'app-github',
  templateUrl: './github.component.html',
  styleUrls: ['./github.component.css']
})
export class GithubComponent implements OnInit {
  projectId = '';
  getGitFiles_stat$ = new Observable<getGitFilesResponse>;
  postFileChange_stat$ = new Observable<postFileChangesResponse>;

  constructor(private fetcherService: FetcherService, private http: HttpClient, private router: Router, private cookieService: CookieService) { 
  }
  fetchData(id: string) {
    //return dummy data of type entry
    var files:entry[] = [
      {id: 1, name: "test", commitMessage: "test", commitDate: new Date('1/27/2022'), type: "folder", url: "test\n".repeat(10)},
      {id: 2, name: "test", commitMessage: "test", commitDate: new Date('1/1/2022'), type: "folder", url: "test"},
      {id: 3, name: "test", commitMessage: "test", commitDate: new Date('1/1/2022'), type: "folder", url: "test"},
      {id: 4, name: "test", commitMessage: "test", commitDate: new Date('1/1/2022'), type: "folder", url: "test"},
      {id: 5, name: "test", commitMessage: "test", commitDate: new Date('1/1/2022'), type: "folder", url: "test"},
      {id: 6, name: "test", commitMessage: "test", commitDate: new Date('1/1/2022'), type: "file", url: "test"},
      {id: 7, name: "test", commitMessage: "test", commitDate: new Date('1/1/2022'), type: "file", url: "test"},
      {id: 8, name: "test", commitMessage: "test", commitDate: new Date('1/1/2022'), type: "file", url: "test"},
      {id: 9, name: "test", commitMessage: "test", commitDate: new Date('1/1/2022'), type: "file", url: "test"},
      {id: 10, name: "test", commitMessage: "test", commitDate: new Date('1/1/2022'), type: "file", url: "test"},
    ]
    return files;
  }
  submitFile(id: number,data: string) {
    //submit data to server
    console.log("submitting file");
    console.log(data);
    console.log(data.split("\n").length);
    for (let i = 0; i < document.getElementsByClassName("file-text").length; i++) {
      document.getElementsByClassName("file-text")[i].setAttribute("readonly", "true");
    }
  }
  allowEditFile(){
    for (let i = 0; i < document.getElementsByClassName("file-text").length; i++) {
      document.getElementsByClassName("file-text")[i].removeAttribute("readonly");
    }
  }
  files: entry[] = this.fetchData(this.projectId);

  ngOnInit(): void {
    //this.projectId = this.fetcherService.getCurrentProjectId();
    this.projectId = this.cookieService.get("pid");
    console.log(this.projectId);
    this.getGitFiles_stat$ = this.http.get<getGitFilesResponse>(GET_GIT_FILES_API+'?pid=' +this.projectId);
    this.getGitFiles_stat$.subscribe((data) => {
      console.log(data);
      if (data.status == true) {
        console.log("success");
        console.log(data.files);
      }
      else {
        console.log("failure");
      }
    });
    }

}