import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_measure/utils/prompt_box.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/managers/interfaces/remote_controller.dart';
import 'package:get/get.dart';

import 'progress_circle.dart';

class FPatchProgressCircle extends FStatefulWidget implements FPage {
  @override
  String get pageName => 'FPatchProgressCircle';
  final void Function()? onCancel;
  FPatchProgressCircle({this.onCancel});
  @override
  FState<FStatefulWidget> createState() {
    return FPatchProgressCircleState();
  }
}

class FPatchProgressCircleState extends FState<FPatchProgressCircle> {
  double _progress = 0;
  final remoteControllerManager = Get.find<IRemoteControllerManager>();

  @override
  void initState() {
    remoteControllerManager.onDeviceDownloadPatchProgressToUser
        .addListener(_onProgressChange);
    super.initState();
  }

  @override
  void dispose() {
    remoteControllerManager.onDeviceDownloadPatchProgressToUser
        .removeListener(_onProgressChange);
    super.dispose();
  }

  @override
  FWidget build(BuildContext context) {
    return QuickFWidget(ProgressCircle(
      progress: _progress,
      onCancel: widget.onCancel,
    ));
  }

  void _onProgressChange(
      Object sender, DeviceDownloadPatchProgressToUserNotification e) {
    setState(() {
      _progress = e.progress.toDouble() / 100;
    });
    if (e.progress == 100) {
      PromptBox.toast(
        i18nBook.remoteControl.pushSuccess.t,
      );
      router.back();
      router.back();
    }
  }
}
