// ignore_for_file: non_constant_identifier_names

import 'package:fis_common/logger/logger.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_measure/process/workspace/measure_data_controller.dart';
import 'package:fis_measure/view/cursor.dart';
import 'package:fis_measure/view/gesture/positioned_cursor.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:get/get.dart';
import 'package:fis_measure/view/measure/measure_config/widgets/measure_configuration_style.dart';

import 'widgets/common.dart';
import 'widgets/palette.dart';
import 'widgets/radio_group.dart';
import 'widgets/select.dart';
import 'widgets/switch.dart';

class StyleConfig extends StatelessWidget {
  StyleConfig({
    Key? key,
    required this.businessParent,
  }) : super(key: key);
  final MeasureSystemSettingDTO measureSystemSetting =
      MeasureSystemSettingDTO();

  ///父级节点
  final FInteractiveContainer businessParent;

  /// 测量数据
  final measureData = Get.find<MeasureDataController>();
  final mouseState = Get.find<IMouseState>();

  /// [卡尺大小] 枚举值
  static final List<int> C_MEASURE_CURSOR_SIZE = [16, 24, 32];

  /// [卡尺颜色] 色值
  static final List<Color> C_MEASURE_CURSOR_COLOR = [
    Color(0xFFffff00),
    Color(0xFFadff2f),
    Color(0xFF008000),
    Color(0xFF00ff00),
    Color(0xFFa52a2a),
    Color(0xFFffa500),
    Color(0xFFff6347),
    Color(0xFF800080),
    Color(0xFF00ffff),
    Color(0xFF0000ff),
    Color(0xFF00008b),
    Color(0xFFffffff),
  ];

  ///  [测量时卡尺大小] 枚举值
  static final List<int> C_MEASURE_MARK_SIZE = [10, 16, 24];

  /// [字体大小] 枚举值
  static final List<int> C_RESULT_TEXT_SIZE = [10, 12, 14, 16, 18, 20, 22, 24];

  /// [注释字体大小] 枚举值
  static final List<int> C_ANNOTATION_TEXT_SIZE = [10, 12, 14, 16, 18, 20, 22];

  /// [卡尺类型] 标识枚举
  static final List<MeasureCursorType> C_MEASURE_CURSOR_TYPE = [
    MeasureCursorType.cursor01,
    MeasureCursorType.cursor02,
    MeasureCursorType.cursor03,
    MeasureCursorType.cursor04,
    MeasureCursorType.cursor05,
  ];

  /// [微距测量自动调整光标] [测量自动捕捉范围] 距离阈值的枚举值
  static final List<MeasureSelectModel> C_DISTANCE_THRESHOLD = [
    MeasureSelectModel(
      name: i18nBook.common.close.t,
      value: 0,
    ),
    MeasureSelectModel(
      name: '10',
      value: 10,
    ),
    MeasureSelectModel(
      name: '20',
      value: 20,
    ),
    MeasureSelectModel(
      name: '30',
      value: 30,
    ),
    MeasureSelectModel(
      name: '50',
      value: 50,
    ),
  ];

  /// [窗口位置] 枚举值
  static final List<MeasureSelectModel> C_PANEL_POSITION = [
    MeasureSelectModel(
      name: i18nBook.measure.topLeft.t,
      value: 0,
    ),
    MeasureSelectModel(
      name: i18nBook.measure.bottomLeft.t,
      value: 1,
    ),
    MeasureSelectModel(
      name: i18nBook.measure.topRight.t,
      value: 2,
    ),
    MeasureSelectModel(
      name: i18nBook.measure.bottomRight.t,
      value: 3,
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return FContainer(
        width: 550,
        height: 500,
        padding: const EdgeInsets.symmetric(
          vertical: 15,
          horizontal: 15,
        ),
        margin: const EdgeInsets.symmetric(
          horizontal: 15,
        ),
        child: PatternBody(
          businessParent: businessParent,
          measureSystemSetting: measureData.measureSystemSetting,
        ));
  }
}

class MeasureTextSize {
  MeasureTextSize(this.textSize);
  final int textSize;

