import { Component, OnInit, ElementRef, ViewChild, ViewChildren, QueryList } from '@angular/core';
import {PostsService} from '../posts.services';
import { fromEvent, Subject } from 'rxjs';
import {UntypedFormControl, Validators} from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { MatSnackBar } from '@angular/material/snack-bar';
import { saveAs } from 'file-saver';
import { YoutubeSearchService, Result } from '../youtube-search.service';
import { Router, ActivatedRoute } from '@angular/router';
import { Platform } from '@angular/cdk/platform';
import { ArgModifierDialogComponent } from 'app/dialogs/arg-modifier-dialog/arg-modifier-dialog.component';
import { RecentVideosComponent } from 'app/components/recent-videos/recent-videos.component';
import { DatabaseFile, Download, FileType, Playlist } from 'api-types';
import { debounceTime, filter, map, switchMap, take, tap } from 'rxjs/operators';

@Component({
    selector: 'app-root',
    templateUrl: './main.component.html',
    styleUrls: ['./main.component.css'],
    standalone: false
})
export class MainComponent implements OnInit {
  youtubeAuthDisabledOverride = false;

  iOS = false;

  // local settings
  determinateProgress = false;
  downloadingfile = false;
  audioOnly: boolean;
  autoplay = false;
  customArgsEnabled = false;
  customArgs = null;
  customOutputEnabled = false;
  replaceArgs = false;
  customOutput = null;
  youtubeAuthEnabled = false;
  youtubeUsername = null;
  youtubePassword = null;
  cropFile = false;
  cropFileStart = null;
  cropFileEnd = null;
  urlError = false;
  path: string | string[] = '';
  url = '';
  exists = '';
  percentDownloaded: number;
  autoStartDownload = false;

  // global settings
  fileManagerEnabled = false;
  allowQualitySelect = false;
  downloadOnlyMode = false;
  forceAutoplay = false;
  globalCustomArgs = null;
  allowAdvancedDownload = false;
  useDefaultDownloadingAgent = true;
  customDownloadingAgent = null;

  // cache
  cachedAvailableFormats: { [key: string]: any } = Object.create(null);
  cachedFileManagerEnabled = localStorage.getItem('cached_filemanager_enabled') === 'true';

  // youtube api
  youtubeSearchEnabled = false;
  youtubeAPIKey = null;
  results_loading = false;
  results_showing = true;
  results = [];

  playlists = {'audio': [], 'video': []};
  playlist_thumbnails = {};
  downloads: Download[] = [];
  download_uids: string[] = [];
  current_download: Download = null;

  urlForm = new UntypedFormControl('', [Validators.required]);

  qualityOptions = {
    'video': [
      {
        'resolution': '3840x2160',
        'value': '2160',
        'label': '2160p (4K)'
      },
      {
        'resolution': '2560x1440',
        'value': '1440',
        'label': '1440p'
      },
      {
        'resolution': '1920x1080',
        'value': '1080',
        'label': '1080p'
      },
      {
        'resolution': '1280x720',
        'value': '720',
        'label': '720p'
      },
      {
        'resolution': '720x480',
        'value': '480',
        'label': '480p'
      },
      {
        'resolution': '480x360',
        'value': '360',
        'label': '360p'
      },
      {
        'resolution': '360x240',
        'value': '240',
        'label': '240p'
      },
      {
        'resolution': '256x144',
        'value': '144',
        'label': '144p'
      }
    ],
    'audio': [
      // TODO: implement
      // {
      //   'kbitrate': '256',
      //   'value': '256K',
      //   'label': '256 Kbps'
      // },
      // {
      //   'kbitrate': '160',
      //   'value': '160K',
      //   'label': '160 Kbps'
      // },
      // {
      //   'kbitrate': '128',
      //   'value': '128K',
      //   'label': '128 Kbps'
      // },
      // {
      //   'kbitrate': '96',
      //   'value': '96K',
      //   'label': '96 Kbps'
      // },
      // {
      //   'kbitrate': '70',
      //   'value': '70K',
      //   'label': '70 Kbps'
      // },
      // {
      //   'kbitrate': '50',
      //   'value': '50K',
      //   'label': '50 Kbps'
      // },
      // {
      //   'kbitrate': '32',
      //   'value': '32K',
      //   'label': '32 Kbps'
      // }
    ]
  }

