import 'package:betting/commom/constants/api_constant.dart';
import 'package:betting/data/base_service/api_response.dart';
import 'package:betting/data/base_service/respone_data.dart';
import 'package:betting/data/model/base_model.dart';
import 'package:betting/data/model/res/infor_team/infor_team.dart';
import 'package:betting/data/model/res/match_event/match_event_model.dart';
import 'package:betting/data/model/res/matches/matches.dart';
import 'package:betting/data/model/res/stats/stats.dart';
import '../../base_service/api_service.dart';


class MatchesRepo {
  Future<ResponseData<List<ItemsMatches>>> getListMatchesFavourites(String status) async {
    ResponseData<List<ItemsMatches>> streamEvent = ResponseData(state: Status.LOADING);

    var param = <String, dynamic>{};
    var urlApi = "${ApiConstants.API_MATCHES}/favourite?status=$status";
    ApiResponse<BaseModel> apiResponse =
    await ApiService(urlApi, request: param)
        .request(Request.GET);

    if (apiResponse.status == Status.SUCCESS) {
      List<ItemsMatches> data = [];
      apiResponse.data?.data.map((e) {
        data.add(ItemsMatches.fromJson(e));
      }).toList();
      streamEvent = ResponseData(state: Status.SUCCESS, object: data);
    }

    if (apiResponse.status == Status.ERROR) {
      streamEvent = ResponseData(
          state: Status.ERROR,
          errorCode: apiResponse.error,
          message: apiResponse.error?.message);
    }
    return streamEvent;
  }


  Future<ResponseData<List<ItemsMatches>>> getListMatches(date, String? competitionId, String? teamId, String? status) async {
    ResponseData<List<ItemsMatches>> streamEvent = ResponseData(state: Status.LOADING);

    var param = <String, dynamic>{};
    var urlApi = "${ApiConstants.API_MATCHES}?date=$date&competitionId=$competitionId&teamId=$teamId&status=$status";
    ApiResponse<BaseModel> apiResponse =
    await ApiService(urlApi, request: param)
        .request(Request.GET);

    if (apiResponse.status == Status.SUCCESS) {
      List<ItemsMatches> data = [];
      apiResponse.data?.data.map((e) {
        data.add(ItemsMatches.fromJson(e));
      }).toList();
      streamEvent = ResponseData(state: Status.SUCCESS, object: data);
    }

    if (apiResponse.status == Status.ERROR) {
      streamEvent = ResponseData(
          state: Status.ERROR,
          errorCode: apiResponse.error,
          message: apiResponse.error?.message);
    }
    return streamEvent;
  }


  Future<ResponseData<List<Stats>>> getStatsMatches(String matchId) async {
    ResponseData<List<Stats>> streamEvent = ResponseData(state: Status.LOADING);

    var param = <String, dynamic>{};
    var urlApi = "${ApiConstants.API_STATS}?matchId=$matchId";
    ApiResponse<BaseModel> apiResponse =
    await ApiService(urlApi, request: param)
        .request(Request.GET);

    if (apiResponse.status  == Status.SUCCESS) {
      List<Stats> data = [];
      apiResponse.data?.data.map((e) {
        data.add(Stats.fromJson(e));
      }).toList();
      streamEvent = ResponseData(state: Status.SUCCESS, object: data);
    }

    if (apiResponse.status == Status.ERROR) {
      streamEvent = ResponseData(
          state: Status.ERROR,
          errorCode: apiResponse.error,
          message: apiResponse.error?.message);
    }
    return streamEvent;
  }




  Future<ResponseData<BaseModel>> addMatchesFavorites(String idMatches) async {
    ResponseData<BaseModel> streamEvent = ResponseData(state: Status.LOADING);
    var param = <String, dynamic>{};
    param["matchId"] = idMatches;
    var urlApi = "${ApiConstants.API_MATCHES}/favourite";
    ApiResponse<BaseModel> apiResponse =
    await ApiService(urlApi, request: param)
        .request(Request.POST);

    if (apiResponse.status == Status.SUCCESS) {
      streamEvent =
          ResponseData(state: Status.SUCCESS, object: apiResponse.data);
    }

    if (apiResponse.status == Status.ERROR) {
      streamEvent = ResponseData(
          state: Status.ERROR,
          errorCode: apiResponse.error,
          message: apiResponse.error?.message);
    }
    return streamEvent;
  }



  Future<ResponseData<BaseModel>> deleteMatchesFavorites(String idMatchesFavourite) async {
    ResponseData<BaseModel> streamEvent = ResponseData(state: Status.LOADING);

    var param = <String, dynamic>{};
    var urlApi = "${ApiConstants.API_MATCHES}/favourite/$idMatchesFavourite";
    ApiResponse<BaseModel> apiResponse =
    await ApiService(urlApi, request: param)
        .request(Request.DELETE);

    if (apiResponse.status == Status.SUCCESS) {
      streamEvent =
          ResponseData(state: Status.SUCCESS, object: apiResponse.data);
    }

    if (apiResponse.status == Status.ERROR) {
      streamEvent = ResponseData(
          state: Status.ERROR,
          errorCode: apiResponse.error,
          message: apiResponse.error?.message);
    }
    return streamEvent;
  }

  Future<ResponseData<InforTeam>> inforMatches(String id) async{
    ResponseData<InforTeam> streamEvent = ResponseData(state: Status.LOADING);

    var param = <String, dynamic>{};
    var urlApi = "${ApiConstants.API_MATCHES}/info/$id";
    ApiResponse<BaseModel> apiResponse =
    await ApiService(urlApi, request: param)
        .request(Request.GET);

    if (apiResponse.status == Status.SUCCESS) {
      InforTeam dataRes = InforTeam.fromJson(apiResponse.data?.data);
      streamEvent = ResponseData(state: Status.SUCCESS, object: dataRes);
    }

    if (apiResponse.status == Status.ERROR) {
      streamEvent = ResponseData(
          state: Status.ERROR,
          errorCode: apiResponse.error,
          message: apiResponse.error?.message);
    }
    return streamEvent;
  }

  Future<ResponseData<List<MatchEvent>>> getMatchEvent(String matchId) async {
    ResponseData<List<MatchEvent>> streamEvent = ResponseData(state: Status.LOADING);

    var param = <String, dynamic>{};
    var urlApi = "${ApiConstants.API_SUMARRY}?matchId=$matchId";
    ApiResponse<BaseModel> apiResponse =
    await ApiService(urlApi, request: param)
        .request(Request.GET);

    if (apiResponse.status  == Status.SUCCESS) {
      List<MatchEvent> data = [];
      apiResponse.data?.data.map((e) {
        data.add(MatchEvent.fromJson(e));
      }).toList();
      streamEvent = ResponseData(state: Status.SUCCESS, object: data);
    }

    if (apiResponse.status == Status.ERROR) {
      streamEvent = ResponseData(
          state: Status.ERROR,
          errorCode: apiResponse.error,
          message: apiResponse.error?.message);
    }
    return streamEvent;
  }
}