import 'package:flutter/foundation.dart';
import 'package:flyinsono/lab/pages/lab_image/urm_data_processor/config/special_urm_config.dart';
import 'package:flyinsono/lab/pages/lab_image/urm_data_processor/index.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_operate_panel/adjust_panel_config.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_operate_panel/index.dart';

class URMConfig {
  static ParamConfig zoomConfig = ParamConfig(
      URMParamType.ZOOM, "ZOOM", 10, 1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  static ParamConfig downsampleIndexConfig =
      ParamConfig(URMParamType.DownsampleIndex, "分辨率", 3, 2, [1, 2, 3]);
  static ParamConfig intPowerDenConfig = ParamConfig(
      URMParamType.IntPowerDen, "密度对比度", 7, 3, [1, 2, 3, 4, 5, 6, 7]);
  static ParamConfig intPowerDirConfig = ParamConfig(
      URMParamType.IntPowerDir, "方向对比度", 7, 3, [1, 2, 3, 4, 5, 6, 7]);
  static ParamConfig vessScaleConfig = ParamConfig(
      URMParamType.VessScale, "血管增强", 6, 3, [0, 0.8, 1.6, 2.4, 3.2, 4]);
  static ParamConfig sigmaGaussConfig =
      ParamConfig(URMParamType.SigmaGauss, "空间平滑", 5, 2, [0, 1, 2, 3, 4]);
  static ParamConfig velMaxScalerConfig = ParamConfig(URMParamType.VelMaxScaler,
      "阈值(上限)", 10, 4, [0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5]);
  static ParamConfig velMinScalerConfig = ParamConfig(URMParamType.VelMinScaler,
      "阈值(下限)", 6, 0, [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]);
  static ParamConfig iterationsConfig =
      ParamConfig(URMParamType.Iterations, "Iterations", 5, 2, [1, 2, 3, 4, 5]);
  static ParamConfig urmVideoParameterConfig =
      ParamConfig(URMParamType.URMVideoParameter, "微泡尺寸", 3, 2, [1, 2, 3]);
  static ParamConfig urmVideoTypeConfig =
      ParamConfig(URMParamType.URMVideoType, "显示样式", 3, 1, [0, 1, 2]);
  static ParamConfig urmVideoIndexConfig = ParamConfig(
      URMParamType.URMVideoIndex,
      "URM 视频帧下标",
      200,
      0,
      List.generate(200, (index) => index.toDouble()));

  // static String videoSaveButtonKey = "videoSaveButtonKey";

  static AdjustPanelConfig getURMAdjustConfig(
    ValueChanged<OperationCall>? onOperationCall, {
    String? probeType,
  }) {
    return AdjustPanelConfig([
      AdjustSwitchItemInfo(
        key: URMParamType.SyncDisplay.toString(),
        title: "同步显示",
        currentValue: true,
        onChanged: (value) {
          onOperationCall?.call(
            OperationCall(
              type: OperationType.SWITCH_SYNC_DISPLAY,
              arguments: value,
            ),
          );
        },
      ),
      AdjustSwitchItemInfo(
        title: "视频播放",
        currentValue: false,
        onChanged: (value) {
          onOperationCall?.call(OperationCall(
            type: OperationType.VIDEO_PLAY,
            arguments: value,
          ));
        },
      ),
      AdjustButtonGroupItemInfo(
        title: "",
        buttons: <AdjustButtonItemInfo>[
          AdjustButtonItemInfo(
            title: '轨迹描绘',
            onPressed: () {
              onOperationCall?.call(OperationCall(
                type: OperationType.TRACE,
                arguments: null,
              ));
            },
          ),
          AdjustButtonItemInfo(
            title: '恢复图像',
            onPressed: () {
              onOperationCall?.call(OperationCall(
                type: OperationType.Revert,
                arguments: null,
              ));
            },
          ),
        ],
      ),
      AdjustTypeItemInfo<UrmBlendType>(
        key: URMParamType.URMBlend.toString(),
        title: "混合",
        types: [
          UrmBlendType.Open,
          UrmBlendType.Middle,
          UrmBlendType.Close,
        ],
        textMap: {
          UrmBlendType.Open: "开启",
          UrmBlendType.Middle: "中",
          UrmBlendType.Close: "关闭",
        },
        currentValue: UrmBlendType.Close,
        onChanged: (value) {
          onOperationCall?.call(OperationCall(
            type: OperationType.ADJUST_PARAMETER,
            arguments:
                URMParamAdjust(type: URMParamType.URMBlend, value: value),
          ));
        },
      ),
      AdjustTypeItemInfo<UrmImageType>(
        title: "URM 图类型",
        types: [
          UrmImageType.Den,
          UrmImageType.Vel,
          UrmImageType.Dir,
          UrmImageType.Angle
        ],
        textMap: {
          UrmImageType.Den: "Den",
          UrmImageType.Vel: "Vel",
          UrmImageType.Dir: "Dir",
          UrmImageType.Angle: "Angle",
        },
        currentValue: UrmImageType.Den,
        onChanged: (value) {
          print(value);
          onOperationCall?.call(
            OperationCall(
              type: OperationType.SWITCH_URM_IMAGE_TYPE,
              arguments: value,
            ),
          );
        },
      ),
      // ZOOM 1-10
      AdjustNumberItemInfo(
        title: "ZOOM",
        min: 1,
        max: 20,
        currentValue: 1,
        onChanged: (value) {
          print("ZOOM value:" + value.toString());
          double val = 0.0;
          if (probeType == URMConfigConvexS18CM.probeType) {
            //弧阵是1
            val = value as double;
          } else {
            val = (value as double) / 2.0 + 0.5;
          }
          print("ZOOM val:" + val.toString());
          onOperationCall?.call(OperationCall(
            type: OperationType.ADJUST_PARAMETER,
            arguments: URMParamAdjust(type: URMParamType.ZOOM, value: val),
          ));
        },
      ),
      if (probeType == URMConfigConvexS18CM.probeType) ...[
        createNumberItem(
            URMConfigConvexS18CM.downsampleIndexConfig, onOperationCall),
        createNumberItem(
            URMConfigConvexS18CM.intPowerDenConfig, onOperationCall),
        createNumberItem(
            URMConfigConvexS18CM.intPowerDirConfig, onOperationCall),
        createNumberItem(URMConfigConvexS18CM.vessScaleConfig, onOperationCall),
        createNumberItem(
            URMConfigConvexS18CM.sigmaGaussConfig, onOperationCall),
      ] else ...[
        createNumberItem(downsampleIndexConfig, onOperationCall),
        createNumberItem(intPowerDenConfig, onOperationCall),
        createNumberItem(intPowerDirConfig, onOperationCall),
        createNumberItem(vessScaleConfig, onOperationCall),
        createNumberItem(sigmaGaussConfig, onOperationCall),
      ],
      createNumberItem(velMaxScalerConfig, onOperationCall),
      createNumberItem(velMinScalerConfig, onOperationCall),
      createNumberItem(urmVideoParameterConfig, onOperationCall),
      createNumberItem(urmVideoTypeConfig, onOperationCall),
      AdjustItemInfo(
        "参数预设",
        AdjustItemType.Preset,
      ),
      // AdjustButtonItemInfo(
      //   key: videoSaveButtonKey,
      //   title: "保存视频",
      //   onPressed: () {
      //     onOperationCall?.call(OperationCall(
      //       type: OperationType.VIDEO_SAVE,
      //       arguments: null,
      //     ));
      //   },
      // ),
    ]);
  }

  static AdjustNumberItemInfo createNumberItem(
      ParamConfig config, ValueChanged<OperationCall>? onOperationCall) {
    return AdjustNumberItemInfo(
      key: config.type.toString(),
      title: config.title,
      min: 0,
      max: config.valueCount - 1,
      currentValue: config.defaultValueIndex,
      onChanged: (value) {
        onOperationCall?.call(OperationCall(
          type: OperationType.ADJUST_PARAMETER,
          arguments: URMParamAdjust(
            type: config.type,
            value: config.valueRange[value],
          ),
        ));
      },
    );
  }

  // Den 模式下的可调项（分辨率、密度对比度、血管增强、空间平滑）
  static List<String> adjustKeysInDen = [
    URMParamType.SyncDisplay.toString(),
    URMParamType.DownsampleIndex.toString(),
    URMParamType.IntPowerDen.toString(),
    URMParamType.VessScale.toString(),
    URMParamType.SigmaGauss.toString(),
  ];
  // Dir 模式下的可调项（分辨率、方向对比度、血管增强、空间平滑、Iterations）
  static List<String> adjustKeysInDir = [
    URMParamType.SyncDisplay.toString(),
    URMParamType.DownsampleIndex.toString(),
    URMParamType.IntPowerDir.toString(),
    URMParamType.VessScale.toString(),
    URMParamType.SigmaGauss.toString(),
    // URMParamType.Iterations.toString()
  ];
  // Vel 模式下的可调项（分辨率、血管增强、空间平滑、阈值（上限）、阈值（下限））
  static List<String> adjustKeysInVel = [
    URMParamType.SyncDisplay.toString(),
    URMParamType.DownsampleIndex.toString(),
    URMParamType.VessScale.toString(),
    URMParamType.SigmaGauss.toString(),
    URMParamType.VelMaxScaler.toString(),
    URMParamType.VelMinScaler.toString()
  ];
  // Angle 模式下的可调项（分辨率、血管增强、空间平滑）
  static List<String> adjustKeysInAngle = [
    URMParamType.SyncDisplay.toString(),
  ];
  // Video 模式下的可调项（微泡尺寸、视频类型、视频帧）
  static List<String> adjustKeysInVideo = [
    URMParamType.SyncDisplay.toString(),
    URMParamType.URMVideoParameter.toString(),
    URMParamType.URMVideoType.toString(),
    URMParamType.URMVideoIndex.toString(),
    // videoSaveButtonKey,
  ];
}

class ParamConfig {
  URMParamType type;
  String title;
  int valueCount;
  List<double> valueRange;
  int defaultValueIndex = 0;

  ParamConfig(
    this.type,
    this.title,
    this.valueCount,
    this.defaultValueIndex,
    this.valueRange,
  )   : assert(valueCount == valueRange.length),
        assert(defaultValueIndex >= 0 && defaultValueIndex < valueCount);

  double get defaultValue => valueRange[defaultValueIndex];
}