  selectedMaxQuality = '';
  selectedQuality: string | unknown = '';
  formats_loading = false;

  @ViewChild('urlinput', { read: ElementRef }) urlInput: ElementRef;
  @ViewChild('recentVideos') recentVideos: RecentVideosComponent;
  last_valid_url = '';
  last_url_check = 0;

  argsChangedSubject: Subject<boolean> = new Subject<boolean>();
  simulatedOutput = '';

  interval_id = null;

  constructor(public postsService: PostsService, private youtubeSearch: YoutubeSearchService, public snackBar: MatSnackBar,
    private router: Router, public dialog: MatDialog, private platform: Platform, private route: ActivatedRoute) {
    this.audioOnly = false;
  }

  async configLoad(): Promise<void> {
    await this.loadConfig();
    if (this.autoStartDownload) {
      this.downloadClicked();
    }
  }

  async loadConfig(): Promise<boolean> {
    // loading config
    this.fileManagerEnabled = this.postsService.config['Extra']['file_manager_enabled']
                              && this.postsService.hasPermission('filemanager');
    this.downloadOnlyMode = this.postsService.config['Extra']['download_only_mode'];
    this.forceAutoplay = this.postsService.config['Extra']['force_autoplay'];
    this.globalCustomArgs = this.postsService.config['Downloader']['custom_args'];
    this.youtubeSearchEnabled = this.postsService.config['API'] && this.postsService.config['API']['use_youtube_API'] &&
        this.postsService.config['API']['youtube_API_key'];
    this.youtubeAPIKey = this.youtubeSearchEnabled ? this.postsService.config['API']['youtube_API_key'] : null;
    this.allowQualitySelect = this.postsService.config['Extra']['allow_quality_select'];
    this.allowAdvancedDownload = this.postsService.config['Advanced']['allow_advanced_download']
                                  && this.postsService.hasPermission('advanced_download');
    this.useDefaultDownloadingAgent = this.postsService.config['Advanced']['use_default_downloading_agent'];
    this.customDownloadingAgent = this.postsService.config['Advanced']['custom_downloading_agent'];

    // set final cache items

    localStorage.setItem('cached_filemanager_enabled', this.fileManagerEnabled.toString());
    this.cachedFileManagerEnabled = this.fileManagerEnabled;

    if (this.allowAdvancedDownload) {
      if (localStorage.getItem('customArgsEnabled') !== null) {
        this.customArgsEnabled = localStorage.getItem('customArgsEnabled') === 'true';
      }

      if (localStorage.getItem('customOutputEnabled') !== null) {
        this.customOutputEnabled = localStorage.getItem('customOutputEnabled') === 'true';
      }

      if (localStorage.getItem('replaceArgs') !== null) {
        this.replaceArgs = localStorage.getItem('replaceArgs') === 'true';
      }

      if (localStorage.getItem('youtubeAuthEnabled') !== null) {
        this.youtubeAuthEnabled = localStorage.getItem('youtubeAuthEnabled') === 'true';
      }

      // set advanced inputs
      const customArgs = localStorage.getItem('customArgs');
      const customOutput = localStorage.getItem('customOutput');
      const youtubeUsername = localStorage.getItem('youtubeUsername');

      if (customArgs && customArgs !== 'null') { this.customArgs = customArgs }
      if (customOutput && customOutput !== 'null') { this.customOutput = customOutput }
      if (youtubeUsername && youtubeUsername !== 'null') { this.youtubeUsername = youtubeUsername }

      this.getSimulatedOutput();
    }

    // get downloads routine
    if (this.interval_id) { clearInterval(this.interval_id) }
    this.interval_id = setInterval(() => {
      if (this.current_download) {
        this.getCurrentDownload();
      }
    }, 1000);

    return true;
  }