  @override
  String toString() {
    return '$textSize×$textSize';
  }
}

class PatternBody extends FStatefulWidget {
  const PatternBody({
    Key? key,
    required this.businessParent,
    required this.measureSystemSetting,
  }) : super(key: key);

  ///父级节点
  final FInteractiveContainer businessParent;
  final MeasureSystemSettingDTO measureSystemSetting;
  @override
  FState<PatternBody> createState() => _PatternBodyState();
}

class _PatternBodyState extends FState<PatternBody> {
  /// 测量数据
  late final measureData = Get.find<MeasureDataController>();
  final mouseState = Get.find<IMouseState>();
  ScrollController measureStyleScrollController = ScrollController();
  Color get cursorColor {
    Color color = StyleConfig.C_MEASURE_CURSOR_COLOR[0];
    final value = measureData.measureSystemSetting.measureColor;
    if (value != null) {
      if (value.startsWith("#")) {
        Color colorFromHex = Color(int.parse(value.substring(1), radix: 16));
        color = colorFromHex;
      }
    }
    return color;
  }

  set cursorColor(Color color) {
    String hexColor = '#${color.value.toRadixString(16).padLeft(8, '0')}';
    measureData.measureSystemSetting.measureColor = hexColor;
  }

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

  /// 自动纠正非法值【脏数据检查】
  // void autoCorrectIllegalValue() {
  //   if (!C_MEASURE_CURSOR_SIZE
  //       .contains(widget.measureSystemSetting.cursorSize)) {
  //     widget.measureSystemSetting.cursorSize = C_MEASURE_CURSOR_SIZE[0];
  //   }
  //   if (!C_MEASURE_MARK_SIZE
  //       .contains(widget.measureSystemSetting.shapeCursorSize)) {
  //     widget.measureSystemSetting.shapeCursorSize = C_MEASURE_MARK_SIZE[0];
  //   }
  //   if (!C_RESULT_TEXT_SIZE.contains(widget.measureSystemSetting.fontSize)) {
  //     widget.measureSystemSetting.fontSize = C_RESULT_TEXT_SIZE[0];
  //   }
  //   if (!C_ANNOTATION_TEXT_SIZE
  //       .contains(widget.measureSystemSetting.annotationFontSize)) {
  //     widget.measureSystemSetting.annotationFontSize =
  //         C_ANNOTATION_TEXT_SIZE[0];
  //   }
  //   if (!isContainsValue(
  //       C_DISTANCE_THRESHOLD, widget.measureSystemSetting.autoSnapDistance)) {
  //     widget.measureSystemSetting.autoSnapDistance =
  //         C_DISTANCE_THRESHOLD[0].value.toString();
  //   }
  //   if (!isContainsValue(
  //       C_DISTANCE_THRESHOLD, widget.measureSystemSetting.minCursorDistance)) {
  //     widget.measureSystemSetting.minCursorDistance =
  //         C_DISTANCE_THRESHOLD[0].value.toString();
  //   }
  // }

  /// List<MeasureSelectModel> 中是否包含该值
  // bool isContainsValue(List<MeasureSelectModel> list, String? value) {
  //   if (value == null) return false;
  //   for (var item in list) {
  //     if (item.value.toString() == value) {
  //       return true;
  //     }
  //   }
  //   return false;
  // }

  CursorTypeEnum getMeasureSystemSettingCursorTypeIcon(
    MeasureCursorType cursorType,
  ) {
    switch (cursorType) {
      case MeasureCursorType.cursor01:
        return CursorTypeEnum.CursorType1Icon;

      case MeasureCursorType.cursor02:
        return CursorTypeEnum.CursorType2Icon;

      case MeasureCursorType.cursor03:
        return CursorTypeEnum.CursorType3Icon;

      case MeasureCursorType.cursor04:
        return CursorTypeEnum.CursorType4Icon;

      case MeasureCursorType.cursor05:
        return CursorTypeEnum.CursorType5Icon;

      default:
        return CursorTypeEnum.CursorType1Icon;
    }
  }

