import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallpaper/models/imgnetodel.dart';
import 'package:wallpaper/utils/downloadmanager.dart';
import 'dart:async';
import 'dart:io';
class ImgScanPageController extends GetxController {
  late NetImgCategory dataModel;
  RxInt currentIndex = RxInt(0);
  RxInt showAppbar = RxInt(0);//控制appbar显示....
  late final PageController pcontroller;
  Timer? _timer;
  var isAndroid = Platform.isAndroid;
  var albumPermisonisDenied = false.obs;
  @override
  void onInit(){
    super.onInit();
      currentIndex.value = Get.arguments["index"];
      dataModel = Get.arguments["dataModel"];
      pcontroller = PageController(initialPage: currentIndex.value);
  }

  @override
  void onClose() {
    cancelTimer();
    super.onClose();

  }

  void cancelTimer() {
    if (_timer != null) {
        _timer!.cancel();
        _timer = null;
      }
  }

  Future < void> downloadImg() async {
    String urlStr = dataModel.data![currentIndex.value].original!;
    if ((await DownloadManager.downloadImgWithUrl(urlStr)) == false){
      albumPermisonisDenied.value = true;
    }
        //提示框
        //....已下载
  }

  Future < void>  saveImgToAlbum() async {
    String imgUrl = dataModel.data![currentIndex.value].original!;
      await DownloadManager.saveNetworkImage(imgUrl);
      //如果是android,还应该调用设置壁纸的功能
      //....
      //给结果提示框
  }

  void showAppbarAction(){
      showAppbar.value = 1;
      cancelTimer();

      const oneSec = Duration(seconds: 3);
      _timer = Timer.periodic(oneSec, (Timer timer) {
        timer.cancel();
        _timer = null;
        showAppbar.value = 0;
      });
  }

  Future <void> setWallpaper() async {
      String imgUrl = dataModel.data![currentIndex.value].original!;
      await DownloadManager.setWallpaper(imgUrl);
  }
}