import 'dart:io';

import 'package:path/path.dart' as p;

import 'package:namida/controller/lyrics_search_utils/lrc_search_details.dart';
import 'package:namida/youtube/class/youtube_id.dart';
import 'package:namida/youtube/controller/youtube_info_controller.dart';

import 'lrc_search_utils_base.dart';

class LrcSearchUtilsYoutubeID extends LrcSearchUtils {
  final YoutubeID video;
  final String? videoTitle;
  final String? channelTitle;
  final Duration? duration;

  const LrcSearchUtilsYoutubeID(
    this.video, {
    required this.videoTitle,
    required this.channelTitle,
    required this.duration,
  });

  @override
  String? get pickFileInitialDirectory => null;

  @override
  String get initialSearchTextHint => videoTitle ?? '';

  @override
  String get embeddedLyrics => ''; // none

  @override
  File get cachedTxtFile => File(p.join(mainLyricsCacheDirectory, "${video.id}.txt"));

  @override
  File get cachedLRCFile => File(p.join(mainLyricsCacheDirectory, "${video.id}.lrc"));

  @override
  List<File Function()> get deviceLRCFiles => <File Function()>[]; // none

  @override
  Future<int> getItemDurationMS() async {
    final dur = this.duration;
    if (dur != null && dur > Duration.zero) {
      return dur.inMilliseconds;
    }
    final seconds = await YoutubeInfoController.utils.getVideoDurationSeconds(video.id);
    if (seconds == null) return 0;
    return seconds * 1000;
  }

  @override
  List<String> searchQueriesGoogle() {
    if (videoTitle == null) return [];
    return <String>[
      '$videoTitle lyrics',
    ];
  }

  @override
  List<LRCSearchDetails> searchDetailsQueries() {
    final durMS = duration?.inMilliseconds ?? 0;
    return [
      LRCSearchDetails(
        title: videoTitle ?? '',
        artist: channelTitle ?? '',
        album: '',
        durationMS: durMS,
        isDurationModified: false,
      ),
      LRCSearchDetails(
        title: videoTitle ?? '',
        artist: '',
        album: '',
        durationMS: durMS,
        isDurationModified: false,
      ),
    ];
  }
}
