import 'package:cpt_main/modules/home/property_news/home_property_news_page.dart';
import 'package:cpt_main/modules/notification/notification_page.dart';
import 'package:domain/repository/main_repository.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:router/componentRouter/component_service_manager.dart';
import 'package:widgets/widget_export.dart';

import 'home_state.dart';
import 'latest_news/latest_news_page.dart';
import 'management_guides/management_guides_page.dart';

part 'home_view_model.g.dart';

@riverpod
class HomeViewModel extends _$HomeViewModel with DioCancelableMixin {
  late final MainRepository _mainRepository;
  var _needShowLoading = true; //是否展示LoadingView

  // Refresh 控制器
  final EasyRefreshController refreshController = EasyRefreshController(
    controlFinishRefresh: true, //允许刷新
    controlFinishLoad: false, //允许加载
  );

  @override
  HomeState build() {
    _mainRepository = ref.read(mainRepositoryProvider);
    final state = HomeState();
    registerCancellation();
    return state;
  }

  // Refresh 刷新事件
  Future onRefresh() async {
    fetchHomeIndex();
  }

  /// 获取首页数据
  Future fetchHomeIndex() async {
    // 获取列表
    var result = await _mainRepository.fetchHomeIndex(
      needLoading: _needShowLoading,
      cancelToken: cancelToken,
    );

    // 处理数据
    if (result.isSuccess) {
      state = state.copyWith(homeIndex: result.data);
    } else {
      ToastEngine.show(result.errorMsg ?? "Network Load Error");
    }

    // 最后赋值
    _needShowLoading = false;
    refreshController.finishRefresh();
  }

  /// 根据索引触发点击的事件
  void switchCategory(int index) {
    switch (index) {
      case 0:
        //设备
        ComponentServiceManager().facilityService.startFacilityPage();
        break;
      case 1:
        //表单
        ComponentServiceManager().formService.startFormPage();
        break;
      case 2:
        //公告板
        ComponentServiceManager().noticeBoardService.startNoticeBoardPage();
        break;
      case 3:
        //房产
        ComponentServiceManager().propertyService.startPropertyPage();
        break;
      case 4:
        //广告服务
        ComponentServiceManager().servicesService.startServicePage();
        break;
      case 5:
        //社区
        ComponentServiceManager().communityService.startCommunityPage();
        break;
    }
  }

  //去消息通知页面
  void gotoNotificationPage() {
    NotificationPage.startInstance();
  }

  void gotoPaymentPage() {
    ComponentServiceManager().paymentService.startPaymentPage();
  }

  void gotoRewardsPage() {
    ComponentServiceManager().rewardsService.startRewardsPage();
  }

  void gotoLastNewsPage() {
    LatestNewsPage.startInstance();
  }

  void gotoPropertyNewsPage() {
    HomePropertyNewsPage.startInstance();
  }

  void gotoManageGuidePage() {
    ManagementGuidesPage.startInstance();
  }

}
