import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flyinsono/lab/manager/page_manager.dart';
import 'package:fis_ui/utils/sizer/sizer.dart';
import 'package:fis_common/index.dart';
import 'package:get/get.dart';
import 'index.dart';

class LabMainController extends GetxController {
  LabMainController();

  static const tabViewId = "tab_view";
  static const headerLogoId = "header_logo";
  static const headerTabsId = "header_tabs";

  final state = LabMainState();

  late final PageManager pageManager;

  // Tab 栏滑动控制器
  final ScrollController tabScrollController = ScrollController();

  void scrollTabByPointerScrollEvent(PointerScrollEvent event) {
    // 如果超出边界，则设置为边界值
    if (tabScrollController.offset + event.scrollDelta.dy < 0) {
      tabScrollController.jumpTo(0);
    } else if (tabScrollController.offset + event.scrollDelta.dy >
        tabScrollController.position.maxScrollExtent) {
      tabScrollController.jumpTo(tabScrollController.position.maxScrollExtent);
    } else {
      tabScrollController
          .jumpTo(tabScrollController.offset + event.scrollDelta.dy);
    }
  }

  // tab 栏滚动监听
  void _onTabScroll() {
    _updateLeftRightMoreIcon();
  }

  // 更新左右两侧的更多图标
  void _updateLeftRightMoreIcon() {
    if (tabScrollController.offset > 0) {
      state.showLeftMoreIcon = true;
    } else {
      state.showLeftMoreIcon = false;
    }
    if (tabScrollController.position.maxScrollExtent > 0 &&
        tabScrollController.offset <
            tabScrollController.position.maxScrollExtent) {
      state.showRightMoreIcon = true;
    } else {
      state.showRightMoreIcon = false;
    }
  }

  void scrollTabToLeft() {
    tabScrollController.animateTo(
      0,
      duration: Duration(milliseconds: 300),
      curve: Curves.ease,
    );
  }

  void scrollTabToRight() {
    tabScrollController.animateTo(
      tabScrollController.position.maxScrollExtent,
      duration: Duration(milliseconds: 300),
      curve: Curves.ease,
    );
  }

  void _onWindowResize(e) {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _updateLeftRightMoreIcon();
    });
  }

  void _onUpdateHeaderTabs(_, __) {
    update([tabViewId, headerTabsId, headerLogoId]);
  }

  void _onUpdateLeftRightMoreIcon(_, bool scrollToEnd) {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (scrollToEnd) {
        if (tabScrollController.position.maxScrollExtent > 0) {
          tabScrollController.animateTo(
            tabScrollController.position.maxScrollExtent,
            duration: Duration(milliseconds: 300),
            curve: Curves.ease,
          );
        }
      }
      _updateLeftRightMoreIcon();
    });
  }

  // 设置底部自定义文本
  void setBottomCustomText(String text) {
    state.bottomCustomText = text;
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    pageManager = PageManager.init();
    pageManager.initDefaultTabs();
    pageManager.tabChangeEvent.addListener(_onUpdateHeaderTabs);
    pageManager.updateLeftRightMoreIconEvent
        .addListener(_onUpdateLeftRightMoreIcon);
    tabScrollController.addListener(_onTabScroll);

    /// 监听resize事件
    Sizer.ins.addListener(_onWindowResize);

    /// 移动端强制横屏
    if (FPlatform.isIOS || FPlatform.isAndroid) {
      SystemChrome.setPreferredOrientations(
          [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
    }
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    tabScrollController.removeListener(_onTabScroll);
    Sizer.ins.removeListener(_onWindowResize);
    pageManager.tabChangeEvent.removeListener(_onUpdateHeaderTabs);
    pageManager.updateLeftRightMoreIconEvent
        .removeListener(_onUpdateLeftRightMoreIcon);
    pageManager.reset();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
    tabScrollController.removeListener(_onTabScroll);
    Sizer.ins.removeListener(_onWindowResize);
    pageManager.tabChangeEvent.removeListener(_onUpdateHeaderTabs);
    pageManager.updateLeftRightMoreIconEvent
        .removeListener(_onUpdateLeftRightMoreIcon);
    pageManager.reset();
  }
}

// 全局调用入口
class LabMainHook {
  /// 设置底部自定义文本
  static setBottomCustomText(String text) {
    try {
      Get.find<LabMainController>().setBottomCustomText(text);
    } catch (e) {
      print(e);
    }
  }
}
