import 'package:flutter/material.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_ticket_provider_mixin.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';

import '../../../data/consts/event_report.dart';
import '../../../handler/event_handler.dart';
import '../../../resource/string.gen.dart';
import '../enums/screenshot_reply_tab.dart';

/// 亲密度分析-截图回复Tab页-controller
@injectable
class IntimacyAnalyseScreenshotReplyController extends BaseController
    with GetTickerProviderStateMixin {
  /// Tab列表
  RxList<ScreenshotReplyTab> tabBarList = ScreenshotReplyTab.values.obs;

  /// Tab控制器
  late final TabController tabController = TabController(
    length: tabBarList.length,
    vsync: this,
  );

  /// PageView控制器
  final PageController pageController = PageController();

  /// 当前索引
  final RxInt currentTabIndex = 0.obs;

  @override
  void onInit() {
    super.onInit();
    currentTabIndex.listen((newIndex) {
      _reportEvent(newIndex);
    });
  }

  @override
  void onClose() {
    tabController.dispose();
    pageController.dispose();
    super.onClose();
  }

  /// 处理Tab切换，联动PageView
  void handleTabChange(int index) {
    if (currentTabIndex.value == index) {
      return;
    }
    pageController.animateToPage(
      index,
      duration: const Duration(milliseconds: 300),
      curve: Curves.fastOutSlowIn,
    );
    currentTabIndex.value = index;
  }

  /// 处理PageView切换，联动Tab
  void handlePageChange(int index) {
    if (currentTabIndex.value == index) {
      return;
    }
    tabController.animateTo(index, duration: const Duration(milliseconds: 300));
    currentTabIndex.value = index;
  }

  /// 埋点
  void _reportEvent(int index) {
    // 识图回复
    if (index == ScreenshotReplyTab.conversationAnalysis.tabIndex) {
      EventHandler.report(EventId.event_10001);
    } else if (index == ScreenshotReplyTab.scanImageReply.tabIndex) {
      EventHandler.report(EventId.event_10004);
    }
  }
}
