import 'dart:async';
import 'dart:developer' as developer;
import 'dart:io';
import 'dart:isolate';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;

import '../local_storage/key_value_storage.dart';
import '../local_storage/sqflite_localpodcast.dart';
import '../type/episodebrief.dart';

class EpisodeTask {
  final String taskId;
  int progress;
  DownloadTaskStatus status;
  final EpisodeBrief episode;
  EpisodeTask(
    this.episode,
    this.taskId, {
    this.progress = 0,
    this.status = DownloadTaskStatus.undefined,
  });
}

void downloadCallback(String id, DownloadTaskStatus status, int progress) {
  developer.log('Homepage callback task in $id  status ($status) $progress');
  final send = IsolateNameServer.lookupPortByName('downloader_send_port');
  send.send([id, status, progress]);
}

void autoDownloadCallback(String id, DownloadTaskStatus status, int progress) {
  developer
      .log('Autodownload callback task in $id  status ($status) $progress');
  final send = IsolateNameServer.lookupPortByName('auto_downloader_send_port');
  send.send([id, status, progress]);
}

//For background auto downlaod
class AutoDownloader {
  DBHelper dbHelper = DBHelper();
  final List<EpisodeTask> _episodeTasks = [];
  final Completer _completer = Completer();
  AutoDownloader() {
    FlutterDownloader.registerCallback(autoDownloadCallback);
  }

  bindBackgroundIsolate() {
    var _port = ReceivePort();
    var isSuccess = IsolateNameServer.registerPortWithName(
        _port.sendPort, 'auto_downloader_send_port');
    if (!isSuccess) {
      IsolateNameServer.removePortNameMapping('auto_downloader_send_port');
      bindBackgroundIsolate();
      return;
    }
    _port.listen((dynamic data) {
      String id = data[0];
      DownloadTaskStatus status = data[1];
      int progress = data[2];

      for (var episodeTask in _episodeTasks) {
        if (episodeTask.taskId == id) {
          episodeTask.status = status;
          episodeTask.progress = progress;
          if (status == DownloadTaskStatus.complete) {
            _saveMediaId(episodeTask);
          } else if (status == DownloadTaskStatus.failed) {
            _episodeTasks.removeWhere((element) =>
                element.episode.enclosureUrl ==
                episodeTask.episode.enclosureUrl);
            if (_episodeTasks.length == 0) _unbindBackgroundIsolate();
          }
        }
      }
    });
  }

  void _unbindBackgroundIsolate() {
    IsolateNameServer.removePortNameMapping('auto_downloader_send_port');
    _completer?.complete();
  }

  Future _saveMediaId(EpisodeTask episodeTask) async {
    final completeTask = await FlutterDownloader.loadTasksWithRawQuery(
        query: "SELECT * FROM task WHERE task_id = '${episodeTask.taskId}'");
    var filePath =
        'file://${path.join(completeTask.first.savedDir, Uri.encodeComponent(completeTask.first.filename))}';
    var fileStat = await File(
            path.join(completeTask.first.savedDir, completeTask.first.filename))
        .stat();
    await dbHelper.saveMediaId(episodeTask.episode.enclosureUrl, filePath,
        episodeTask.taskId, fileStat.size);
    _episodeTasks.removeWhere((element) =>
        element.episode.enclosureUrl == episodeTask.episode.enclosureUrl);
    if (_episodeTasks.length == 0) _unbindBackgroundIsolate();
  }

  Future startTask(List<EpisodeBrief> episodes,
      {bool showNotification = false}) async {
    for (var episode in episodes) {
      final dir = await getExternalStorageDirectory();
      var localPath = path.join(dir.path, episode.feedTitle);
      final saveDir = Directory(localPath);
      var hasExisted = await saveDir.exists();
      if (!hasExisted) {
        saveDir.create();
      }
      var now = DateTime.now();
      var datePlus = now.year.toString() +
          now.month.toString() +
          now.day.toString() +
          now.second.toString();
      var fileName =
          '${episode.title}$datePlus.${episode.enclosureUrl.split('/').last.split('.').last}';
      var taskId = await FlutterDownloader.enqueue(
        fileName: fileName,
        url: episode.enclosureUrl,
        savedDir: localPath,
        showNotification: showNotification,
        openFileFromNotification: false,
      );
      _episodeTasks.add(EpisodeTask(episode, taskId));
      var dbHelper = DBHelper();
      await dbHelper.saveDownloaded(episode.enclosureUrl, taskId);
    }
    await _completer.future;
    return;
  }
}

