import 'dart:io';

import 'package:app_installer/app_installer.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:flutter/cupertino.dart';
import 'package:oktoast/oktoast.dart';
import 'package:path_provider/path_provider.dart';
import 'package:url_launcher/url_launcher.dart';
import 'CustomUI.dart';
import 'HttpUtil.dart';

class VersionUtils {
  static bool hasShowUpdate=false;
  static void versionUpdate(BuildContext context,{bool fromProfileNeedShow=false}) async {
    if(hasShowUpdate && !fromProfileNeedShow){
      return;
    }
    hasShowUpdate=true;


    Directory tempDir ;
    if(Platform.isAndroid){
      tempDir = await getExternalStorageDirectory();
    }
    final ValueNotifier<String> updateProgress = ValueNotifier<String>('-1');

    HttpUtil().appVersionUpdate((bool needNotice, bool forceUpdate,
        String versionName, String describe, String downloadUrl,String appId) {



        List<String> online = versionName.split('.');
        List<String> current = Constants.versionName.split('.');
        bool isSandbox =false;
        bool needUpdate = false;
        if(int.parse(current[0]) > int.parse(online[0])){
          isSandbox=true;
        }

        if(int.parse(current[0]) == int.parse(online[0]) && int.parse(current[1]) > int.parse(online[1])){
          isSandbox=true;
        }

        if(int.parse(current[0]) == int.parse(online[0]) && int.parse(current[1]) == int.parse(online[1]) && int.parse(current[2]) > int.parse(online[2])){
          isSandbox=true;
        }

      if(int.parse(online[0]) > int.parse(current[0])){
        needUpdate=true;
      }

      if(int.parse(current[0]) == int.parse(online[0]) && int.parse(online[1]) > int.parse(current[1])){
        needUpdate=true;
      }

      if(int.parse(current[0]) == int.parse(online[0]) && int.parse(current[1]) == int.parse(online[1]) && int.parse(online[2]) > int.parse(current[2])){
        needUpdate=true;
      }


        print('isSandbox $isSandbox');
        Constants.isSandbox = isSandbox;


//        needUpdate=true;///test
//      needNotice=true;///test


      String filePath ;
      if(Platform.isAndroid){
        filePath = '${tempDir.path}/$versionName.apk';
      }
      print(
          '当前版本号:${Constants.versionName} --新包版本号 $versionName   needNotice:$needNotice   forceUpdate"$forceUpdate  describe:$describe');
      if (needUpdate) {
        if (needNotice) {
          ///需要弹窗提示更新
          CustomUI.buildVersionUpdate(
              context, versionName, forceUpdate, describe, updateProgress, () {
            if (Constants.isStoreVersion) {
              String androidAppId = Constants.packageName;
              String iOSAppId = appId;
              AppInstaller.goStore(androidAppId, iOSAppId);
            } else {
              if (Platform.isAndroid) {
                HttpUtil().downloadApk(context, filePath, downloadUrl,
                    (int count, int total) {
                  ///下载进度更新
                  double percent = (count / total) * 100;
                  print('count:$count    total:$total percent:$percent');
                  updateProgress.value = percent.toStringAsFixed(0);
                  if (count == total) {
                    ///下载完成-安装
                    AppInstaller.installApk(filePath);
                    Future.delayed(Duration(seconds: 3), () {
                      updateProgress.value = '-1';

                      ///重置状态
                    });
                  }
                });
              } else {
                launch(downloadUrl);
              }
            }
          });
        }
      } else {
        if(fromProfileNeedShow){
          showToast(I18n.of(context).already_new);
        }

      }
    });
  }
}
