import 'dart:io';

import 'package:ftrecruiter/api/api_provider.dart';
import 'package:ftrecruiter/api/http_result.dart';
import 'package:ftrecruiter/comm/constants/api_constants.dart';
import 'package:ftrecruiter/comm/utils/log_utils.dart';
import 'package:ftrecruiter/entity/response/UserLogin.dart';
import 'package:ftrecruiter/entity/response/hotel_login.dart';
import 'package:ftrecruiter/entity/response/industry_data.dart';
import 'package:ftrecruiter/entity/response/server_time.dart';
import 'package:ftrecruiter/entity/response/user_profile.dart';
import 'package:get/get.dart';

class ApiRepository {
  ApiProvider apiProvider;

  ApiRepository({required this.apiProvider});

  // //获取服务器时间
  // void getServerTime({NetSuccessCallback<ServerTime>? success, NetErrorCallback? onError}) {
  //   Map<String, String> headers = {};
  //   headers["Accept"] = "application/x.yyjobs-api.v1+json";
  //
  //   //获取数据和处理数据的逻辑放在这里
  //   apiProvider.requestNetEasy(ApiConstants.apiServiceTime, headers: headers, onSuccess: (json) {
  //     var serverTime = ServerTime.fromJson(json);
  //     if (success != null) {
  //       success(serverTime);
  //     }
  //   }, onError: onError);
  // }

  //获取服务器时间2
  Future<HttpResult<ServerTime?>> getServerTime2() async {
    Map<String, String> headers = {};
    headers["Accept"] = "application/x.yyjobs-api.v1+json";

    //网络请求获取原始数据
    final result = await apiProvider.requestNetResult(ApiConstants.apiServiceTime, headers: headers);

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

  /// 获取行业列表
  Future<HttpResult<IndustryData?>> getIndustryList2() async {
    Map<String, String> headers = {};
    headers["Accept"] = "application/x.yyjobs-api.v1+json";

    final result = await apiProvider.requestNetResult(ApiConstants.apiIndustryList, headers: headers);

    if (result.isSuccess) {
      final jsonList = result.getListJson();

      //获取List数据 需要转换一次
      var list = jsonList?.map((value) {
        if (value is Map<String, dynamic>) {
          return IndustryData.fromJson(value);
        } else {
          return null;
        }
      }).toList();

      return result.convert<IndustryData?>(list: list);
    }

    return result.convert<IndustryData>();
  }

  //获取用户信息
  Future<HttpResult<UserProfile?>> getUserProfile(String? token) async {
    Map<String, String> headers = {};
    headers["Accept"] = "application/x.yyjobs-api.v1+json";
    headers["Authorization"] = "bearer$token";

    var result = await apiProvider.requestNetResult(ApiConstants.apiUserProfile, headers: headers);

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

  //用户登陆
  Future<HttpResult<UserLogin?>> userLogin() async {
    Map<String, String> headers = {};
    headers["Accept"] = "application/x.yyjobs-api.v1+json";

    Map<String, String> params = {};
    params["nric_no"] = "+8618571458165";
    params["password"] = "12345678";
    params["registration_id"] = "1234";

    apiProvider = apiProvider.cancelAndResetHttpClient();

    var result = await apiProvider.requestNetResult(ApiConstants.apiUserLogin,
        method: HttpMethod.POST, headers: headers, query: params);

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

  //更新用户信息
  Future<HttpResult<UserLogin?>> userProfileUpdate(String? token) async {
    Map<String, String> headers = {};
    headers["Accept"] = "application/x.yyjobs-api.v12+json";
    headers["Authorization"] = "bearer$token";

    Map<String, String> params = {};
    params["mobile_no"] = "+8618571458166";
    params["verification_code"] = "123456 ";
    params["nick_name"] = "test liukai";

    Map<String, String> paths = {};
    paths['news_feed_avatar'] = '/data/user/0/com.hongyegroup.ftrecruiter/app_flutter/test_avatar.jpeg';

    var result = await apiProvider.requestNetResult(
      ApiConstants.apiUpdateProfile,
      method: HttpMethod.POST,
      headers: headers,
      query: params,
      paths: paths,
    );

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

  //用户登陆(回调的方式)
  void userLoginEasy({NetSuccessCallback<UserLogin>? success, NetErrorCallback? onError}) {
    Map<String, String> headers = {};
    headers["Accept"] = "application/x.yyjobs-api.v1+json";

    Map<String, String> params = {};
    params["nric_no"] = "+8618571458166";
    params["password"] = "12345678";
    params["registration_id"] = "1234";

    apiProvider.requestNetEasy(ApiConstants.apiUserLogin, method: HttpMethod.POST, headers: headers, query: params,
        onSuccess: (json) {
      var userLogin = UserLogin.fromJson(json);
      if (success != null) {
        success(userLogin);
      }
    }, onError: onError);
  }
}