import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallpaper/models/imgnetodel.dart';
import 'package:wallpaper/utils/dbmanager.dart';
import 'package:wallpaper/utils/downloadmanager.dart';
import 'dart:async';
import 'dart:io';

import 'package:wallpaper/utils/local_img_manager.dart';
class LocalImgScanPageController extends GetxController {
  // late LocalImageInfo dataModel;
  var dataModel = <LocalImageInfo>[].obs;
  RxInt currentIndex = RxInt(0);
  RxInt showAppbar = RxInt(0);//控制appbar显示....
  late final PageController pcontroller;
  Timer? _timer;
  var isAndroid = Platform.isAndroid;
  var albumPermisonisDenied = false.obs;
  // var localDir = RxString(DBManager.localDir);
  var localDir = "";
  @override
  void onInit() {
    super.onInit();
     
      currentIndex.value = Get.arguments["index"];
      dataModel.value = Get.arguments["dataItems"];
      pcontroller = PageController(initialPage: currentIndex.value);

  }

@override
  void onReady() async {
    // TODO: implement onReady
    super.onReady();
          localDir = await LocalImgManager.getImgLocalDir();
          refresh();
  }

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

  }

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

  

  Future < void>  saveImgToAlbum() async {
    String imgUrl = '$localDir/${dataModel[currentIndex.value].localUrl!}';
      await DownloadManager.saveLocalImage(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 = '$localDir/${dataModel[currentIndex.value].localUrl!}';
      await DownloadManager.setWallpaperNative(imgUrl);
  }
}