part of 'repositories.dart';

class AuthRepository implements IAuth<AuthModel?, LoginParam> {
  @override
  void dispose() {}

  @override
  Future<void> init() async {}

  @override
  Future<AuthModel?> signIn(LoginParam? param) async {
    AuthModel auth;
    String url = '${SharedValue.api}/login/store';
    String userAgent = await initUserAgentState();
    var response = await http.post(Uri.parse(url), body: {
      "email": param!.email,
      "password": param.password
    }, headers: {
      "User-Agent" : userAgent
    });
    if (response.statusCode == 200) {
      auth = AuthModel.fromJson(response.body);
    } else {
      final responseData = json.decode(response.body);
      Map checkResult = checkAPIResultStatus(responseData);
      if (checkResult['error']) {
        throw AuthException(checkResult['message']);
      }
      throw AuthException('There was an error. Please try again');
    }
    return auth;
  }

  @override
  Future<String> signOut(LoginParam? param) async {
    String url = '${SharedValue.api}/logout?token=${param!.token}';
    String userAgent = await initUserAgentState();
    var response = await http.post(Uri.parse(url), body: {
      "userId": param.userId.toString()
    }, headers: {
      "User-Agent" : userAgent
    });
    if (response.statusCode == 200) {
      return '200';
    } else {
      return response.statusCode.toString();
    }
  }

  @override
  Future<AuthModel?> signUp(LoginParam? param) {
    // TODO: implement signUp
    throw UnimplementedError();
  }
}