import { Component, ChangeDetectorRef } from '@angular/core'
import { DownloadService } from '../../../core/services/download.service'
import { ElectronService } from '../../../core/services/electron.service'
import { groupBy } from '../../../../electron/shared/UtilFunctions'
import { VersionResult } from '../../../../electron/shared/interfaces/songDetails.interface'
import { SearchService } from '../../../core/services/search.service'
import { SelectionService } from '../../../core/services/selection.service'

@Component({
  selector: 'app-status-bar',
  templateUrl: './status-bar.component.html',
  styleUrls: ['./status-bar.component.scss']
})
export class StatusBarComponent {

  resultCount = 0
  downloading = false
  percent = 0
  batchResults: VersionResult[]
  chartGroups: VersionResult[][]

  constructor(
    private electronService: ElectronService,
    private downloadService: DownloadService,
    private searchService: SearchService,
    private selectionService: SelectionService,
    ref: ChangeDetectorRef
  ) {
    downloadService.onDownloadUpdated(() => {
      setTimeout(() => { // Make sure this is the last callback executed to get the accurate downloadCount
        this.downloading = downloadService.downloadCount > 0
        this.percent = downloadService.totalPercent
        ref.detectChanges()
      }, 0)
    })

    searchService.onSearchChanged(() => {
      this.resultCount = searchService.resultCount
    })
  }

  get allResultsVisible() {
    return this.searchService.allResultsVisible
  }

  get selectedResults() {
    return this.selectionService.getSelectedResults()
  }

  showDownloads() {
    $('#downloadsModal').modal('show')
  }

  async downloadSelected() {
    this.chartGroups = []
    this.batchResults = await this.electronService.invoke('batch-song-details', this.selectedResults.map(result => result.id))
    const versionGroups = groupBy(this.batchResults, 'songID')
    for (const versionGroup of versionGroups) {
      if (versionGroup.findIndex(version => version.chartID != versionGroup[0].chartID) != -1) {
        // Must have multiple charts of this song
        this.chartGroups.push(versionGroup.filter(version => version.versionID == version.latestVersionID))
      }
    }

    if (this.chartGroups.length == 0) {
      for (const versions of versionGroups) {
        const downloadVersion = versions.find(version => version.versionID == version.latestVersionID)
        const downloadSong = this.selectedResults.find(song => song.id == downloadVersion.songID)
        this.downloadService.addDownload(
          downloadVersion.versionID, {
            avTagName: downloadVersion.avTagName,
            artist: downloadSong.artist,
            charter: downloadVersion.charters,
            driveData: downloadVersion.driveData
          })
      }
    } else {
      $('#selectedModal').modal('show')
      // [download all charts for each song] [deselect these songs] [X]
    }
  }

  downloadAllCharts() {
    for (const version of this.batchResults) {
      if (version.versionID != version.latestVersionID) { continue }
      const downloadSong = this.selectedResults.find(song => song.id == version.songID)
      this.downloadService.addDownload(
        version.versionID, {
          avTagName: version.avTagName,
          artist: downloadSong.artist,
          charter: version.charters,
          driveData: version.driveData
        })
    }
  }

  deselectSongsWithMultipleCharts() {
    for (const chartGroup of this.chartGroups) {
      this.selectionService.deselectSong(chartGroup[0].songID)
    }
  }
}