//For download episode inside app
class DownloadState extends ChangeNotifier {
  DBHelper dbHelper = DBHelper();
  List<EpisodeTask> _episodeTasks = [];
  List<EpisodeTask> get episodeTasks => _episodeTasks;

  DownloadState() {
    _autoDelete();
    _bindBackgroundIsolate();
    FlutterDownloader.registerCallback(downloadCallback);
  }

  @override
  void addListener(VoidCallback listener) async {
    _loadTasks();
    super.addListener(listener);
  }

  _loadTasks() async {
    _episodeTasks = [];
    var dbHelper = DBHelper();
    var tasks = await FlutterDownloader.loadTasks();
    if (tasks.length != 0) {
      for (var task in tasks) {
        var episode = await dbHelper.getRssItemWithUrl(task.url);
        if (episode == null) {
          await FlutterDownloader.remove(
              taskId: task.taskId, shouldDeleteContent: true);
        } else {
          _episodeTasks.add(EpisodeTask(episode, task.taskId,
              progress: task.progress, status: task.status));
        }
      }
    }
    notifyListeners();
  }

  void _bindBackgroundIsolate() {
    var _port = ReceivePort();
    var isSuccess = IsolateNameServer.registerPortWithName(
        _port.sendPort, 'downloader_send_port');
    if (!isSuccess) {
      _unbindBackgroundIsolate();
      _bindBackgroundIsolate();
      return;
    }

    _port.listen((dynamic data) {
      String id = data[0];
      DownloadTaskStatus status = data[1];
      int progress = data[2];

      for (var episodeTask in _episodeTasks) {
        if (episodeTask.taskId == id) {
          episodeTask.status = status;
          episodeTask.progress = progress;
          if (status == DownloadTaskStatus.complete) {
            _saveMediaId(episodeTask).then((value) {
              notifyListeners();
            });
          } else {
            notifyListeners();
          }
        }
      }
    });
  }

  Future _saveMediaId(EpisodeTask episodeTask) async {
    episodeTask.status = DownloadTaskStatus.complete;
    final completeTask = await FlutterDownloader.loadTasksWithRawQuery(
        query: "SELECT * FROM task WHERE task_id = '${episodeTask.taskId}'");
    var filePath =
        'file://${path.join(completeTask.first.savedDir, Uri.encodeComponent(completeTask.first.filename))}';
    var fileStat = await File(
            path.join(completeTask.first.savedDir, completeTask.first.filename))
        .stat();
    dbHelper.saveMediaId(episodeTask.episode.enclosureUrl, filePath,
        episodeTask.taskId, fileStat.size);
    var episode =
        await dbHelper.getRssItemWithUrl(episodeTask.episode.enclosureUrl);
    _removeTask(episodeTask.episode);
    _episodeTasks.add(EpisodeTask(episode, episodeTask.taskId,
        progress: 100, status: DownloadTaskStatus.complete));
  }

  void _unbindBackgroundIsolate() {
    IsolateNameServer.removePortNameMapping('downloader_send_port');
  }

  EpisodeTask episodeToTask(EpisodeBrief episode) {
    return _episodeTasks
        .firstWhere((task) => task.episode.enclosureUrl == episode.enclosureUrl,
            orElse: () {
      return EpisodeTask(
        episode,
        '',
      );
    });
  }

  @override
  void dispose() {
    _unbindBackgroundIsolate();
    super.dispose();
  }

