import 'dart:typed_data';
import 'dart:io';
import 'package:chat/map/location_result.dart';
import 'package:flutter/services.dart';
import 'package:flutter_native_image/flutter_native_image.dart';
//import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'MessageMgr.dart';
import 'file_cache_mgr.dart';
class ScreenShot {
  static const MethodChannel _channel =
      const MethodChannel('make.photo.screen.hibok');
       static const EventChannel eventChannel =
  const EventChannel('com.file.hibok');

  static Future<List<int>> takeScreenshotImage() async {
    Map<String, dynamic> args = <String, dynamic>{};
    Uint8List bytes = await _channel.invokeMethod('makePhotoScreen', args);

    File file =await FileCacheMgr().writeFile(DateTime.now().millisecondsSinceEpoch.toString(),bytes);
    File compressedFile = await FlutterNativeImage.compressImage(file.path,
        quality: 20, percentage: 80);
    var compressImg =compressedFile.readAsBytesSync().toList();


//    var compressImg = await FlutterImageCompress.compressWithList(bytes,
//        quality: 20, minWidth: (Screen.width).toInt());

    print('地图大小${bytes.length} 压缩后${compressImg.length} ');
    return compressImg;
  }

  static Future<List> getOtherMapAppList() async {
    Map<String, dynamic> args = <String, dynamic>{};
    return _channel.invokeMethod('getListForMap', args);
  }

  static void openMapForOth(String mapType, LocationResult result) async {
    Map<String, dynamic> args = <String, dynamic>{
      'mapType': mapType,
      'address': result.address,
      'latLng': [result.latLng.latitude, result.latLng.longitude]
    };
    _channel.invokeMethod('openMapForOth', args);
  }

  static Future<bool> isSupportGoogle() async {
    Map<String, dynamic> args = <String, dynamic>{};
    return _channel.invokeMethod('isSupportGoogle', args);
  }

  static  getFileStream() {
    eventChannel.receiveBroadcastStream().listen((Object agrs){
      print('######## receiveBroadcastStream $agrs');

      File file = new File(agrs.toString());
      if(file.existsSync()){
        print('接收到的文件--存在');
        MessageMgr().emit(MessageMgr.RECEIVE_THIRD_SHARE, agrs);
      }else{
        print('接收到的文件--不存在');
      }

    }, onError: (Object error){
      print('######## error $error');
    });
  }
}
