import 'package:app_business/service/api.dart';
import 'package:app_business/service/app.dart';
import 'package:app_business/service/map_watch.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:track_common/service/map_watch.dart';
import 'package:track_common/track_common.dart';

import '../generated/base.pb.dart' as pb;
import 'abase.dart';

Future<void> allInit() async {
  final app = Get.put(AppService(), permanent: true);
  final api = Get.put(ApiService(), permanent: true);

  final services = <IService>[
    app,
    api,
  ];

  for (var service in services) {
    await service.init();
  }

  Get.put<MapWatchService>(MapWatchServiceImpl(), permanent: true);
  info('初始化完成');

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

  if (updateInfo.needUpdate && GetPlatform.isAndroid) {
    final pStream = CommonPub().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);
  }
}
