import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flyinsono/architecture/utils/window__change_helpler.dart';
import 'package:flyinsono/global.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/manager/page_manager.dart';
import 'package:flyinsono/lab/pages/lab_main/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/lab/pages/lab_main/widgets/logo_item.dart';
import 'package:flyinsono/lab/pages/lab_main/widgets/user_label.dart';
import 'package:flyinsono/lab/pages/lab_main/widgets/widgets.dart';
import 'package:flyinsono/pages/others/title_bar_buttons.dart';
import 'package:get/get.dart';
import 'package:fis_common/index.dart';

class MainPageHeader extends GetView<LabMainController> {
  const MainPageHeader({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final content = Container(
      color: LabColors.mainPageHeaderColor,
      height: 50,
      child: Row(
        children: [
          if (!Global.isVStationSlave)
            GetBuilder<LabMainController>(
              id: LabMainController.headerLogoId,
              builder: (_) {
                return LogoItem(
                  isActive: PageManager.ins.activeTabHash ==
                      PageManager.ins.tabTitles[0].tabHash,
                  onTap: () {
                    PageManager.ins
                        .changeTabByHash(PageManager.ins.tabTitles[0].tabHash);
                  },
                );
              },
            ),
          Expanded(
            child: Stack(
              children: [
                GetBuilder<LabMainController>(
                  id: LabMainController.headerTabsId,
                  builder: (_) {
                    List<Widget> tabs = [];
                    if (PageManager.ins.tabViews.isEmpty) {
                      return Container();
                    }
                    for (var i = 0; i < PageManager.ins.tabTitles.length; i++) {
                      final int tabHash = PageManager.ins.tabTitles[i].tabHash;
                      tabs.add(
                        TabItem(
                          index: i,
                          isActive: PageManager.ins.activeTabHash == tabHash,
                          text: PageManager.ins.tabTitles[i].title,
                          onTap: () {
                            PageManager.ins.changeTabByHash(tabHash);
                          },
                          onClose: PageManager.ins.isFixedTab(tabHash)
                              ? null
                              : () => PageManager.ins.closeTab(tabHash),
                        ),
                      );
                    }
                    return Listener(
                      onPointerSignal: (pointerSignal) {
                        if (pointerSignal is PointerScrollEvent) {
                          controller
                              .scrollTabByPointerScrollEvent(pointerSignal);
                        }
                      },
                      child: SingleChildScrollView(
                        scrollDirection: Axis.horizontal,
                        controller: controller.tabScrollController,
                        physics: const NeverScrollableScrollPhysics(),
                        child: Row(
                          children: tabs,
                        ),
                      ),
                    );
                  },
                ),
                Row(
                  children: [
                    Obx(
                      () => controller.state.showLeftMoreIcon
                          ? ShowMoreIcon(
                              isLeft: true,
                              onTap: () {
                                controller.scrollTabToLeft();
                              },
                            )
                          : Container(),
                    ),
                    Expanded(child: Container()),
                    Obx(
                      () => controller.state.showRightMoreIcon
                          ? ShowMoreIcon(
                              isLeft: false,
                              onTap: () {
                                controller.scrollTabToRight();
                              },
                            )
                          : Container(),
                    ),
                  ],
                )
              ],
            ),
          ),
          Container(
            margin: const EdgeInsets.symmetric(horizontal: 10),
            child: Row(
              children: [
                if (!Global.isVStationSlave) UserLabel(),
                if (FPlatform.isWindows) _buildWindowOperateButtons(),
              ],
            ),
          ),
        ],
      ),
    );
    if (FPlatform.isPureWeb) {
      return content;
    } else {
      return GestureDetector(
        onPanStart: (event) {
          WindowChangeHelper.onStartDrag();
        },
        onPanEnd: (e) {
          WindowChangeHelper.onEndDrag();
        },
        onDoubleTap: () {
          WindowChangeHelper.onMaximize();
        },
        onPanCancel: () {
          WindowChangeHelper.onEndDrag();
        },
        child: content,
      );
    }
  }

  ///最大化、最小化、关闭按钮
  Widget _buildWindowOperateButtons() {
    return TitleBarButtons(
      WindowChangeHelper.onMinimize,
      WindowChangeHelper.onClose,
      WindowChangeHelper.onMaximize,
      LabColors.text100,
      LabColors.tabBarButtonColor,
      hoverColor: LabColors.hoverButtonColor,
      isLabHeader: true,
      isShowNotification: false,
    );
  }
}
