import 'package:domain/constants/api_constants.dart';
import 'package:domain/entity/property_news_detail_entity.dart';
import 'package:domain/entity/property_news_entity.dart';
import 'package:domain/entity/property_sale_rent_entity.dart';
import 'package:domain/entity/server_time.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:plugin_platform/platform_export.dart';
import 'package:plugin_platform/http/dio_engine.dart';
import 'package:plugin_platform/http/http_result.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/util.dart';

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:plugin_basic/provider/http_provider/http_provider.dart';


part 'property_resposity.g.dart';

@Riverpod(keepAlive: true)
PropertyRespository propertyRespository(Ref ref) {
  final dioEngine = ref.watch(dioEngineProvider);
  return PropertyRespository(dioEngine: dioEngine);
}

/*
 * 数据仓库
 */
class PropertyRespository {
  DioEngine dioEngine;

  PropertyRespository({required this.dioEngine});

  // 获取 news 列表
  Future<HttpResult<Object>> fetchNewsList(
      Map<String, dynamic>? data, {
        CancelToken? cancelToken,
      }) async {
    Map<String, dynamic> params = {};

    params = data!;

    Map<String, String> headers = {};

    headers["Content-Type"] = "application/x-www-form-urlencoded";
    headers["Accept"] = "application/x.yyjobs-api.v1+json";

    final result = await dioEngine.requestNetResult(
      // ApiConstants.apiServerTime, // api 地址
      '/api/v1/user/property/news/list', // api 地址
      params: params,
      headers: headers,
      method: HttpMethod.GET,
      isShowLoadingDialog: false,  //是否展示默认的Loading弹窗
      networkDebounce: true,   //是否防抖防止重复请求
      cancelToken: cancelToken,
    );

    Log.d("------请求返回的result--$result--------");
    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();
      var data = PropertyNewsEntity.fromJson(json!);
      //重新赋值data或list
      return result.convert<PropertyNewsEntity>(data: data);
    }else {
      if(result.errorMsg != null && result.errorMsg!.isNotEmpty){
        ToastEngine.show("${result.errorMsg}");
      }
    }
    return result.convert();
  }

  // 获取 news 详情
  Future<HttpResult<Object>> fetchNewsDetail(
      Map<String, dynamic>? data, {
        CancelToken? cancelToken,
      }) async {
    Map<String, dynamic> params = {};

    params = data!;

    Map<String, String> headers = {};

    headers["Content-Type"] = "application/x-www-form-urlencoded";
    headers["Accept"] = "application/x.yyjobs-api.v1+json";

    final result = await dioEngine.requestNetResult(
      // ApiConstants.apiServerTime, // api 地址
      '/api/v1/user/property/news/detail', // api 地址
      params: params,
      headers: headers,
      method: HttpMethod.GET,
      isShowLoadingDialog: false,  //是否展示默认的Loading弹窗
      networkDebounce: true,   //是否防抖防止重复请求
      cancelToken: cancelToken,
    );

    Log.d("------请求返回的result--$result--------");
    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();
      var data = PropertyNewsDetailEntity.fromJson(json!);
      //重新赋值data或list
      return result.convert<PropertyNewsDetailEntity>(data: data);
    }else {
      if(result.errorMsg != null && result.errorMsg!.isNotEmpty){
        ToastEngine.show("${result.errorMsg}");
      }
    }
    return result.convert();
  }


  // 点赞/取消点赞
  Future<HttpResult<Object>> fetchPropertyNewsLikeClick(
      Map<String, dynamic>? data, {
        CancelToken? cancelToken,
      }) async {
    Map<String, dynamic> params = {};

    params = data!;

    Map<String, String> headers = {};

    headers["Content-Type"] = "application/x-www-form-urlencoded";
    headers["Accept"] = "application/x.yyjobs-api.v1+json";

    final result = await dioEngine.requestNetResult(
      // ApiConstants.apiServerTime, // api 地址
      '/api/v1/user/property/like/click', // api 地址
      params: params,
      headers: headers,
      method: HttpMethod.POST,
      isShowLoadingDialog: true,  //是否展示默认的Loading弹窗
      networkDebounce: true,   //是否防抖防止重复请求
      cancelToken: cancelToken,
    );

    Log.d("------请求返回的result--$result--------");
    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();

      if(json!=null && json['data'] == true){
        return result.convert<bool>(data: true);
      }else {
        return result.convert<bool>(data: false);
      }
    }else {
      if(result.errorMsg != null && result.errorMsg!.isNotEmpty){
        ToastEngine.show("${result.errorMsg}");
      }
    }
    return result.convert();
  }

  // 获取 transaction 列表 （rent sale）
  Future<HttpResult<Object>> fetchTransactionList(
      Map<String, dynamic>? data, {
        CancelToken? cancelToken,
      }) async {
    Map<String, dynamic> params = {};

    params = data!;

    Map<String, String> headers = {};

    headers["Content-Type"] = "application/x-www-form-urlencoded";
    headers["Accept"] = "application/x.yyjobs-api.v1+json";

    final result = await dioEngine.requestNetResult(
      // ApiConstants.apiServerTime, // api 地址
      '/api/v1/user/property/transaction/list', // api 地址
      params: params,
      headers: headers,
      method: HttpMethod.GET,
      isShowLoadingDialog: false,  //是否展示默认的Loading弹窗
      networkDebounce: true,   //是否防抖防止重复请求
      cancelToken: cancelToken,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();
      var data = PropertySaleRentEntity.fromJson(json!);
      //重新赋值data或list
      return result.convert(data: data);
    }else {
      if(result.errorMsg != null && result.errorMsg!.isNotEmpty){
        ToastEngine.show("${result.errorMsg}");
      }
    }
    return result.convert();
  }

}

