
import 'package:domain/entity/home_module.dart';
import 'package:domain/entity/response/hotel_info_entity.dart';
import 'package:domain/repository/auth_repository.dart';
import 'package:get/get.dart';
import 'package:plugin_basic/constants/app_constant.dart';
import 'package:plugin_basic/service/app_config_service.dart';
import 'package:plugin_basic/service/user_service.dart';
import 'package:plugin_platform/engine/sp/sp_util.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:router/componentRouter/component_router_service.dart';
import 'package:shared/utils/event_bus.dart';
import 'package:widgets/load_state_layout.dart';
import 'package:widgets/widget_export.dart';

import '../job/sign_in_sign_out/sign_in_sign_out_page.dart';
import 'main_state.dart';

class MainController extends GetxController {
  final AuthRepository _authRepository = Get.find();
  final MainState state = MainState();

  var _needShowPlaceholder = true;

  //页面PlaceHolder的展示
  LoadState loadingState = LoadState.State_Success;
  String? errorMessage;

  //刷新页面状态
  void changeLoadingState(LoadState state) {
    loadingState = state;
    update();
  }

  // Refresh 控制器
  final EasyRefreshController refreshController = EasyRefreshController(
    controlFinishRefresh: true,
    controlFinishLoad: false,
  );

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

  // 重试请求
  Future retryRequest() async {
    _needShowPlaceholder = true;
    fetchHomeData();
  }

  /// 获取首页的数据
  void fetchHomeData() async {
    if (_needShowPlaceholder) {
      changeLoadingState(LoadState.State_Loading);
    }

    //获取到数据
    var result = await _authRepository.fetchHotelInfo(country: ConfigService.to.selectCountry.value);

    //处理数据
    if (result.isSuccess) {
      final hotelInfo = result.data;

      if (hotelInfo != null) {
        UserService.to.hotelInfo.value = hotelInfo;

        _handleList();
      } else {
        ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
      }
    } else {
      ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
    }

    //停止刷新
    refreshController.finishRefresh();
    //最后赋值
    _needShowPlaceholder = false;
  }

  // 处理数据与展示的逻辑
  void _handleList() {

      state.datas.clear();
      state.datas.addAll(state.modules);

      changeLoadingState(LoadState.State_Success);
  }


  @override
  void onReady() async {
    super.onReady();
    fetchHomeData();
    registerEventBus();
  }

  @override
  void onClose() {
    unregisterEventBus();
    state.datas.clear();
    super.onClose();
  }

  /// 跳转到指定的模块中去
  void gotoModulePage(HomeModule module) {
    switch (module.key) {
      case 'sign':
        SignInSignOutPage.startInstance();
        break;
    }
  }

  /// 跳转到设置页面
  void gotoSettingPage() {
    ComponentRouterServices.authService.startSettingPage();
  }

  // EventBus 的事件接收
  Subscription? subscribe;

  void registerEventBus() {
    subscribe = bus.on(AppConstant.eventMainRefresh, (arg) {
      refreshController.callRefresh();
    });
  }

  void unregisterEventBus() {
    bus.off(AppConstant.eventMainRefresh, subscribe);
  }
}
