import 'package:fis_jsonrpc/rpc.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/managers/interfaces/remote_controller.dart';
import 'package:flyinsono/pages/remote_controller/controller.dart';
import 'package:flyinsono/views/desktop/remote_maintenance/widgets/apiinfo.dart';
import 'package:flyinsono/views/desktop/remote_maintenance/widgets/custom_button.dart';
import 'package:flyinsono/views/desktop/remote_maintenance/widgets/custom_value_select.dart';
import 'package:get/get.dart';

import 'remote_controller_parameters_styles.dart';

class FirstParametersViewPage extends FisView<RemoteControllerController>
    implements FPage {
  final remoteControllerManager = Get.find<IRemoteControllerManager>();

  ///常规装饰器
  final normalBoxDecoration = TuningParametersStyles.normalBoxDecoration;
  final firstPageController = ScrollController();

  ///选中项装饰器
  final selectedBoxDecoration = TuningParametersStyles.selectedBoxDecoration;

  static const String _INT32_MAX = '2147483647';

  FirstParametersViewPage();

  String get deviceCode => controller.state.deviceCode;
  @override
  String get pageName => 'FirstParametersViewPage';

  @override
  FWidget build(BuildContext context) {
    return FExpanded(
      child: FObx(
        () {
          if (controller.state.allKeyBoardApiInfos.isEmpty &&
              controller.state.allFunctionInfos.isEmpty) {
            return FContainer(
              decoration: TuningParametersStyles.transparentBorderDecoration,
            );
          }
          List<FWidget> children = [];
          var allKeyBoardApiInfos = controller.state.allKeyBoardApiInfos.map(
            (e) {
              return CustomButton<ApiInfo>(
                onPressed: (value) async {
                  await controller.referParameters(
                    argsParameterValue: e.valuesMapString == 'Toggle:'
                        ? _INT32_MAX
                        : value.toString(),
                    pathParameterValue:
                        e.parentDescription! + '\\' + e.description!,
                    loopsParameterValue: '1',
                    controllerType: ControlDeviceParameterEnum.RunExecuteApi,
                  );
                },
                stopEventHandler: controller.stopEventHandler,
                width: 100,
                height: 40,
                isDecreaseAndIncrease:
                    (e.valuesMapString ?? '').contains('Decrease'),
                text: _setTitle(e.description ?? ''),
                decoration: normalBoxDecoration,
                textColor: Colors.white,
                data: e,
                isSelected: e.displayValue == 'Checked',
                selectedDecoration: selectedBoxDecoration,
              );
            },
          ).toList();
          List<FWidget> allFunctionInfos =
              controller.state.allFunctionInfos.map((e) {
            /// 判断是否是点击的
            if ((e.valuesMapString ?? '').contains(';')) {
              return CustomValueSelect(
                data: e,
                width: 100,
                height: 40,
                title: _setTitle(e.description ?? ''),
                decoration: normalBoxDecoration,
                textColor: Colors.white,
                onChanged: (value) async {
                  print('下拉框的值');
                  await controller.referParameters(
                    argsParameterValue: value.toString(),
                    pathParameterValue:
                        e.parentDescription! + '\\' + e.description!,
                    loopsParameterValue: '1',
                    controllerType: ControlDeviceParameterEnum.RunExecuteApi,
                  );
                },
              );
            } else {
              return CustomButton<ApiInfo>(
                onPressed: (value) async {
                  await controller.referParameters(
                    argsParameterValue: e.valuesMapString == 'Toggle:'
                        ? _INT32_MAX
                        : e.displayValue ?? "",
                    pathParameterValue:
                        e.parentDescription! + '\\' + e.description!,
                    loopsParameterValue: '1',
                    controllerType: ControlDeviceParameterEnum.RunExecuteApi,
                  );
                },
                stopEventHandler: controller.stopEventHandler,
                textColor: Colors.white,
                decoration: normalBoxDecoration,
                selectedDecoration: selectedBoxDecoration,
                width: 100,
                height: 40,
                text: _setTitle(e.description ?? ''),
                data: e,
                isSelected: e.displayValue == 'Checked',
              );
            }
          }).toList();
          children.addAll(allKeyBoardApiInfos);
          children.addAll(allFunctionInfos);
          return FSingleChildScrollView(
            controller: firstPageController,
            child: FWrap(
              runSpacing: 10,
              spacing: 10,
              children: children,
            ),
          );
        },
      ),
    );
  }

  String _setTitle(String description) {
    if (description.contains(':')) {
      return description.split(':')[1];
    }
    return description;
  }
}
