import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import '../index.dart';

/// 视频标签选择器
class VideoLabelBar extends FisView<PastVideosController> {
  VideoLabelBar();

  @override
  FWidget build(BuildContext context) {
    return FObx(() {
      List<CourseLabelDTO> topLabels = controller.state.topLevelLabelList;
      String selectedLabel = controller.state.selectedTopLevelLabelId;
      List<CourseLabelDTO> secondLabels = controller.state.secondLevelLabelList;
      String selectedSecondLabel = controller.state.selectedSecondLevelLabelId;
      bool isExpandSecondLevel = controller.state.isExpandSecondLevelLabel;
      List<FWidget> topLabelWidgets = [];
      List<FWidget> secondLabelWidgets = [];
      if (isExpandSecondLevel) {
        secondLabelWidgets = secondLabels
            .map(
              (e) =>
                  _buildVideoSecondLevelLabel(e, selectedSecondLabel == e.code),
            )
            .toList();
      } else {
        topLabelWidgets = topLabels
            .map(
              (e) => _buildVideoTopLabel(e, selectedLabel == e.code),
            )
            .toList();
        FWidget allVideoLabel = _buildAllVideoLabel(selectedLabel.isEmpty);
        if (topLabelWidgets.length > 0) {
          topLabelWidgets.insert(0, allVideoLabel);
        }
      }
      ScrollController scrollController = ScrollController();
      return QuickFWidget(
        Listener(
          onPointerSignal: (event) {
            if (event is PointerScrollEvent) {
              const double overScroll = 100;
              final offset = event.scrollDelta.dy;
              double targetOffset = scrollController.offset + offset;
              if (targetOffset > -overScroll &&
                  targetOffset <
                      scrollController.position.maxScrollExtent + overScroll) {
                scrollController.jumpTo(targetOffset);
              }
            }
          },
          child: Container(
            height: 50,
            child: Row(
              children: [
                Expanded(
                  child: ScrollbarTheme(
                    data: ScrollbarThemeData(
                      radius: Radius.circular(20),
                      thickness: MaterialStateProperty.all(5),
                    ),
                    child: Scrollbar(
                      controller: scrollController,
                      thumbVisibility: true,
                      scrollbarOrientation: ScrollbarOrientation.bottom,
                      child: FListView(
                        physics: ClampingScrollPhysics(),
                        controller: scrollController,
                        scrollDirection: Axis.horizontal,
                        padding: EdgeInsets.symmetric(horizontal: 5),
                        children: isExpandSecondLevel
                            ? secondLabelWidgets
                            : topLabelWidgets,
                      ),
                    ),
                  ),
                ),
                if (isExpandSecondLevel) _buildExitSecondLevelButton(),
              ],
            ),
          ),
        ),
      );
    });
  }

  // 所有视频标签
  FWidget _buildAllVideoLabel(bool isSelected) {
    return FInkWell(
      onTap: () {
        controller.onClickAllVideo();
      },
      child: FContainer(
        padding: EdgeInsets.only(top: 6, left: 5, right: 5, bottom: 14),
        child: FContainer(
          padding: EdgeInsets.symmetric(horizontal: 5),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(
              Radius.circular(20),
            ),
            border: Border.all(
              width: 1,
              color: isSelected ? Colors.blue : Colors.grey,
            ),
            color: isSelected ? Colors.blue : Colors.transparent,
          ),
          child: FCenter(
              child: FText(
            i18nBook.common.allVideos.t,
            style: TextStyle(
              color: isSelected ? Colors.white : Colors.black87,
            ),
          )),
        ),
      ),
    );
  }

  FWidget _buildVideoTopLabel(CourseLabelDTO label, bool isSelected) {
    return FInkWell(
      onTap: () {
        controller.onClickTopLabel(label);
      },
      child: FContainer(
        padding: EdgeInsets.only(top: 6, left: 5, right: 5, bottom: 14),
        child: FContainer(
          padding: EdgeInsets.symmetric(horizontal: 5),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(
              Radius.circular(20),
            ),
            border: Border.all(
              width: 1,
              color: isSelected ? Colors.blue : Colors.grey,
            ),
            color: isSelected ? Colors.blue : Colors.transparent,
          ),
          child: FCenter(
              child: FText(
            _getLocalLabelName(label),
            style: TextStyle(
              color: isSelected ? Colors.white : Colors.black87,
            ),
          )),
        ),
      ),
    );
  }

  FWidget _buildVideoSecondLevelLabel(CourseLabelDTO label, bool isSelected) {
    return FInkWell(
      onTap: () {
        controller.onClickSecondLevelLabel(label);
      },
      child: FContainer(
        padding: EdgeInsets.only(top: 6, left: 5, right: 5, bottom: 14),
        child: FContainer(
          padding: EdgeInsets.symmetric(horizontal: 5),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(
              Radius.circular(20),
            ),
            border: Border.all(
              width: 1,
              color: isSelected ? Colors.blue : Colors.grey,
            ),
            color: isSelected ? Colors.blue : Colors.transparent,
          ),
          child: FCenter(
              child: FText(
            _getLocalLabelName(label),
            style: TextStyle(
              color: isSelected ? Colors.white : Colors.black87,
            ),
          )),
        ),
      ),
    );
  }

  // 退出二级标签
  FWidget _buildExitSecondLevelButton() {
    return FInkWell(
      onTap: () {
        controller.onClickCollapseSecondLevelLabel();
      },
      child: FContainer(
        padding: EdgeInsets.only(top: 6, left: 10, right: 10, bottom: 14),
        child: FContainer(
          padding: EdgeInsets.symmetric(horizontal: 10),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(
              Radius.circular(20),
            ),
            border: Border.all(
              width: 1,
              color: Colors.grey,
            ),
            color: Colors.transparent,
          ),
          child: FCenter(
            child: FText(
              i18nBook.common.back.t,
              style: TextStyle(
                color: Colors.black87,
              ),
            ),
          ),
        ),
      ),
    );
  }

  String _getLocalLabelName(CourseLabelDTO label) {
    // print("label.labelLanguageConfigs ${label.labelLanguageConfigs!.length}");
    if (label.labelLanguageConfigs!.length > 0) {
      for (var item in label.labelLanguageConfigs!) {
        if (item.languageCode == i18nBook.locale.toCodeString('-')) {
          return item.name ?? "";
        }
      }
    }
    return label.name ?? "";
  }
}
