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 '../../base_service/api_service.dart';
import '../../model/res/teams/teams.dart';


class TeamsRepo {
  Future<ResponseData<Teams>> getListTeam(int page, int size, String? name) async {
    ResponseData<Teams> streamEvent = ResponseData(state: Status.LOADING);

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

    if (apiResponse.status == Status.SUCCESS) {
      Teams dataRes = Teams.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<BaseModel>> addTeamFollow(List<ItemTeams> listTeamFollow,listNotifyMatch,listNotifyNews) async {
    ResponseData<BaseModel> streamEvent = ResponseData(state: Status.LOADING);
    var maps = <Map>[];
    var list =  <dynamic>[];
    for(int i = 0;i<listTeamFollow.length;i++){
      maps.add(Map());
      maps[i]["teamId"] = listTeamFollow[i].id;
      maps[i]["notifyMatch"] = listNotifyMatch[i];
      maps[i]["notifyNews"] = listNotifyNews[i];
      list.add(maps[i]);
    }

    var urlApi = "${ApiConstants.API_TEAMS}/favourite";
    ApiResponse<BaseModel> apiResponse =
    await ApiService(urlApi, request: list)
        .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;
  }

  /// todo: get favourite team by user
  Future<ResponseData<List<ItemTeams>>> getFavouriteTeams() async {
    ResponseData<List<ItemTeams>> streamEvent = ResponseData(state: Status.LOADING);
    var param = <String, dynamic>{};
    var urlApi = "${ApiConstants.API_TEAMS}/favourite";
    ApiResponse<BaseModel> apiResponse =
    await ApiService(urlApi, request: param).request(Request.GET);

    if (apiResponse.status == Status.SUCCESS) {
      List<ItemTeams> data = [];
      apiResponse.data?.data.map((e) {
        data.add(ItemTeams.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;
  }

  /// todo: unfavorite team
  Future<ResponseData<BaseModel>> delFavouriteTeam(String id) async{
    ResponseData<BaseModel> streamEvent = ResponseData(state: Status.LOADING);
    String uri = '${ApiConstants.API_TEAMS}/favourite/$id';
    var param = <String, dynamic>{};
    ApiResponse<BaseModel> apiResponse = await ApiService(uri, 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>> inforTeam(String id) async{
    ResponseData<InforTeam> streamEvent = ResponseData(state: Status.LOADING);

    var param = <String, dynamic>{};
    var urlApi = "${ApiConstants.API_TEAMS}/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;
  }
}