import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:news_getx/theme/app_colors.dart';
import 'package:news_getx/utils/iconfont.dart';
import 'package:uni_links/uni_links.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;

  // 第一次打开
  Future<void> handleInitialUri() async {
    if (!isInitialUriIsHandled) {
      isInitialUriIsHandled = true;

      try {
        final uri = await getInitialUri();
        if (uri == null) {
          print('no initial uri');
        } else {
          // 这里获取了 scheme 请求
          print('got initial uri: $uri');
        }
      } on PlatformException {
        print('falied to get initial uri');
      } on FormatException catch (err) {
        print('malformed initial uri, $err');
      }
    }
  }

  // 程序打开时介入
  handleIncomingLinks() {
    if (!kIsWeb) {
      uriSub = uriLinkStream.listen((Uri? uri) {
        // 这里获取了 scheme 请求
        print('got uri: $uri');

        // if (uri!.pathSegments[1].toLowerCase() == 'category') {
        if (uri != null && uri.path == '/notify/category') {
          print('跳转到Category页');
          handleNavBarTap(1);
        }
      }, onError: (err) {
        print('got err: $err');
      });
    }
  }

  /// 生命周期

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

    // deek link处理
    // handleInitialUri();
    // handleIncomingLinks();

    // 准备静态数据 作为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();
  }
}
