import 'package:cpt_community/modules/my_following/tabs_data.dart';
import 'package:cs_resources/generated/assets.dart';
import 'package:flutter/cupertino.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:auto_route/auto_route.dart';

import 'my_following_page.dart';
import 'my_following_state.dart';

part 'my_following_vm.g.dart';

@riverpod
class MyFollowingVm extends _$MyFollowingVm {

  MyFollowingState initState() {
    return MyFollowingState(
      currentPageViewIdx: 0,
    );
  }

  @override
  MyFollowingState build(){
    final state = initState();
    Log.d("--------------------------build---------------------");
    return state;
  }

  tabsRouterChange(){
    Log.d("----tabsRouterChange---${myFollowingPageTabsRouterKey.currentState?.controller?.activeIndex}-");
    state = state.copyWith(
        currentPageViewIdx: myFollowingPageTabsRouterKey.currentState?.controller?.activeIndex ?? 0
    );
  }

  // 切换tab
  handlerChangeTab(int tabIndex, TabsRouter? tabsRouter,) {
    tabsRouter = (tabsRouter?? myFollowingPageTabsRouterKey.currentState?.controller)!;
    tabsRouter.setActiveIndex(tabIndex);
  }

  // 设置当前tab
  setCurrentTabIndex(int activeTabIdx){
    state = state.copyWith(
      activeTabIdx: activeTabIdx,
    );
  }

  // 改变 follow 页面navigatorbar
  handlerChangeFollowPageNavbar(bool showSearchBar){
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      if(showSearchBar){
        state = state.copyWith(
          followPageIsSearchBarStatus: true,
        );
      } else {
        state = state.copyWith(
          followPageIsSearchBarStatus: false,
        );
      }
    });
  }
  // 改变 follower 页面navigatorbar
  handlerChangeFollowerPageNavbar(bool showSearchBar){
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      if(showSearchBar){
        state = state.copyWith(
          followerPageIsSearchBarStatus: true,
        );
      } else {
        state = state.copyWith(
          followerPageIsSearchBarStatus: false,
        );
      }
    });
  }

  // 更新 tab 的数字
  updateMyFollowingTabsNum(String code, int num, int activeTabIdx){
    if(code == tabsNamesData['Follower']){
      state = state.copyWith(
        followerCount: num,
        activeTabIdx: activeTabIdx,
      );
    } else if(code == tabsNamesData['Follow']){
      state = state.copyWith(
        followCount: num,
        activeTabIdx: activeTabIdx,
      );
    }
  }
}