import 'package:fis_common/event/event_type.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:fis_ui/widgets/layout/transform.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/live_consultation/controller.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/live_consultation/widgets/record_botton.dart';
import 'package:get/get.dart';

import '../state.dart';

// ignore: must_be_immutable
class OperatesDropdownButton extends FStatefulWidget
    implements FInteractiveContainer {
  /// 获取到清除事件通知
  FEventHandler<String> onClearCanavsHandler = FEventHandler<String>();

  OperatesDropdownButton({
    required this.onClearCanavsHandler,
  });
  @override
  String get pageName => "OperatesDropdownButton";

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

class _OperateDropdownState extends FState<OperatesDropdownButton> {
  ///屏幕缩放比例
  double get devicePixelRatio => MediaQuery.of(context).devicePixelRatio;
  // 按钮组宽度
  double get buttonGroupWidth =>
      (i18nBook.isCurrentRussian ? 68 : 60) / devicePixelRatio;
  // 按钮组容器底部内边距 30 + 30
  static const double groupContainerPadding = 5;
  static const Color _buttonTextColor = Color.fromARGB(255, 199, 198, 201);
  BoxDecoration get _buttonDecoration => BoxDecoration(
      borderRadius: BorderRadius.all(
        Radius.circular(buttonGroupWidth),
      ),
      border: Border.all(width: 1.5, color: Color.fromARGB(255, 170, 170, 170)),
      color: Color.fromARGB(255, 14, 16, 17));
  BoxDecoration get _buttonSelectDecoration => BoxDecoration(
      borderRadius: BorderRadius.all(
        Radius.circular(buttonGroupWidth),
      ),
      border: Border.all(width: 1.5, color: Colors.blue),
      color: Color.fromARGB(255, 0, 36, 54));
  bool _isExpanded = true;
  double _groupHeight = 0;
  final controller = Get.find<LiveConsultationController>();

  /// 获取当前组件最大高度
  get maxGroupHeight => Get.height - 200;

  @override
  FWidget build(BuildContext context) {
    return FColumn(
      children: [
        FTransform.translate(
          offset: Offset(0, buttonGroupWidth),
          child: FAnimatedContainer(
            duration: const Duration(milliseconds: 300),
            padding: EdgeInsets.only(
              top: 0,
              bottom: buttonGroupWidth,
            ),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(buttonGroupWidth),
                topRight: Radius.circular(buttonGroupWidth),
              ),
              // color: Colors.grey.withOpacity(0.6),
            ),
            clipBehavior: Clip.antiAlias,
            width: buttonGroupWidth,
            // height: groupHeight,
            constraints: BoxConstraints(
              maxHeight: _groupHeight,
            ),
            child: FListView(
              shrinkWrap: true,
              children: _buildMainLiveOperateButtons(),
            ),
          ),
        ),
        FInkWell(
          onHover: (isHover) {
            if (isHover && !_isExpanded) {
              setState(() {
                _groupHeight = maxGroupHeight;
                _isExpanded = true;
              });
            }
          },
          onTap: () {
            if (_isExpanded) {
              setState(() {
                _groupHeight = groupContainerPadding;
                _isExpanded = false;
              });
            } else {
              setState(() {
                _groupHeight = maxGroupHeight;
                _isExpanded = true;
              });
            }
          },
          child: FContainer(
            decoration: _buttonDecoration,
            // padding: const EdgeInsets.only(top: 5),
            height: buttonGroupWidth,
            width: buttonGroupWidth,
            child: FColumn(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                FIcon(
                  _isExpanded ? Icons.expand_more : Icons.more_vert,
                  color: _buttonTextColor,
                  size: 40,
                ),
                // FText(
                //   _isExpanded
                //       ? i18nBook.measure.collapseGroup.t
                //       : i18nBook.measure.expandGroup.t,
                //   style: TextStyle(color: _buttonTextColor),
                // ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  @override
  void initState() {
    super.initState();
    _groupHeight = maxGroupHeight;
  }

  List<FWidget> _buildMainLiveOperateButtons() {
    return [
      FObx(
        () {
          return _buildRoundButton(i18nBook.realTimeConsultation.lineSegment.t,
              () {
            controller.state.paintType = PaintType.straightLine;
          }, isSelect: controller.state.paintType == PaintType.straightLine);
        },
      ),
      FObx(
        () {
          return _buildRoundButton(i18nBook.realTimeConsultation.paintBrush.t,
              () {
            controller.state.paintType = PaintType.curvedLine;
          }, isSelect: controller.state.paintType == PaintType.curvedLine);
        },
      ),
      _buildRoundButton(i18nBook.measure.clear.t, () {
        controller.sendInteractiveBoardData('', true);
        widget.onClearCanavsHandler.emit(this, Store.user.code);
      }),
      FObx(() {
        if (controller.state.isVidFileCapturing) {
          return FContainer(
            width: buttonGroupWidth,
            height: buttonGroupWidth,
            margin: EdgeInsets.only(bottom: 10),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(
                  Radius.circular(buttonGroupWidth / 2),
                ),
                color: Colors.white),
            child: FCenter(
              child: FContainer(
                padding: EdgeInsets.all(5),
                child: FCircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation<Color>(Colors.black)),
              ),
            ),
          );
        } else {
          return RecordButton(
            onTap: () {
              controller.captureOneFrame();
            },
            onStartRecord: () {
              controller.startRecording();
            },
            onStopRecord: () {
              controller.endRecording();
            },
            buttonGroupWidth: buttonGroupWidth,
          );
        }
      }),
      FObx(() {
        if (controller.state.canRemoteParameter && controller.state.isUSDevice)
          return FTextTooltip(
            position: i18nBook.isCurrentRussian
                ? DisplayPosition.left
                : DisplayPosition.right,
            textStyle: TextStyle(
              fontSize: 16,
              color: Colors.white,
            ),
            height: 36,
            decoration: BoxDecoration(
              color: Colors.black.withOpacity(0.8),
              borderRadius: BorderRadius.circular(4),
            ),
            message: i18nBook.realTimeConsultation.tuningParameter.t,
            child: _buildRoundButton(
              i18nBook.realTimeConsultation.tuningParameter4btn.t,
              () {
                controller.controlParameterAsync();
              },
            ),
          );
        else
          return FSizedBox();
      })
    ];
  }

  FWidget _buildRoundButton(String title, GestureTapCallback? onTap,
      {bool isSelect = false}) {
    return FMouseRegion(
      cursor: SystemMouseCursors.click,
      child: FGestureDetector(
        name: title,
        businessParent: this.widget,
        onTap: onTap,
        child: FContainer(
          width: buttonGroupWidth,
          height: buttonGroupWidth,
          margin: EdgeInsets.only(bottom: 10),
          padding: i18nBook.isCurrentRussian ? EdgeInsets.only(left: 5) : null,
          decoration: isSelect ? _buttonSelectDecoration : _buttonDecoration,
          child: FCenter(
            child: FText(
              title,
              style: TextStyle(
                fontWeight: FontWeight.w300,
                color: _buttonTextColor,
                fontSize: devicePixelRatio > 1 ? 16 / devicePixelRatio : null,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
