import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/widgets/progressbar/progressbar.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/views/desktop/device_live/device_live_list/controller.dart';
import 'package:get/get.dart';

class UploadProgressDialog extends FStatelessWidget {
  UploadProgressDialog() {
    _initUploadProgress();
  }
  final controller = Get.find<DeviceLiveListDesktopController>();
  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      contentPadding: EdgeInsets.symmetric(vertical: 25, horizontal: 25),
      title: FText(
        i18nBook.remoteControl.progressBar.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      okString: i18nBook.common.confirm4Btn.t,
      onClose: () {
        router.back();
      },
      cancelString: i18nBook.common.cancel.t,
      insetPadding: EdgeInsets.symmetric(vertical: 100),
      children: [
        _buildUploadProgress(),
      ],
      isDefault: false,
    );
  }

  /// 上传的进度条
  FWidget _buildUploadProgress() {
    return FContainer(
      width: 400,
      padding: EdgeInsets.symmetric(horizontal: 15),
      child: FColumn(
        children: [
          FSizedBox(height: 8.0),
          FObx(
            () => FProgeressbar(
              percentage: controller.state.uploadProgress,
              lineHeight: 20,
              animation: true,
              animationDuration: 100,
              animateFromLastPercentage: true,
              alignment: MainAxisAlignment.spaceBetween,
              child: FText(
                _getProgressDisplay(controller.state.uploadProgress),
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 16, color: Colors.white),
              ),
              backgroundColor: Colors.black26,
              progressBarColor: Colors.blue,
            ),
          ),
          FSizedBox(height: 8.0),
        ],
      ),
    );
  }

  /// 初始化进度条
  void _initUploadProgress() {
    controller.state.uploadProgress = 0;
  }

  String _getProgressDisplay(double exportState) {
    if (controller.state.uploadProgress == 1.0) {
      router.back();
      return "100%";
    }
    return (controller.state.uploadProgress * 100).toStringAsPrecision(2) + "%";
  }
}
