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';

class AppUpgradeWidget extends GetView<AppUpgradeController> {
  const AppUpgradeWidget({super.key, required this.model});
  final UpgradeCheckResultModel model;
  @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？";
        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: model.isNeedUpdate
              ? () {
                  if (controller.state.progress == 1) {
                    controller.installPackage(//安装
                        model.version);
                  } else {
                    //下载
                    controller.downloadPackage(model.version, model.downloadUrl,
                        onProgress: (progress) {
                      controller.state.progress = progress;
                    });
                    controller.state.updating = true;
                  }
                }
              : null,
          onCanceled: () {
            //取消
            controller.deleteFile(model.version); //删除本地安装包
          },
        );
      },
    );
  }
}
