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

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);

      //如果是android,还应该调用设置壁纸的功能
      // if (Platform.isAndroid) {

      // }
      
      
      //给结果提示框
      //....
    }

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

    //删除图片
    void deleteWallpaper(String imgUrl,int index) async{
      await DownloadManager.deleteWallpaperFile(imgUrl);
      DBManager.deleteImg(dataItems[index]);
      // ignore: invalid_use_of_protected_member
      dataItems.removeAt(index);
      // dataItems.value = DBManager.allImgs();
    }

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


}