  // app initialization.
  ngOnInit(): void {
    if (this.postsService.initialized) {
      this.configLoad();
    } else {
      this.postsService.service_initialized
        .pipe(filter(Boolean), take(1))
        .subscribe(() => this.configLoad());
    }

    this.postsService.config_reloaded.subscribe(changed => {
      if (changed) {
        this.loadConfig();
      }
    });

    this.iOS = this.platform.IOS;

    // get checkboxes
    if (localStorage.getItem('audioOnly') !== null) {
      this.audioOnly = localStorage.getItem('audioOnly') === 'true';
    }

    this.autoplay = this.forceAutoplay;
    if (!this.forceAutoplay && localStorage.getItem('autoplay') !== null) {
      this.autoplay = localStorage.getItem('autoplay') === 'true';
    }

    // check if params exist
    if (this.route.snapshot.paramMap.get('url')) {
      this.url = decodeURIComponent(this.route.snapshot.paramMap.get('url'));
      this.audioOnly = this.route.snapshot.paramMap.get('audioOnly') === 'true';

      // set auto start flag to true
      this.autoStartDownload = true;
    }

    this.argsChangedSubject
      .pipe(debounceTime(500))
      .subscribe((should_simulate) => {
        if (should_simulate) this.getSimulatedOutput();
    });
  }

  ngAfterViewInit(): void {
    if (this.youtubeSearchEnabled && this.youtubeAPIKey) {
      this.youtubeSearch.initializeAPI(this.youtubeAPIKey);
      this.attachToInput();
    }
  }

  ngOnDestroy(): void {
    if (this.interval_id) { clearInterval(this.interval_id) }
  }

  // download helpers
  downloadHelper(container: DatabaseFile | Playlist, type: string, is_playlist = false, force_view = false, navigate_mode = false): void {
    this.downloadingfile = false;
    if (!this.autoplay && !this.downloadOnlyMode && !navigate_mode) {
      // do nothing
      this.reloadRecentVideos(is_playlist);
    } else {
      // if download only mode, just download the file. no redirect
      if (force_view === false && this.downloadOnlyMode && !this.iOS) {
        if (is_playlist) {
          this.downloadPlaylist(container['uid']);
        } else {
          this.downloadFileFromServer(container as DatabaseFile, type);
        }
        this.reloadRecentVideos(is_playlist);
      } else {
        localStorage.setItem('player_navigator', this.router.url.split(';')[0]);
        if (is_playlist) {
          this.router.navigate(['/player', {playlist_id: container['id'], type: type}]);
        } else {
          this.router.navigate(['/player', {type: type, uid: container['uid']}]);
        }
      }
    }
  }

