/*
 * @author lsy
 * @date   2019-11-01
 **/
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:gengmei_flutter_plugin/ScanImagePlugn.dart';
import 'package:gengmei_flutter_plugin/gengmei_flutter_plugin.dart';
import 'package:gmalpha_flutter/AlbumModel/repository/AlbumRepository.dart';
import 'package:gmalpha_flutter/commonModel/GMBase.dart';
import 'package:gmalpha_flutter/commonModel/toast/toast.dart';

class AlbumPreviewModel extends BaseModel {
  AlbumRepository repo = AlbumRepository.getInstance();
  var imageLive = LiveData<String>();
  var pageList = LiveData<List<ScanImageItem>>();
  var titleLive = LiveData<String>();

  var selectLive = LiveData<bool>();
  var nextLive = LiveData<int>();

  final String imgPath;
  int size;
  int _maxCount;
  int maxVideoCount;
  String noVideoHint;

  AlbumPreviewModel(this.imgPath, this.size, this._maxCount, this.maxVideoCount,
      this.noVideoHint);

  getPreviewImage(BuildContext context) {
    print("IMAGE SIZE ===> $size");
//    if (size != 0 && size < 1024 * 1024*3 && Platform.isAndroid) {
//      imageLive.notifyView(imgPath);
//    } else {
//      int a = DateTime.now().millisecondsSinceEpoch;
//      iosItem(imgPath, context, (value) {
//        var realPath = value["realImagePath"];
//        imageLive.notifyView(realPath);
//        print("TIME ${DateTime.now().millisecondsSinceEpoch - a}");
//      });
//    }
  }

//
//  Future<Codec> _loadAsync(ResizeFileImage key) async {
//    assert(key == this);
//    final Uint8List bytes = await file.readAsBytes();
//    if (bytes.lengthInBytes == 0) return null;
//    return await instantiateImageCodec(bytes,
//        targetHeight: this.targetHeight, targetWidth: this.targetWidth);
//  }

//  void iosItem(String path, BuildContext context, Function fun) {
//    GengmeiFlutterPlugin.previewImage(path).then((value) {
//      if (value != null) {
//        fun(Map<String, String>.from(value));
//      }
//    }).catchError((error) {
//      Toast.debugShow(context, error.toString());
//      print(error);
//    });
//  }

  void getItem(String path, BuildContext context) {
    List<String> temp = [path];
  }

  @override
  void dispose() {
    nextLive.dispost();
    selectLive.dispost();
    pageList.dispost();
    imageLive.dispost();
    titleLive.dispost();
  }

  void getRealPath(BuildContext context, int index) {
    if(Platform.isAndroid){
      pageList.notifyView(fromPage);
      return;
    }
//    GengmeiFlutterPlugin.scareImg(fromPage[index].path, 1080);
    GengmeiFlutterPlugin.ios_album_path([fromPage[index].path]).then((value) {
      print("HEEEEEEE  ");
      var map = Map<String, String>.from(value[0]);
      print(map);
      fromPage[index].realPath = map["realImagePath"];
      pageList.notifyView(fromPage);
    }).catchError((error) {
      Toast.show(context, error.toString());
      print(error);
    });
  }

  List<ScanImageItem> fromPage;

  void setList(List<ScanImageItem> fromPage) {
    this.fromPage = fromPage;
  }

  void init(BuildContext context) {
    nextLive.notifyView(
        repo.getSelectVideo().length + repo.getSelectPhoto().length);
  }

  void clickItem(BuildContext context) {
    if (maxVideoCount == 0 && fromPage[currentIndex].isVideo) {
      if (noVideoHint != null) {
        Toast.show(context, noVideoHint);
      }
      return;
    }
    String path;
    if (Platform.isAndroid) {
      path = fromPage[currentIndex].realPath;
    } else {
      path = fromPage[currentIndex].path;
    }
    if (maxVideoCount > 0 && fromPage[currentIndex].isVideo) {
      if (!repo.getSelectVideo().contains(path)) {
        if (repo.getSelectVideo().length >= maxVideoCount) {
          Toast.show(context, "最多选择${maxVideoCount}个视频");
          return;
        }
        repo.addVideo(path);
        selectLive.notifyView(true);
      } else {
        repo.removeVideo(path);
        selectLive.notifyView(false);
      }
    } else {
      if (!repo.getSelectPhoto().contains(path)) {
        if (repo.getSelectPhoto().length >= _maxCount) {
          Toast.show(context, "最多选择${_maxCount}张图片");
          return;
        }
        repo.addPhoto(path);
        selectLive.notifyView(true);
      } else {
        repo.removePhoto(path);
        selectLive.notifyView(false);
      }
    }
    nextLive.notifyView(
        repo.getSelectPhoto().length + repo.getSelectVideo().length);
  }

  int currentIndex;

  void pageIndex(int index) {
    currentIndex = index;
    bool haveIt = false;
    repo.getSelectPhoto().forEach((value) {
      if (Platform.isAndroid) {
        if (value == fromPage[index].realPath) {
          haveIt = true;
        }
      } else {
        if (value == fromPage[index].path) {
          haveIt = true;
        }
      }
    });
    repo.getSelectVideo().forEach((value) {
      if (Platform.isAndroid) {
        if (value == fromPage[index].realPath) {
          haveIt = true;
        }
      } else {
        if (value == fromPage[index].path) {
          haveIt = true;
        }
      }
    });
    selectLive.notifyView(haveIt);
    titleLive.notifyView("${currentIndex+1} / ${fromPage.length}");
  }

  void playVideo() {
    if (fromPage[currentIndex].isVideo) {
      String path = Platform.isAndroid
          ? fromPage[currentIndex].realPath
          : fromPage[currentIndex].path;
      GengmeiFlutterPlugin.playAlbumVideo(path);
    }
  }

  void onNext() {
  }
}
