import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.scss']
})
export class MainComponent implements OnInit {
    httpOptions: any;
    allUserAppFolder: any;
    allUserAppFile: any;
    mainFolder: any;
    currentUser: any;
    socialIdUser: String;
  constructor(private router: Router, private route: ActivatedRoute, private http: HttpClient) { }

    logout() {
      localStorage.removeItem('jwtToken');
      this.router.navigate(['home']);
    }

  ngOnInit() {
      this.httpOptions = {
          headers: new HttpHeaders({
              'Authorization': localStorage.getItem('jwtToken'),
              'Access-Control-Allow-Origin' : '*',
              'Access-Control-Allow-Methods' : 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
              'Access-Control-Allow-Headers' : 'Origin, X-Requested-With, Content-Type, Accept'
          }),
          pathFolder: ''
      };


      this.http.get('/api/getCurrentUser', this.httpOptions).subscribe(user => {
          this.currentUser  = user;
          if (user) {
              this.getAllUserAppFolder();
          }
      }, err => {
          if (err.status === 401) {
              this.router.navigate(['/']);
          }
      });
      // this.httpOptions = {
      //     headers: new HttpHeaders({ 'Authorization': localStorage.getItem('jwtToken'), 'Access-Control-Allow-Origin' : '*'}),
      //     pathFolder: ''
      // };



  }


    getItems(path) {
      var test = [];
        if(this.allUserAppFile !== undefined){
            test = this.allUserAppFile.filter((file) => file.path === path);
        }
        return test;
    }

    setOriginFolder(path) {
        var pathChunk = path.split('/');

        var origin = pathChunk.length === 1 ? pathChunk[0] : pathChunk[pathChunk.length - 2]

        return origin;
    }

    getItemsFolder(name) {
      var tempp;
        var test = [];
        if(this.allUserAppFolder !== undefined) {
            tempp = this.allUserAppFolder;
            test = tempp.filter((folder) => folder.origin === name);

        }
        return test;
    }

    getAllUserAppFiles() {
        this.http.post('/api/getFileAppList', {owner: this.currentUser.username.toString()}).subscribe(files => {
            if (files) {
                this.allUserAppFile = files;
                // return files;
            } else {
                this.allUserAppFile = [];
            }
        });
    }

    getAllUserAppFolder(){

        this.http.post('/api/getFolderAppList', {owner: this.currentUser.username.toString()}).subscribe(folders => {
            if (folders) {

                this.allUserAppFolder = folders;
                for (let f of this.allUserAppFolder){
                    f.origin = this.setOriginFolder(f.path);
                    //console.log('origin : ' + f.origin);
                }
                this.getAllUserAppFiles();

                    } else {
                this.allUserAppFolder = [];
            }
        });


    }
}