  // download click handler
  downloadClicked(): void {
    // Sanitize single YouTube watch URLs (keep only v=...)
    const _urlsForSanitize = this.getURLArray(this.url || '');
    if (_urlsForSanitize.length === 1) {
      const _sanitized = this.sanitizeYouTubeWatchUrl(_urlsForSanitize[0]);
      if (_sanitized && _sanitized !== _urlsForSanitize[0]) {
        this.url = _sanitized;
      }
    }
    if (!this.ValidURL(this.url)) {
      this.urlError = true;
      return;
    }

    this.urlError = false;

    // get common args
    const customArgs = (this.customArgsEnabled && this.replaceArgs ? this.customArgs : null);
    const additionalArgs = (this.customArgsEnabled && !this.replaceArgs ? this.customArgs : null);
    const customOutput = (this.customOutputEnabled ? this.customOutput : null);
    const youtubeUsername = (this.youtubeAuthEnabled && this.youtubeUsername ? this.youtubeUsername : null);
    const youtubePassword = (this.youtubeAuthEnabled && this.youtubePassword ? this.youtubePassword : null);

    // set advanced inputs
    if (this.allowAdvancedDownload) {
      if (customArgs) {
        localStorage.setItem('customArgs', customArgs);
      }
      if (customOutput) {
        localStorage.setItem('customOutput', customOutput);
      }
      if (youtubeUsername) {
        localStorage.setItem('youtubeUsername', youtubeUsername);
      }
    }

    const type = this.audioOnly ? 'audio' : 'video';

    const customQualityConfiguration = type === 'audio' ? this.getSelectedAudioFormat() : this.getSelectedVideoFormat();

    let cropFileSettings = null;

    if (this.cropFile) {
      cropFileSettings = {
        cropFileStart: this.cropFileStart,
        cropFileEnd: this.cropFileEnd
      }
    }

    const selected_quality = this.selectedQuality;
    this.selectedQuality = '';
    this.downloadingfile = true;

    const urls = this.getURLArray(this.url);
    for (let i = 0; i < urls.length; i++) {
      const url = this.sanitizeYouTubeWatchUrl(urls[i]);
      this.postsService.downloadFile(url, type as FileType, (customQualityConfiguration || selected_quality === '' || typeof selected_quality !== 'string' ? null : selected_quality),
        customQualityConfiguration, customArgs, additionalArgs, customOutput, youtubeUsername, youtubePassword, cropFileSettings).subscribe(res => {
          this.current_download = res['download'];
          this.downloads.push(res['download']);
          this.download_uids.push(res['download']['uid']);
      }, () => { // can't access server
        this.downloadingfile = false;
        this.current_download = null;
        this.postsService.openSnackBar($localize`Download failed!`, 'OK.');
      });

      if (!this.autoplay && urls.length === 1) {
          const download_queued_message = $localize`Download for ${url}:url: has been queued!`;
          this.postsService.openSnackBar(download_queued_message);
          this.url = '';
          this.downloadingfile = false;
      }
    }
  }

  // download canceled handler
  cancelDownload(download_to_cancel = null): void {
    // if one is provided, cancel that one. otherwise, remove the current one
    if (download_to_cancel) {
      this.removeDownloadFromCurrentDownloads(download_to_cancel)
      return;
    }
    this.downloadingfile = false;
    this.current_download = null;
  }

  getSelectedAudioFormat(): string {
    if (typeof this.selectedQuality === 'string') { return null; }
    const cachedFormatsExists = this.cachedAvailableFormats[this.url] && this.cachedAvailableFormats[this.url]['formats'];
    if (cachedFormatsExists) {
      return this.selectedQuality['format_id'];
    } else {
      return null;
    }
  }

  getSelectedVideoFormat(): string {
    if (typeof this.selectedQuality === 'string') { return null; }
    const cachedFormats = this.cachedAvailableFormats[this.url] && this.cachedAvailableFormats[this.url]['formats'];
    if (cachedFormats) {
      if (this.selectedQuality) {
        let selected_video_format = this.selectedQuality['format_id'];
        // add in audio format if necessary
        const audio_missing = !this.selectedQuality['acodec'] || this.selectedQuality['acodec'] === 'none';
        if (audio_missing && cachedFormats['best_audio_format']) selected_video_format += `+${cachedFormats['best_audio_format']}`;
        return selected_video_format;
      }
    }
    return null;
  }

  getDownloadByUID(uid: string): Download {
    const index = this.downloads.findIndex(download => download.uid === uid);
    if (index !== -1) {
      return this.downloads[index];
    } else {
      return null;
    }
  }

  removeDownloadFromCurrentDownloads(download_to_remove: Download): boolean {
    if (this.current_download === download_to_remove) {
      this.current_download = null;
    }
    const index = this.downloads.indexOf(download_to_remove);
    if (index !== -1) {
      this.downloads.splice(index, 1);
      return true;
    } else {
      return false;
    }
  }

