import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/components/alert_dialog.dart';
import 'package:vitalapp/managers/interfaces/models/upgrade.dart';
import 'package:vitalapp/pages/settings/center/view.dart';
import 'package:vitalapp/pages/upgrade/controller.dart';
import 'package:fis_common/logger/logger.dart';

class AppUpgradeWidget extends GetView<AppUpgradeController> {
  const AppUpgradeWidget({
    super.key,
    required this.model,
    this.onCanceled,
    this.onInstallReady,
  });

  final UpgradeCheckResultModel model;

  /// 取消
  final VoidCallback? onCanceled;

  /// 准备安装
  final VoidCallback? onInstallReady;

  @override
  Widget build(BuildContext context) {
    controller.state.updating = false;
    controller.state.progress = 0.0;
    return Obx(
      () {
        var content =
            "当前版本：${controller.state.currentVersion},最新版本：${model.version}, \n是否更新App？";
        logger
            .i("当前版本：${controller.state.currentVersion},最新版本：${model.version}");
        return VAlertDialog(
          title: controller.state.updating ? "下载新版本" : "提示",
          width: 400,
          confirmLabel: "安装",
          // showCancel: !model.isCoerce,
          content: Container(
            height: 80,
            padding: const EdgeInsets.symmetric(horizontal: 24),
            alignment: Alignment.center,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                if (!controller.state.updating)
                  Center(
                    child: Text(
                      content,
                      softWrap: true,
                      style: const TextStyle(fontSize: 20),
                    ),
                  ),
                if (controller.state.updating)
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 8.0),
                    child: Column(
                      children: [
                        ProgressBar(
                          progress: controller.state.progress,
                        ),
                        const SizedBox(
                          height: 10,
                        ),
                        if (controller.state.progress != 1)
                          const Center(child: Text("正在下载,请稍等")),
                        if (controller.state.progress == 1)
                          const Center(child: Text("下载完成")),
                      ],
                    ),
                  ),
              ],
            ),
          ),
          onConfirm: isShowInstallBtn(model)
              ? () {
                  if (controller.state.progress == 1) {
                    logger.i("开始新版本，版本号：${model.version}");
                    onInstallReady?.call();
                    controller.installPackage(model.version); //安装
                  } else {
                    controller.state.cancelToken = CancelToken();
                    logger.i("开始下载安装包,版本号：:${model.version}");
                    //下载
                    controller.downloadPackage(model.version, model.downloadUrl,
                        onProgress: (progress) {
                      if (controller.state.updating) {
                        controller.state.progress = progress;
                      }
                      if (controller.state.progress == 1) {
                        controller.state.updating = false;
                        logger.i("安装包下载完成,版本号：:${model.version}");
                      }
                    });
                    controller.state.updating = true;
                  }
                }
              : null,
          onCanceled: () {
            onCanceled?.call();
            //取消
            controller.state.cancelToken?.cancel();
            controller.deleteFile(model.version); //删除本地安装包
          },
        );
      },
    );
  }

  bool isShowInstallBtn(UpgradeCheckResultModel model) {
    if (!model.isNeedUpdate) {
      return false;
    } else {
      if (controller.state.updating) {
        return false;
      }
      return true;
    }
  }
}
