import { Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core';
import { RouterHandler } from '../../../shared/services/router-handler.service';
import { SearchResult, SearchService } from '../../../shared/server-services/search-service';
import { sideMenuActiveObserver } from 'src/app/shared/shared-observers';

@Component({
  selector: 'app-search-sidebar-view',
  templateUrl: './search-sidebar-view.component.html',
  styleUrls: ['./search-sidebar-view.component.scss'],
})
export class SearchSidebarViewComponent implements OnDestroy {
  public searchResult: Map<string, SearchResult[]> = new Map<string, SearchResult[]>();
  public searchQuery: string;
  public searchResourceIds: string[];

  public activeResourceId; // clicked, to check the drive
  public isOnSearchPage = false;

  public onRouterChangeSearch = (res) => {
    this.isOnSearchPage = true;
  };

  public onRouterChangeResource = (res) => {
    this.isOnSearchPage = false;
    this.activeResourceId = res.params.id;
  };

  constructor(private routerHandler: RouterHandler, private searchService: SearchService) {
    this.routerHandler.subscribe('search', this.onRouterChangeSearch);
    this.routerHandler.subscribe('res', this.onRouterChangeResource);

    this.searchService.subscribe(this.searchHandler);

    if (this.routerHandler.isOnPage('search')) {
      this.onRouterChangeSearch(this.routerHandler.getRoute());
    }
    if (this.routerHandler.isOnPage('res')) {
      this.onRouterChangeResource(this.routerHandler.getRoute());
    }

    this.searchHandler();
  }

  private searchHandler = () => {
    let map: Map<string, SearchResult[]> = new Map<string, SearchResult[]>();

    let allResult = this.searchService.getResults();
    this.searchQuery = this.searchService.getLastQuery();
    this.searchResourceIds = this.searchService.getSelectedResourceIds();

    allResult.forEach((result) => {
      if (map.has(result.resourceId)) {
        map.set(result.resourceId, map.get(result.resourceId).concat(result));
      } else {
        map.set(result.resourceId, [result]);
      }
    });

    this.searchResult = map;
  };

  ngOnDestroy() {
    this.routerHandler.unsubscribe('search', this.onRouterChangeSearch);
    this.routerHandler.unsubscribe('res', this.onRouterChangeResource);
    this.searchService.unsubscribe(this.searchHandler);
  }

  public openDrive(resourceId) {
    sideMenuActiveObserver.next(false);
    if (this.searchResult.has(resourceId)) {
      this.routerHandler.navigate(['/room/', resourceId, 'drive']);
    }
  }

  public openPath(resourceId, result: SearchResult) {
    sideMenuActiveObserver.next(false);
    let fragment = this.routerHandler.getRoute().fragment;
    fragment['path'] = result.dir;
    fragment['file'] = result.file.fullName;
    this.routerHandler.navigate(['/room/', resourceId, 'drive'], {
      fragment: this.routerHandler.fragmentToRaw(fragment),
    });
  }

  public openAllResult() {
    /*let fragment = this.routerHandler.getRoute().fragment;
    fragment['search'] = this.searchQuery;
    this.routerHandler.navigate(['/search/', this.searchResourceId], {
      fragment: this.routerHandler.fragmentToRaw(fragment),
    });*/
  }
}