  downloadFileFromServer(file: DatabaseFile, type: string): void {
    const ext = type === 'audio' ? 'mp3' : 'mp4'
    this.postsService.downloadFileFromServer(file.uid).subscribe(res => {
      const blob: Blob = res;
      saveAs(blob, decodeURIComponent(file.id) + `.${ext}`);

      if (!this.fileManagerEnabled) {
        // tell server to delete the file once downloaded
        this.postsService.deleteFile(file.uid).subscribe(() => {});
      }
    });
  }

  downloadPlaylist(playlist: Playlist): void {
    this.postsService.downloadPlaylistFromServer(playlist.id).subscribe(res => {
      const blob: Blob = res;
      saveAs(blob, playlist.name + '.zip');
    });

  }

  clearInput(): void {
    this.url = '';
    this.results_showing = false;
  }

  onInputBlur(): void {
    this.results_showing = false;
  }

  visitURL(url: string): void {
    window.open(url);
  }

  useURL(url: string): void {
    this.results_showing = false;
    this.url = url;
  }

  inputChanged(new_val: string): void {
    this.selectedQuality = '';

    // Sanitize YouTube watch URLs (keep only v=...) so a \"video-from-playlist\" link
    // doesn't get treated like a playlist by the downloader.
    const urls = this.getURLArray(new_val || '');
    if (urls.length === 1) {
      const sanitized = this.sanitizeYouTubeWatchUrl(urls[0]);
      if (sanitized && sanitized !== urls[0]) {
        this.url = sanitized;
        new_val = sanitized;
      }
    }
    if (new_val === '' || !new_val) {
      this.results_showing = false;
    } else {
      if (this.ValidURL(new_val)) {
        this.results_showing = false;
      }
    }
  }

