import 'package:domain/entity/response/hotel_info_entity.dart';
import 'package:get/get.dart';
import 'package:plugin_platform/platform_export.dart';
import 'package:plugin_platform/http/http_provider.dart';
import 'package:plugin_platform/http/http_result.dart';

import '../constants/api_constants.dart';

/// 用户相关
class AuthRepository extends GetxService {
  HttpProvider httpProvider;

  AuthRepository({required this.httpProvider});

  /// 酒店登录
  Future<HttpResult<HotelInfoEntity>> userLogin(String? email,
      String? password, {
        int country = 1,
        CancelToken? cancelToken,
      }) async {
    //Post请求
    Map<String, String> params = {};
    params['email'] = email ?? "";
    params['password'] = password ?? "";

    final result = await httpProvider.requestNetResult(
      method: HttpMethod.POST,
      country == 1
          ? ApiConstants.apiAuthLoginV2 //新加坡
          : country == 3
          ? ApiConstants.apiLoginMS //马来
          : country == 5
          ? ApiConstants.apiLoginTH //泰国
          : ApiConstants.apiUserLogin,
      //通用
      params: params,
      networkDebounce: true,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = HotelInfoEntity.fromJson(json!);
      //重新赋值data
      return result.convert<HotelInfoEntity>(data: data);
    }
    return result.convert<HotelInfoEntity>();
  }

  /// 退出登录
  Future<HttpResult> userLogout({
    required int country,
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      country == 3 ? ApiConstants.apiUserLogoutMS : ApiConstants.apiUserLogout,
      method: HttpMethod.GET,
      networkDebounce: true,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      return result.convert();
    }
    return result.convert();
  }

  /// 修改密码
  Future<HttpResult> resetPassword(String? oldPassword,
      String? password,
      String? confirmPassword, {
        CancelToken? cancelToken,
      }) async {
    Map<String, String> params = {};
    params['old_password'] = oldPassword ?? "";
    params['password'] = password ?? "";
    params['password_confirmation'] = confirmPassword ?? "";

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiResetPassword,
      method: HttpMethod.POST,
      params: params,
      networkDebounce: true,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      return result.convert();
    }
    return result.convert();
  }

  /// 获取酒店信息
  Future<HttpResult<HotelInfoEntity>> fetchHotelInfo({required int country}) async {
    final result = await httpProvider.requestNetResult(
      country == 1
          ? ApiConstants.apiHotelInfoV2 //新加坡
          : country == 3
          ? ApiConstants.apiAccountInfoMS //马来
          : country == 5
          ? ApiConstants.apiAccountInfoTH //泰国
          : ApiConstants.apiHotelInfo,
      method: HttpMethod.GET,
      networkDebounce: true,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();
      var data = HotelInfoEntity.fromJson(json!);
      //重新赋值data或list
      return result.convert<HotelInfoEntity>(data: data);
    }
    return result.convert();
  }

  /// 酒店用户注册
  Future<HttpResult> hotelRegister(String? email,
      String? password,
      String? confirmPassword, {
        CancelToken? cancelToken,
      }) async {
    //参数
    Map<String, String> params = {};
    params['email'] = email ?? "";
    params['password'] = password ?? "";
    params['password_confirmation'] = confirmPassword ?? "";

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiHotelRegister,
      method: HttpMethod.POST,
      params: params,
      networkDebounce: true,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      return result.convert();
    }
    return result.convert();
  }

  /// 酒店用户注销
  Future<HttpResult> hotelDeactivate({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiHotelDeactivate,
      method: HttpMethod.POST,
      networkDebounce: true,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      return result.convert();
    }
    return result.convert();
  }
}