  @override
  FWidget build(BuildContext context) {
    return QuickFWidget(
      RawScrollbar(
        thumbVisibility: true,
        thickness: 4.0,
        radius: Radius.circular(2.0),
        thumbColor: LabColors.text100,
        trackColor: Colors.blue,
        trackBorderColor: Colors.blueAccent,
        controller: measureStyleScrollController,
        child: FListView(
          shrinkWrap: true,
          controller: measureStyleScrollController,
          children: [
            PatternBodyItem(
              patternBodyItemTitle: i18nBook.measure.ruler.t,
              patternBodyItem: FColumn(
                children: [
                  //卡尺类型
                  PatternItem(
                    title: i18nBook.measure.rulerType.t,
                    item: PatternItemRadio(
                        tabList: StyleConfig.C_MEASURE_CURSOR_TYPE,
                        businessParent: widget.businessParent,
                        value: getMeasureSystemSettingCursorType(
                          widget.measureSystemSetting.cursorType,
                        ),
                        type: PatternValueType.cursor,
                        onChangeState: (value) {
                          setState(
                            () {
                              measureData.measureSystemSetting.cursorType =
                                  getMeasureSystemSettingCursorTypeIcon(value);
                            },
                          );
                        }),
                  ),
                  //卡尺大小 (光标大小)
                  PatternItem(
                    title: i18nBook.measure.rulerSize.t,
                    item: PatternItemRadio(
                      tabList: StyleConfig.C_MEASURE_CURSOR_SIZE,
                      businessParent: widget.businessParent,
                      value: widget.measureSystemSetting.cursorSize,
                      onChangeState: (value) {
                        setState(() {
                          measureData.measureSystemSetting.cursorSize = value;
                        });
                      },
                    ),
                  ),
                  //测量时卡尺大小 (标记点大小)
                  PatternItem(
                    title: i18nBook.measure.measureRulerSize.t,
                    item: PatternItemRadio(
                      tabList: StyleConfig.C_MEASURE_MARK_SIZE,
                      value: widget.measureSystemSetting.shapeCursorSize,
                      businessParent: widget.businessParent,
                      onChangeState: (value) {
                        setState(() {
                          measureData.measureSystemSetting.shapeCursorSize =
                              value;
                        });
                      },
                    ),
                  ),
                ],
              ),
            ),
            PatternBodyItem(
              patternBodyItemTitle: i18nBook.measure.resultWindowConfig.t,
              patternBodyItem: FColumn(
                children: [
                  //显示结果窗口
                  PatternItem(
                    title: i18nBook.measure.showResultWindow.t,
                    item: PatternItemSwitch(
                      onChanged: (value) {
                        setState(() {
                          measureData.measureSystemSetting.showResultWindow =
                              value;
                        });
                      },
                      value: widget.measureSystemSetting.showResultWindow,
                    ),
                  ),
                  // //窗口位置
                  // PatternItem(
                  //   title: i18nBook.measure.panelPosition.t,
                  //   item: PatternItemSelect(
                  //     itemList: StyleConfig.C_PANEL_POSITION,
                  //     value: widget.measureSystemSetting.showResultLocation
                  //         .toString(),
                  //     onChanged: (value) {
                  //       try {
                  //         setState(() {
                  //           measureData.measureSystemSetting
                  //               .showResultLocation = int.parse(value);
                  //         });
                  //       } catch (e) {
                  //         logger.e("int.parse failed", e);
                  //       }
                  //     },
                  //   ),
                  // ),
                  //字体大小
                  PatternItem(
                    title: i18nBook.measure.fontSize.t,
                    item: PatternItemSelect(
                      itemList: StyleConfig.C_RESULT_TEXT_SIZE
                          .map((e) =>
                              MeasureSelectModel(name: e.toString(), value: e))
                          .toList(),
                      value: widget.measureSystemSetting.fontSize.toString(),
                      onChanged: (value) {
                        try {
                          setState(() {
                            measureData.measureSystemSetting.fontSize =
                                int.parse(value);
                          });
                        } catch (e) {
                          logger.e("int.parse failed", e);
                        }
                      },
                    ),
                  ),
                ],
              ),
            ),
            PatternBodyItem(
              patternBodyItemTitle: i18nBook.measure.display.t,
              isEnd: true,
              patternBodyItem: FColumn(
                children: [
                  //显示卡尺线
                  PatternItem(
                    title: i18nBook.measure.showRulerLine.t,
                    item: PatternItemSwitch(
                      onChanged: (value) {
                        setState(() {
                          measureData.measureSystemSetting.showCursorLine =
                              value;
                        });
                        measureData.setGuideline(value);
                      },
                      value: widget.measureSystemSetting.showCursorLine,
                    ),
                  ),
                  PatternItem(
                    title: i18nBook.measure.rulerLineColor.t,
                    item: PatternItemPalette(
                      activeColor: cursorColor,
                      colors: StyleConfig.C_MEASURE_CURSOR_COLOR,
                      onChanged: (value) {
                        setState(() {
                          cursorColor = value;
                          measureData.setPenColor(value);
                        });
                      },
                    ),
                  ),
                  //显示深度测量线
                  PatternItem(
                    title: i18nBook.measure.showDepthMeasureLine.t,
                    item: PatternItemSwitch(
                      onChanged: (value) {
                        setState(() {
                          measureData.measureSystemSetting.showDepthGuideline =
                              value;
                        });
                      },
                      value: widget.measureSystemSetting.showDepthGuideline,
                    ),
                  ),
                  //显示简洁注释
                  PatternItem(
                    title: i18nBook.measure.showSimpleAnnotation.t,
                    item: PatternItemSwitch(
                      onChanged: (value) {
                        setState(() {
                          measureData.measureSystemSetting.showBriefAnnotation =
                              value;
                        });
                      },
                      value: widget.measureSystemSetting.showBriefAnnotation,
                    ),
                  ),
                  //微距测量自动调整光标
                  PatternItem(
                    title: i18nBook.measure.autoAdjustCursor.t,
                    item: PatternItemSelect(
                      itemList: StyleConfig.C_DISTANCE_THRESHOLD,
                      value: widget.measureSystemSetting.minCursorDistance ??
                          StyleConfig.C_DISTANCE_THRESHOLD[0].value.toString(),
                      onChanged: (value) {
                        setState(() {
                          measureData.measureSystemSetting.minCursorDistance =
                              value;
                        });
                      },
                    ),
                  ),
                  //测量自动捕捉范围
                  PatternItem(
                    title: i18nBook.measure.autoCaptureRange.t,
                    item: PatternItemSelect(
                      itemList: StyleConfig.C_DISTANCE_THRESHOLD,
                      value: widget.measureSystemSetting.autoSnapDistance ??
                          StyleConfig.C_DISTANCE_THRESHOLD[0].value.toString(),
                      onChanged: (value) {
                        setState(() {
                          measureData.measureSystemSetting.autoSnapDistance =
                              value;
                        });
                      },
                    ),
                  ),
                  //注释字体大小
                  PatternItem(
                    title: i18nBook.measure.annotationFontSize.t,
                    item: PatternItemSelect(
                      itemList: StyleConfig.C_ANNOTATION_TEXT_SIZE
                          .map((e) =>
                              MeasureSelectModel(name: e.toString(), value: e))
                          .toList(),
                      value: widget.measureSystemSetting.annotationFontSize
                          .toString(),
                      onChanged: (value) {
                        try {
                          setState(() {
                            measureData.measureSystemSetting
                                .annotationFontSize = int.parse(value);
                          });
                        } catch (e) {
                          logger.e("int.parse failed", e);
                        }
                      },
                    ),
                  ),
                  //显示测量注释 [中文翻译值]
                  PatternItem(
                    title: i18nBook.measure.showMeasureAnnotation.t,
                    item: PatternItemSwitch(
                      onChanged: (value) {
                        if (value !=
                            measureData.measureSystemSetting.showAnnotation) {
                          setState(() {
                            measureData.measureSystemSetting.showAnnotation =
                                value;
                          });
                          measureData.showItemTransStateChanged
                              .emit(this, value);
                        }
                      },
                      value: widget.measureSystemSetting.showAnnotation,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