  // checks if url is a valid URL
  ValidURL(str: string): boolean {
    // mark multiple urls as valid but don't get additional info
    const urls = this.getURLArray(str);
    if (urls.length > 1) {
      this.autoplay = false;
      return true;
    }
    
    // tslint:disable-next-line: max-line-length
    const strRegex = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/;
    const re = new RegExp(strRegex);
    const valid = re.test(str);

    if (!valid) { return false; }

    // tslint:disable-next-line: max-line-length
    const youtubeStrRegex = /(?:http(?:s)?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'<> #]+)/;
    const reYT = new RegExp(youtubeStrRegex);
    const ytValid = true;
    if (valid && ytValid && Date.now() - this.last_url_check > 1000) {
      if (str !== this.last_valid_url && this.allowQualitySelect) {
        // get info
        this.getURLInfo(str);
        this.argsChanged();
      }
      this.last_valid_url = str;
    }
    return valid;
  }

  getURLInfo(url: string): void {
    if (!this.cachedAvailableFormats[url]) {
      this.cachedAvailableFormats[url] = Object.create(null);
    }
    // If URL is a YouTube playlist page (not a single watch?v=...), skip format probing
    if (this.isYouTubePlaylistUrl(url)) {
      // make it think that formats errored so that users have options
      this.cachedAvailableFormats[url]['formats_loading'] = false;
      this.cachedAvailableFormats[url]['formats_failed'] = true;
      return;
    }
    if (!(this.cachedAvailableFormats[url] && this.cachedAvailableFormats[url]['formats'])) {
      this.cachedAvailableFormats[url]['formats_loading'] = true;
      this.postsService.getFileFormats(url).subscribe(res => {
        this.cachedAvailableFormats[url]['formats_loading'] = false;
        const infos = res['result'];
        if (!infos || !infos.formats) {
          this.errorFormats(url);
          return;
        }
        this.cachedAvailableFormats[url]['formats'] = this.getAudioAndVideoFormats(infos.formats);
      }, () => {
        this.errorFormats(url);
      });
    }
  }

  private safeParseURL(raw: string): URL | null {
    try {
      const hasScheme = /^[a-zA-Z][a-zA-Z0-9+\-.]*:\/\//.test(raw);
      return new URL(hasScheme ? raw : `https://${raw}`);
    } catch {
      return null;
    }
  }

  // If this is a YouTube watch URL (or youtu.be), return a canonical single-video URL with ONLY v=
  // Otherwise return the input unchanged.
  private sanitizeYouTubeWatchUrl(raw: string): string {
    const u = this.safeParseURL(raw);
    if (!u) return raw;

    const host = u.hostname.replace(/^www\./, '').toLowerCase();
    const isYouTubeHost = host === 'youtube.com' || host === 'm.youtube.com' || host === 'music.youtube.com' || host === 'youtu.be';
    if (!isYouTubeHost) return raw;

    let videoId: string;

    if (host === 'youtu.be') {
      videoId = u.pathname.replace(/^\/+/, '').split('/')[0] || '';
    } else {
      videoId = u.searchParams.get('v') || '';

      // Support common alternate URL shapes
      if (!videoId && u.pathname.startsWith('/shorts/')) {
        const parts = u.pathname.split('/').filter(Boolean);
        videoId = parts[1] || '';
      }
      if (!videoId) {
        const parts = u.pathname.split('/').filter(Boolean);
        if ((parts[0] === 'embed' || parts[0] === 'v' || parts[0] === 'vi') && parts[1]) {
          videoId = parts[1];
        }
      }
    }

    if (!videoId) return raw;
    return `https://www.youtube.com/watch?v=${videoId}`;
  }

  // Detect actual playlist pages / pure playlist links.
  // NOTE: watch?v=...&list=... is treated as a single video (we sanitize it above).
  private isYouTubePlaylistUrl(raw: string): boolean {
    const u = this.safeParseURL(raw);
    if (!u) {
      return raw.includes('youtube.com/playlist') || raw.includes('/playlist?');
    }

    const host = u.hostname.replace(/^www\./, '').toLowerCase();
    const isYouTubeHost = host === 'youtube.com' || host === 'm.youtube.com' || host === 'music.youtube.com';
    if (!isYouTubeHost) return false;

    const sp = u.searchParams;

    // https://www.youtube.com/playlist?list=...
    if (u.pathname.startsWith('/playlist') && sp.has('list')) return true;

    // Any URL with list= but no v= is effectively a playlist request
    if (sp.has('list') && !sp.has('v')) return true;

    return false;
  }


  getSimulatedOutput(): void {
    const urls = this.getURLArray(this.url);
    if (urls.length > 1) return;

    // this function should be very similar to downloadClicked()
    const customArgs = (this.customArgsEnabled && this.replaceArgs ? this.customArgs : null);
    const additionalArgs = (this.customArgsEnabled && !this.replaceArgs ? this.customArgs : null);
    const customOutput = (this.customOutputEnabled ? this.customOutput : null);
    const youtubeUsername = (this.youtubeAuthEnabled && this.youtubeUsername ? this.youtubeUsername : null);
    const youtubePassword = (this.youtubeAuthEnabled && this.youtubePassword ? this.youtubePassword : null);

    const type = this.audioOnly ? 'audio' : 'video';

    const customQualityConfiguration = type === 'audio' ? this.getSelectedAudioFormat() : this.getSelectedVideoFormat();

    let cropFileSettings = null;

    if (this.cropFile) {
      cropFileSettings = {
        cropFileStart: this.cropFileStart,
        cropFileEnd: this.cropFileEnd
      }
    }

    this.postsService.generateArgs(this.url, type as FileType, (customQualityConfiguration || this.selectedQuality === '' || typeof this.selectedQuality !== 'string' ? null : this.selectedQuality),
      customQualityConfiguration, customArgs, additionalArgs, customOutput, youtubeUsername, youtubePassword, cropFileSettings).subscribe(res => {
        const simulated_args = res['args'];
        if (simulated_args) {
          // hide password if needed
          const passwordIndex = simulated_args.indexOf('--password');
          if (passwordIndex !== -1 && passwordIndex !== simulated_args.length - 1) {
            simulated_args[passwordIndex + 1] = simulated_args[passwordIndex + 1].replace(/./g, '*');
          }
          const downloader = this.postsService.config.Advanced.default_downloader;
          this.simulatedOutput = `${downloader} ${this.url} ${simulated_args.join(' ')}`;
        }
    });
  }

  errorFormats(url: string): void {
    this.cachedAvailableFormats[url]['formats_loading'] = false;
    this.cachedAvailableFormats[url]['formats_failed'] = true;
    console.error('Could not load formats for url ' + url);
  }

  attachToInput(): void {
    fromEvent(this.urlInput.nativeElement, 'keyup')
      .pipe(
        map((e: any) => e.target.value),            // extract the value of input
        filter((text: string) => text.length > 1),  // filter out if empty
        debounceTime(250),                          // only once every 250ms
        tap(() => this.results_loading = true),     // enable loading
        switchMap((query: string) => this.youtubeSearch.search(query))
      )
      .subscribe(
        (results: Result[]) => {
          this.results_loading = false;
          if (this.url !== '' && results && results.length > 0) {
            this.results = results;
            this.results_showing = true;
          } else {
            this.results_showing = false;
          }
        },
        (err: any) => {
          console.log(err)
          this.results_loading = false;
          this.results_showing = false;
        },
        () => { // on completion
          this.results_loading = false;
        }
      );
  }

  argsChanged(): void {
    this.argsChangedSubject.next(true);
  }

  videoModeChanged(new_val): void {
    this.selectedQuality = '';
    localStorage.setItem('audioOnly', new_val.checked.toString());
    this.argsChanged();
  }

  autoplayChanged(new_val): void {
    localStorage.setItem('autoplay', new_val.checked.toString());
  }

  customArgsEnabledChanged(new_val): void {
    localStorage.setItem('customArgsEnabled', new_val.checked.toString());
    this.argsChanged();
  }

  replaceArgsChanged(new_val): void {
    localStorage.setItem('replaceArgs', new_val.checked.toString());
    this.argsChanged();
  }

  customOutputEnabledChanged(new_val): void {
    localStorage.setItem('customOutputEnabled', new_val.checked.toString());
    this.argsChanged();
  }

  youtubeAuthEnabledChanged(new_val): void {
    localStorage.setItem('youtubeAuthEnabled', new_val.checked.toString());
    this.argsChanged();
  }

  getAudioAndVideoFormats(formats): void {
    const audio_formats: any = {};
    const video_formats: any = {};

    for (let i = 0; i < formats.length; i++) {
      const format_obj = {type: null};

      const format = formats[i];
      const format_type = (format.vcodec === 'none') ? 'audio' : 'video';

      format_obj.type = format_type;
      if (format_obj.type === 'audio' && format.abr) {
        const key = format.abr.toString() + 'K';
        format_obj['key'] = key;
        format_obj['bitrate'] = format.abr;
        format_obj['format_id'] = format.format_id;
        format_obj['ext'] = format.ext;
        format_obj['label'] = key;
        format_obj['expected_filesize'] = format.filesize ? format.filesize : (format.filesize_approx || null);

        // don't overwrite if not m4a
        if (audio_formats[key]) {
          if (format.ext === 'm4a') {
            audio_formats[key] = format_obj;
          }
        } else {
          audio_formats[key] = format_obj;
        }
      } else if (format_obj.type === 'video') {
        // check if video format is mp4
        const key = `${format.height}p${Math.round(format.fps)}`;
        if (format.ext === 'mp4' || format.ext === 'mkv' || format.ext === 'webm') {
          format_obj['key'] = key;
          format_obj['height'] = format.height;
          format_obj['acodec'] = format.acodec;
          format_obj['format_id'] = format.format_id;
          format_obj['label'] = key;
          format_obj['fps'] = Math.round(format.fps);
          format_obj['expected_filesize'] = format.filesize ? format.filesize : (format.filesize_approx || null);

          // no acodec means no overwrite
          if (!(video_formats[key]) || format_obj['acodec'] !== 'none') {
            video_formats[key] = format_obj;
          }
        }
      }
    }

    const parsed_formats: any = {};

    parsed_formats['best_audio_format'] = this.getBestAudioFormatForMp4(audio_formats);

    // add audio file size to the expected video file size -- but only if best_audio_format will be used (i.e. when the video has no acodec already). if acodec is present expected filesize will include it
    for (const video_format of Object.values(video_formats)) {
      if ((!video_format['acodec'] || video_format['acodec'] === 'none')
        && video_format['expected_filesize']
        && parsed_formats['best_audio_format']?.filesize) 
          video_format['expected_filesize'] += parsed_formats['best_audio_format'].filesize;
    }

    parsed_formats['video'] = Object.values(video_formats);
    parsed_formats['audio'] = Object.values(audio_formats);

    parsed_formats['video'] = parsed_formats['video'].sort((a, b) => b.height - a.height || b.fps - a.fps);
    parsed_formats['audio'] = parsed_formats['audio'].sort((a, b) => b.bitrate - a.bitrate);

    return parsed_formats;
  }

  getBestAudioFormatForMp4(audio_formats): void {
    let best_audio_format_for_mp4 = null;
    let best_audio_format_bitrate = 0;
    const available_audio_format_keys = Object.keys(audio_formats);
    for (let i = 0; i < available_audio_format_keys.length; i++) {
      const audio_format_key = available_audio_format_keys[i];
      const audio_format = audio_formats[audio_format_key];
      const is_m4a = audio_format.ext === 'm4a';
      if (is_m4a && audio_format.bitrate > best_audio_format_bitrate) {
        best_audio_format_for_mp4 = audio_format.format_id;
        best_audio_format_bitrate = audio_format.bitrate;
      }
    }
    return best_audio_format_for_mp4;
  }

  // modify custom args
  openArgsModifierDialog(): void {
    const dialogRef = this.dialog.open(ArgModifierDialogComponent, {
      data: {
       initial_args: this.customArgs
      }
    });
    dialogRef.afterClosed().subscribe(new_args => {
      if (new_args !== null && new_args !== undefined) {
        this.customArgs = new_args;
      }
    });
  }

  getCurrentDownload(): void {
    if (!this.current_download) {
      return;
    }
    this.postsService.getCurrentDownload(this.current_download['uid']).subscribe(res => {
      if (res['download']) {
        this.current_download = res['download'];
        this.percentDownloaded = this.current_download.percent_complete;

        if (this.current_download['finished'] && !this.current_download['error']) {
          const container = this.current_download['container'];
          const is_playlist = this.current_download['file_uids'].length > 1;
          const type = this.current_download['type'];
          this.current_download = null;  
          this.downloadHelper(container, type, is_playlist, false);
        } else if (this.current_download['finished'] && this.current_download['error']) {
          this.downloadingfile = false;
          this.current_download = null;
          this.postsService.openSnackBar($localize`Download failed!`, 'OK.');
        }
      } else {
        // console.log('failed to get new download');
      }
    });
  }

  reloadRecentVideos(is_playlist = false): void {
    this.postsService.files_changed.next(true);
    if (is_playlist) this.postsService.playlists_changed.next(true);
  }

  getURLArray(url_str: string): Array<string> {
    let lines = url_str.split('\n');
    lines = lines.filter(line => line);
    return lines;
  }

    /**
   * Format bytes as human-readable text.
   * From: https://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable-string
   * 
   * @param bytes Number of bytes.
   * @param si True to use metric (SI) units, aka powers of 1000. False to use 
   *           binary (IEC), aka powers of 1024.
   * @param dp Number of decimal places to display.
   * 
   * @return Formatted string.
   */
  humanFileSize(bytes: number, si=true, dp=1) {
    const thresh = si ? 1000 : 1024;

    if (Math.abs(bytes) < thresh) {
      return bytes + ' B';
    }

    const units = si 
      ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] 
      : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
    let u = -1;
    const r = 10**dp;

    do {
      bytes /= thresh;
      ++u;
    } while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);


    return bytes.toFixed(dp) + ' ' + units[u];
  }
}
