import 'dart:typed_data';

import 'package:domain/entity/feedback_detail_entity.dart';
import 'package:domain/entity/form_option_entity.dart';
import 'package:plugin_basic/constants/app_constant.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/date_time_utils.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/util.dart';

import '../constants/api_constants.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:plugin_basic/provider/http_provider/http_provider.dart';
import '../entity/facility_book_entity.dart';
import '../entity/facility_index_entity.dart';
import '../entity/facility_page_entity.dart';
import '../entity/form_content_entity.dart';
import '../entity/form_detail_entity.dart';
import '../entity/form_list_entity.dart';
import '../entity/form_submitted_page_entity.dart';
import '../entity/id_name_entity.dart';

part 'facility_repository.g.dart';

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

/*
 * 设施的数据仓库
 */
class FacilityRepository {
  DioEngine dioEngine;

  FacilityRepository({required this.dioEngine});

  /// 获取设施可申请的列表
  Future<HttpResult<FacilityIndexEntity>> fetchFacilityList({
    CancelToken? cancelToken,
  }) async {
    final result = await dioEngine.requestNetResult(
      ApiConstants.apiFacilityList,
      method: HttpMethod.GET,
      cancelToken: cancelToken,
    );

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

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

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

  /// 获取设施的指定日期的表
  Future<HttpResult<FacilityBookEntity>> fetchFacilityDateDetail({
    required String? id,
    required DateTime? date,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params['type_id'] = id ?? "";
    if (date != null) {
      params['date'] = DateTimeUtils.formatDate(date, format: 'yyyy-MM-dd');
    }

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiFacilityDate,
      params: params,
      method: HttpMethod.GET,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = FacilityBookEntity.fromJson(json!);
      return result.convert<FacilityBookEntity>(data: data);
    }
    return result.convert();
  }

  /// 预约设施
  Future<HttpResult<FacilityDetail>> submitBooking({
    required String? periodId,
    required DateTime date,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};

    params["period_id"] = periodId ?? "";
    params["date"] = DateTimeUtils.formatDate(date, format: 'yyyy-MM-dd');

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiFacilityBook,
      params: params,
      method: HttpMethod.POST,
      networkDebounce: true,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = FacilityDetail.fromJson(json!);
      return result.convert<FacilityDetail>(data: data);
    }
    return result.convert();
  }

  /// 获取已经申请的设施可用列表
  Future<HttpResult<FacilityPageEntity>> fetchFacilityActiveList({
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params['page'] = curPage.toString();
    params['limit'] = "10";

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiFacilityActiveList,
      params: params,
      method: HttpMethod.GET,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = FacilityPageEntity.fromJson(json!);
      return result.convert<FacilityPageEntity>(data: data);
    }
    return result.convert();
  }

  /// 获取已经申请的设施押金列表
  Future<HttpResult<FacilityPageEntity>> fetchFacilityDepositList({
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params['page'] = curPage.toString();
    params['limit'] = "10";

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiFacilityDepositList,
      params: params,
      method: HttpMethod.GET,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = FacilityPageEntity.fromJson(json!);
      return result.convert<FacilityPageEntity>(data: data);
    }
    return result.convert();
  }

  /// 获取已经申请的设施历史列表
  Future<HttpResult<FacilityPageEntity>> fetchFacilityHistoryList({
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params['page'] = curPage.toString();
    params['limit'] = "10";

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiFacilityHistoryList,
      params: params,
      method: HttpMethod.GET,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = FacilityPageEntity.fromJson(json!);
      return result.convert<FacilityPageEntity>(data: data);
    }
    return result.convert();
  }

  /// 获取预约Booking的详情
  Future<HttpResult<FacilityDetail>> fetchFacilityBookingDetail({
    required String? id,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params['id'] = id ?? "";

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiFacilityBookingDetail,
      params: params,
      isShowLoadingDialog: true,
      networkDebounce: true,
      method: HttpMethod.GET,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = FacilityDetail.fromJson(json!);
      return result.convert<FacilityDetail>(data: data);
    }
    return result.convert();
  }

}
