import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/services/notification.m.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:flyinsono/views/common/device_live/device_live_list/controller.dart';
import 'package:flyinsono/views/common/device_live/device_live_list/widgets/patchInfo_card.dart';
import 'package:get/get.dart';

import 'patch_progress_circle.dart';

class FPushUpdatePackage extends FStatefulWidget implements FPage {
  @override
  String get pageName => 'PushUpdatePackage';
  final String deviceCode;
  FPushUpdatePackage(this.deviceCode);

  @override
  FState<FStatefulWidget> createState() {
    return FPushUpdatePackageState();
  }
}

class FPushUpdatePackageState extends FState<FPushUpdatePackage> {
  final controller = Get.find<DeviceLiveListController>();
  final remoteControllerManager = Get.find<IRemoteControllerManager>();
  String _selectedPatchCode = '';
  double _progress = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  FWidget build(BuildContext context) {
    return FScaffold(
        appBar: FAppBar.simple(
          title: i18nBook.remoteControl.updatePush.t,
          onBackClicked: () {
            router.back();
          },
        ),
        body: FColumn(
          children: [
            FExpanded(child: FObx(
              (() {
                return FListView.builder(
                  itemBuilder: ((context, index) {
                    var patchInfo = controller.state.devicePatchList[index];
                    return FPatchInfoCard(
                      patchInfo: patchInfo,
                      isSelected: _selectedPatchCode == patchInfo.code,
                      onTap: (v) {
                        if (v) {
                          setState(() {
                            _selectedPatchCode = patchInfo.code ?? '';
                          });
                        }
                      },
                    );
                  }),
                  itemCount: controller.state.devicePatchList.length,
                );
              }),
            )),
            FElevatedButton(
              businessParent: widget,
              name: 'push patch',
              onPressed: () async {
                if (_selectedPatchCode.isEmpty) {
                  PromptBox.toast(i18nBook.remoteControl.selectPatchPackage.t);
                  return;
                }
                var result = await controller.pushDevicePatchAsync(
                  deviceCode: widget.deviceCode,
                  patchCode: _selectedPatchCode,
                );
                if (result) {
                  await Get.dialog(
                    FPatchProgressCircle(
                      onCancel: () async {
                        var result = await controller.pushDevicePatchAsync(
                          deviceCode: widget.deviceCode,
                          patchCode: _selectedPatchCode,
                          pushEnum: PushDevicePatchEnum.Cancel,
                        );
                        if (result) {
                          router.back();
                        }
                      },
                    ),
                  );
                }
              },
              child: FText(i18nBook.remoteControl.updatePush.t),
              style: ButtonStyle(
                padding: MaterialStateProperty.all(
                  EdgeInsets.symmetric(vertical: kIsMobile ? 0 : 15),
                ),
                fixedSize: MaterialStateProperty.all(
                    Size(MediaQuery.of(context).size.width, 50)),
              ),
            ),
          ],
        ));
  }
}
