import 'dart:async';
import 'dart:io';

import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:wallpaper/routes/app_routes.dart';
import 'package:wallpaper/utils/dbmanager.dart';
import 'package:wallpaper/utils/downloadmanager.dart';
import 'package:wallpaper/utils/local_img_manager.dart';

class DownloadPageController extends GetxController {
  static DownloadPageController get to => Get.find();
  var dataItems = <LocalImageInfo>[].obs;
  // ignore: prefer_typing_uninitialized_variables
  var localDir = RxString("");
  var isAndroid = Platform.isAndroid;
  @override
  Future <void> onInit() async{
    super.onInit();
    localDir.value = await LocalImgManager.getImgLocalDir();
    reloadList();
  }

  
  void reloadList()  {
    dataItems.value = DBManager.allImgs();
  }

   Future < void>  saveImgToAlbum(String imgUrl) async {

    //应当调用本地的图片方法
      await DownloadManager.saveLocalImage(imgUrl);

      //给结果提示框
      EasyLoading.showToast("Saved to Photos");
    }

    Future <void> setWallpaper(String imgUrl) async {
      //应当调用本地的图片方法
      await DownloadManager.setWallpaperNative(imgUrl);
      EasyLoading.showToast("Wallpaper set successfully");
    }

    //删除图片
    void deleteWallpaper(String imgUrl,int index) async{
      await DownloadManager.deleteWallpaperFile(imgUrl);
      DBManager.deleteImg(dataItems[index]);
      // ignore: invalid_use_of_protected_member
      dataItems.removeAt(index);
      EasyLoading.showToast("Image deleted");
    }

    void goLocalScanPage(int index) {
      Get.toNamed(AppRoutes.localimgscanpage,arguments: {"index":index,"dataItems":dataItems});
    }


}