
import 'package:cpt_community/components/custom_tabs_state.dart';
import 'package:cpt_community/modules/community/community_vm.dart';
import 'package:cpt_community/modules/garagesale/garagesale_vm.dart';
import 'package:cpt_community/modules/newsfeed/newsfeed_vm.dart';
import 'package:cs_resources/generated/assets.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared/utils/log_utils.dart';

part 'custom_tabs_vm.g.dart';

@riverpod
class CustomTabsVm extends _$CustomTabsVm {

  CustomTabsState initState(int? useTag, List? tabsList, tabItemBuilder, onClickAction) {
    CustomTabsState state = CustomTabsState(
        useTag: useTag??0,
        activeTabIndex: 0,
        tabsList: tabsList ?? [],
        tabItemBuilder:tabItemBuilder,
        onClickAction: onClickAction
    );

    return state;
  }

  @override
  CustomTabsState build(){
    print("5656566---${ref.read(communityVmProvider).useTag}");
    // 当前的 useTag
    // setUseTag(ref.read(communityVmProvider).useTag as int);

    // 监听 UserVM 的状态变化
    ref.listen(communityVmProvider.select((community) => community.useTag), (previous, next) {
      print('community useTag changed from $previous to $next');
      // 设置当前的 tabsList
      if(next == 1){
        // Garage Sale
        setUseTag(1);
      }else if(next == 0){
        setUseTag(0);
      }

    });

    CustomTabsState state = initState(0,[], null, null);
    return state;
  }

  // 设置当前的 useTag
  void setUseTag(int useTag) {
    CustomTabsState newState = state.copyWith(useTag: useTag);
    state = newState;
  }

  // 设置当前的tabsList
  void setTabsList() {
    List currentTabsList =  [];
    if(state.useTag == 0){
      // 找到 newsfeed 中的 tabsList
      currentTabsList = ref.read(newsfeedVmProvider).tabsList;
    }else if(state.useTag == 1){
      currentTabsList = ref.read(garagesaleVmProvider).tabsList;
    }

    CustomTabsState newState = state.copyWith(tabsList: currentTabsList);
    state = newState;
  }

  // 将组件构造器中的参数保存到state中
  Future<void> initPropData(tabsList,tabItemBuilder, onClickAction) async {
    // 获取 community 中的 useTag
    // state = state.copyWith(useTag: ref.watch(communityVmProvider).useTag);
    CustomTabsState newState = state.copyWith(
        tabsList: tabsList,
        tabItemBuilder: tabItemBuilder,
        onClickAction: onClickAction
    );
    state = newState;
  }


  // 点击tab
  void handlerClickTab(int index, item){
    setTabsList();

    // List newState = state.tabsList!.asMap().entries.map((entry) {
    //   Map<String, dynamic> obj = entry.value;
    //   int idx = entry.key;
    //
    //   if (obj['title'] == item['title'] && index == idx) {
    //     return {
    //       ...obj,
    //       'active': true,
    //     };
    //   } else {
    //     return {
    //       ...obj,
    //       'active': false,
    //     };
    //   }
    // }).toList();
    //
    // // if(state.useTag)
    // // 修改当前item 的active
    // state = state.copyWith(tabsList: newState);
    state = state.copyWith(activeTabIndex: index);

    print("切换后新的 sate, ${state.tabsList}");

    // ref.invalidate(customTabsVmProvider);
  }

}