import 'package:betting/commom/app_cache.dart';
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 '../../base_service/api_service.dart';
import '../../model/res/auth/LoginResponse.dart';

class AuthRepo {
  Future<ResponseData<LoginResponse>> requestToken(
      username, password, fcmToken) async {
    ResponseData<LoginResponse> streamEvent =
    ResponseData(state: Status.LOADING);

    var param = <String, dynamic>{};
    param["fcmToken"] = fcmToken;
    param["password"] = password;
    param["username"] = username;
    param["type"] = "user";
    ApiResponse<BaseModel> apiResponse =
    await ApiService(ApiConstants.API_LOGIN, request: param)
        .request(Request.POST);

    if (apiResponse.status == Status.SUCCESS) {
      LoginResponse dataRes = LoginResponse.fromJson(apiResponse.data?.data);
      var token = dataRes.accessToken ?? "";
      AppCache().setToken(token);
      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;
  }


}