import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../upgrade_dialog.dart';
import 'entity/upgrade_entity.dart';

Future<Map> request(String url) async{
  Response response = await Dio().get(url);
  return response.data;
}
void doHttpCheckVersion(BuildContext context){
  Future<Map> map = request("https://meeting-api.23544.com/meeting/mobile/latest.json");
  map.then((result) async {
    var jsonStr = json.encode(result);
    var listDynamic = jsonDecode(jsonStr);
    UpgradeEntity upgradeEntity = UpgradeEntity.fromJson(listDynamic);

    if(upgradeEntity.code == 200){
      PackageInfo packageInfo = await PackageInfo.fromPlatform();
      String versionCode = packageInfo.buildNumber;
      if(int.tryParse(versionCode)! < 2){
        debugPrint("检查更新-服务器版本信息：$jsonStr");
        debugPrint("检查更新-当前版本号：$versionCode");
        upgradeDialog(
            context,
            upgradeEntity.data.versionDescribe,
            hideCancelBtn: /*upgradeEntity.data.updateType == 1 ? false : true*/true,
            title: '版本更新 v${upgradeEntity.data.versionName}',
            /*upgradeEntity.data.url*/'https://s3.cn-north-1.amazonaws.com.cn/mtab.kezaihui.com/apk/takeaway_phone_release_1.apk',
            upgradeEntity.data.appStoreUrl,
            upgradeEntity.data.versionName
        );
      }else{
        return;
      }
    }else{
      return;
    }
  });
}