import 'package:common_pub/common_pub_platform_interface.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'api.dart';
import 'app.dart';
import 'package:common_pub/pb.dart' as pb;

Future<void> initBeforeApp() async {
  // final flavor = await getFlavor();
  // if(flavor=='dev'){
  //   GlobalVar.apiHost='totapi-lc.beswell.com';
  //   GlobalVar.flavor=Flavor.dev;
  //   info('版本：dev');
  //   FlavorConfig(
  //     name: "开发版",
  //     color: Colors.red,
  //     location: BannerLocation.topStart,
  //   );
  // }
}

Future<void> allInit() async {
  Get.put<AppService>(await AppService().init());
  Get.put<ApiService>(await ApiService().init());

  ApiService api = Get.find();
  final packageInfo = await PackageInfo.fromPlatform();
  final appVersion = packageInfo.version;
  final updateInfo = await api.stub
      .toGetUpdateVersion(pb.ToGetUpdateVersionRequest(vCode: appVersion));

  if (updateInfo.needUpdate) {
    final pStream =
        CommonPubPlatform.instance.updateApp(updateInfo.vUrl, updateInfo.vCode);
    final value = 0.0.obs;
    value.bindStream(pStream);

    await Get.dialog(
        AlertDialog(
          title: const Text('软件更新'),
          content: Obx(() =>  LinearProgressIndicator(value: value.value)),
        ),
        barrierDismissible: false);
  }
}
