import 'dart:io';

import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class ImageDownloadUtil {
  static Future<List<String>> download(List<String> urls) async {
    try {
      var dict = await getPhoneLocalPath();

      Dio dio = Dio();
      var paths = await Future.wait(urls.map((url) async {
        String path = '${dict.path}/${EncodeUtil.generateMd5(url)}.jpg';
        Logger.debug('图片下载地址 ${path.toString()}');
        await dio.download(url, path);
        return path;
      }).toList());

      return paths;
      // return paths.map((path) {
      //   return File(path);
      // }).toList();
    } catch (err) {
      Logger.error(err);
    }
    return [];
  }

  ///获取手机的存储目录路径
  static Future<Directory> getPhoneLocalPath() async {
    if (Platform.isIOS) {
      return getTemporaryDirectory();
    } else if (Platform.isAndroid) {
      return getExternalStorageDirectory();
    }
  }
}
