import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:news_getx/theme/app_colors.dart';
import 'package:news_getx/utils/iconfont.dart';

class ApplicationController extends GetxController {
  /// 响应式成员变量
  // 当前 tab 页码
  final _page = 0.obs;

  set page(value) => _page.value = value;

  get page => _page.value;

  /// 成员变量
  ///
  // tab 页标题
  late final List<String> tabTitles;

  // 页控制器
  late final PageController pageController;

  // 底部导航项目
  late final List<BottomNavigationBarItem> bottomTabs;

  /// 事件
  // tab栏动画
  void handleNavBarTap(int page) {
    pageController.animateToPage(
      page,
      duration: const Duration(seconds: 1),
      curve: Curves.ease,
    );
  }

  // tab栏页码切换
  void handlePageChange(int newPage) {
    page = newPage;
  }

  /// scheme 内部打开 TODO uriLink 通过地址打开程序指定页面
  bool isInitialUriIsHandled = false;
  StreamSubscription? uriSub;

  /// 生命周期

  @override
  void onInit() {
    super.onInit();

    // 准备静态数据 作为tab
    tabTitles = ['Welcome', 'Category', 'Bookmarks', 'Account'];
    bottomTabs = <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Icon(
          Iconfont.home,
          color: AppColors.tabBarElement,
        ),
        activeIcon: Icon(
          Iconfont.home,
          color: AppColors.secondaryElementText,
        ),
        label: "main",
        backgroundColor: AppColors.primaryBackground,
      ),
      BottomNavigationBarItem(
        icon: Icon(
          Iconfont.grid,
          color: AppColors.tabBarElement,
        ),
        activeIcon: Icon(
          Iconfont.grid,
          color: AppColors.secondaryElementText,
        ),
        label: 'category',
        backgroundColor: AppColors.primaryBackground,
      ),
      BottomNavigationBarItem(
        icon: Icon(
          Iconfont.tag,
          color: AppColors.tabBarElement,
        ),
        activeIcon: Icon(
          Iconfont.tag,
          color: AppColors.secondaryElementText,
        ),
        label: 'tag',
        backgroundColor: AppColors.primaryBackground,
      ),
      BottomNavigationBarItem(
        icon: Icon(
          Iconfont.me,
          color: AppColors.tabBarElement,
        ),
        activeIcon: Icon(
          Iconfont.me,
          color: AppColors.secondaryElementText,
        ),
        label: 'my',
        backgroundColor: AppColors.primaryBackground,
      ),
    ];

    pageController = PageController(initialPage: page);
  }

  @override
  void dispose() {
    uriSub?.cancel();
    pageController.dispose();
    super.dispose();
  }
}