  Future startTask(EpisodeBrief episode, {bool showNotification = true}) async {
    var dbHelper = DBHelper();
    var isDownloaded = await dbHelper.isDownloaded(episode.enclosureUrl);
    if (!isDownloaded) {
      final dir = await getExternalStorageDirectory();
      var localPath = path.join(dir.path, episode.feedTitle);
      final saveDir = Directory(localPath);
      var hasExisted = await saveDir.exists();
      if (!hasExisted) {
        saveDir.create();
      }
      var now = DateTime.now();
      var datePlus = now.year.toString() +
          now.month.toString() +
          now.day.toString() +
          now.second.toString();
      var fileName =
          '${episode.title}$datePlus.${episode.enclosureUrl.split('/').last.split('.').last}';
      var taskId = await FlutterDownloader.enqueue(
        fileName: fileName,
        url: episode.enclosureUrl,
        savedDir: localPath,
        showNotification: showNotification,
        openFileFromNotification: false,
      );
      _episodeTasks.add(EpisodeTask(episode, taskId));
      await dbHelper.saveDownloaded(episode.enclosureUrl, taskId);
      notifyListeners();
    }
  }

  Future pauseTask(EpisodeBrief episode) async {
    var task = episodeToTask(episode);
    await FlutterDownloader.pause(taskId: task.taskId);
  }

  Future resumeTask(EpisodeBrief episode) async {
    var task = episodeToTask(episode);
    var newTaskId = await FlutterDownloader.resume(taskId: task.taskId);
    var index = _episodeTasks.indexOf(task);
    _removeTask(episode);
    FlutterDownloader.remove(taskId: task.taskId);
    var dbHelper = DBHelper();
    _episodeTasks.insert(index, EpisodeTask(episode, newTaskId));
    await dbHelper.saveDownloaded(episode.enclosureUrl, newTaskId);
  }

  Future retryTask(EpisodeBrief episode) async {
    var task = episodeToTask(episode);
    var newTaskId = await FlutterDownloader.retry(taskId: task.taskId);
    await FlutterDownloader.remove(taskId: task.taskId);
    var index = _episodeTasks.indexOf(task);
    _removeTask(episode);
    var dbHelper = DBHelper();
    _episodeTasks.insert(index, EpisodeTask(episode, newTaskId));
    await dbHelper.saveDownloaded(episode.enclosureUrl, newTaskId);
  }

  Future removeTask(EpisodeBrief episode) async {
    var task = episodeToTask(episode);
    await FlutterDownloader.remove(
        taskId: task.taskId, shouldDeleteContent: false);
  }

  Future delTask(EpisodeBrief episode) async {
    var task = episodeToTask(episode);
    await FlutterDownloader.remove(
        taskId: task.taskId, shouldDeleteContent: true);
    await dbHelper.delDownloaded(episode.enclosureUrl);

    for (var episodeTask in _episodeTasks) {
      if (episodeTask.taskId == task.taskId) {
        episodeTask.status = DownloadTaskStatus.undefined;
      }
      notifyListeners();
    }
    _removeTask(episode);
  }

  _removeTask(EpisodeBrief episode) {
    _episodeTasks.removeWhere((element) => element.episode == episode);
    notifyListeners();
  }

  _autoDelete() async {
    developer.log('Start auto delete outdated episodes');
    var autoDeleteStorage = KeyValueStorage(autoDeleteKey);
    var autoDelete = await autoDeleteStorage.getInt();
    if (autoDelete == 0) {
      await autoDeleteStorage.saveInt(30);
    } else if (autoDelete > 0) {
      var deadline = DateTime.now()
          .subtract(Duration(days: autoDelete))
          .millisecondsSinceEpoch;
      var episodes = await dbHelper.getOutdatedEpisode(deadline);
      if (episodes.isNotEmpty) {
        for (var episode in episodes) {
          await delTask(episode);
        }
      }
      final tasks = await FlutterDownloader.loadTasksWithRawQuery(
          query:
              'SELECT * FROM task WHERE time_created < $deadline AND status = 3');
      for (var task in tasks) {
        FlutterDownloader.remove(
            taskId: task.taskId, shouldDeleteContent: true);
      }
    }
  }
}
