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

import 'remote_controller_parameters_styles.dart';

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

  final TextStyle _texytStyle = TuningParametersStyles.texytStyle;
  final modelController = ScrollController();

  ///常规装饰器
  final normalBoxDecoration = TuningParametersStyles.normalBoxDecoration;

  ///选中项装饰器
  final selectedBoxDecoration = TuningParametersStyles.selectedBoxDecoration;
  String get deviceCode => controller.state.deviceCode;

  @override
  String get pageName => 'ModelSelectView';

  static const String _INT32_MAX = '2147483647';

  ///UI样式定义
  Color get _primaryColor => TuningParametersStyles.primaryColor;

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      height: 90,
      margin: EdgeInsets.symmetric(vertical: 5, horizontal: 5),
      padding: EdgeInsets.symmetric(horizontal: 10),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        border: Border.all(
          color: Colors.grey,
          width: 1,
        ),
      ),
      child: FColumn(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          FExpanded(
            child: FRow(
              children: [
                FIcon(
                  Icons.grid_view_rounded,
                  color: _primaryColor,
                ),
                FText(
                  i18nBook.remoteControl.modeSelection.t,
                  style: _texytStyle,
                ),
                FExpanded(
                  child: FContainer(),
                ),
                FIconButton(
                  icon: FIcon(
                    Icons.refresh,
                    color: _primaryColor,
                  ),
                  onPressed: () {
                    controller.getUltrasoundParameters();
                    controller.initRemoteControllerData();
                  },
                  businessParent: this,
                  name: 'refresh Probe and Application',
                ),
                FObx(() => controller.state.isSensorDataAvailable
                    ? FContainer()
                    : FElevatedButton(
                        child: FText(
                          i18nBook.realTimeConsultation.probesAndApplications.t,
                          style: _texytStyle,
                        ),
                        onPressed: () {
                          router.dialog(
                            () => ApplyProbeApplicationSettingDialog(),
                          );
                        },
                        businessParent: this,
                        name: "Probe and Application",
                      )),
              ],
            ),
          ),
          FDivider(
            height: 1,
            thickness: 1,
            color: Colors.grey,
          ),
          FSizedBox(
            height: 5,
          ),
          FFlexible(
            child: FObx(
              (() {
                if (controller.state.allModes.isEmpty) {
                  return FContainer(
                    height: 42.5,
                  );
                }
                return FAlwaysScrollListView(
                  scrollController: modelController,
                  // 添加Scrollbar小部件
                  // isAlwaysShown: true, // 设置为始终显示
                  child: FSingleChildScrollView(
                    controller: modelController,
                    scrollDirection: Axis.horizontal,
                    child: FContainer(
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.only(bottom: 10),
                      child: FRow(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: _buildAllModels(),
                      ),
                    ),
                  ),
                );
              }),
            ),
          ),
        ],
      ),
    );
  }

  ///所有模式集合
  List<FWidget> _buildAllModels() {
    return controller.state.allModes.map((e) {
      return FContainer(
        margin: EdgeInsets.only(right: 15),
        child: CustomButton<ApiInfo>(
          onPressed: (value) async {
            print('选择的模式集合');
            // if (e.valuesMapString == 'Click:0') {
            //   PromptBox.toast(i18nBook.remoteControl.thisKeyUnavailable.t);
            //   return;
            // }
            await controller.referParameters(
              argsParameterValue:
                  e.valuesMapString == 'Toggle:' ? _INT32_MAX : '0',
              pathParameterValue: e.parentDescription! +
                  '\\' +
                  _harmonicDescription(e.description!),
              loopsParameterValue: '1',
            );
          },
          isDecreaseAndIncrease:
              e.valuesMapString == 'Toggle:0;Decrease:-1;Increase:1',
          canExecute: e.canExecute ?? false,
          text: e.description ?? '',
          data: e,
          isSelected: e.displayValue == 'Checked',
          width: 80,
          height: 35,
          stopEventHandler: controller.stopEventHandler,
          decoration: normalBoxDecoration,
          textColor: Colors.white,
          selectedDecoration: selectedBoxDecoration,
        ),
      );
    }).toList();
  }

  String _harmonicDescription(String description) {
    if (description == 'HAR') {
      return 'Harmonic';
    } else {
      return description;
    }
  }
}
