import 'package:flutter/cupertino.dart';
import 'package:grostore/api_request.dart';
import 'package:grostore/configs/app_config.dart';
import 'package:grostore/helpers/route.dart';
import 'package:grostore/helpers/shared_value_helper.dart';
import 'package:grostore/middlewares/banned_user_middleware.dart';
import 'package:grostore/models/auth/login_response_model.dart';
import 'package:grostore/models/auth/registration_response_model.dart';
import 'package:grostore/models/auth/token_check_response.dart';
import 'package:grostore/models/common/user_info.dart';
import 'package:grostore/screens/auth/login.dart';

class AuthApi{

  /// Login
static Future<LoginResponse> login(String requestBody,BuildContext context)async{
  var url = "${AppConfig.apiUrl}/login";
  Map<String,String> header = {
  "Accept": "application/json",
  "Content-Type": "application/json",
  "App-Language": app_language.$,
  };
    ApiResponse response = await ApiRequest.post(url:url, header:header,body: requestBody,middleWare:BannedMiddleware(context));
    debugPrint("Respons:::${response.body}:::");
    if(response.result){
      return loginResponseFromJson(response.body);
    }else{
      return loginResponseDefaultValue();
    }
  }

  /// Token chack
static Future<TokenCheckResponse> tokenCheck(BuildContext context)async{
  var url = "${AppConfig.apiUrl}/token-check";
  Map<String,String> header = {
  "Accept": "application/json",
  "Content-Type": "application/json",
  "App-Language": app_language.$,
  "Authorization": "Bearer ${access_token.$}",
  };
  print(access_token.$);
    ApiResponse response = await ApiRequest.get(url, header,middleWare:BannedMiddleware(context));
    debugPrint(response.body);
    if(response.result){
      return tokenCheckResponseFromJson(response.body);
    }else
    //   if(response.statusCode != 200){
    //   access_token.$ = "";
    //   MakeRoute.goAndRemoveAll(context, Login());
    // }else
    {
      return TokenCheckResponse(result: false, user: UserInfo.init());
    }
  }

  // logout
static Future<TokenCheckResponse> logout(BuildContext context)async{
  var url = "${AppConfig.apiUrl}/logout";
  Map<String,String> header = {
  "Accept": "application/json",
  "Content-Type": "application/json",
  "App-Language": app_language.$,
  "Authorization": "Bearer ${access_token.$}",
  };

  print("LogoOUt::: $url ::");
    ApiResponse response = await ApiRequest.get(url, header,middleWare:BannedMiddleware(context));
    debugPrint(response.body);
    if(response.result){
      return tokenCheckResponseFromJson(response.body);
    }else{
      return TokenCheckResponse(result: false, user: UserInfo.init());
    }
  }

  /// Regster
static Future<RegistrationResponses> registration(BuildContext context,String postBody)async{
  var url = "${AppConfig.apiUrl}/register";
  Map<String,String> header = {
  "Accept": "application/json",
  "Content-Type": "application/json",
  "App-Language": app_language.$
  };
    ApiResponse response = await ApiRequest.post(url:url, header:header, body: postBody, middleWare:BannedMiddleware(context));

    debugPrint("Register response:: ${response.body} ::");
    if(response.result){
      return registrationResFromJson(response.body);
    }else{
      return registrationResponsesDefault();
    }
  }

  /// Forget password
static Future<RegistrationResp> forgetPassword(BuildContext context,String postBody)async{
  var url = "${AppConfig.apiUrl}/forgotPassword";
  Map<String,String> header = {
  "Accept": "application/json",
  "Content-Type": "application/json",
  "App-Language": app_language.$
  };
  print("}} $url {{");
    ApiResponse response = await ApiRequest.post(url:url, header:header, body: postBody,middleWare:BannedMiddleware(context));
    debugPrint("Status Info}} ${response.result} {{");
    // debugPrint("}} ${response.body} {{");
    if(response.result){
      return registrationRespFromJson(response.body);
    }else{
      return registrationRespDefault();
    }
  }

  /// Otp
  static Future<RegistrationResponse> forgetOTP(BuildContext context, String postBody)async{
  var url = "${AppConfig.apiUrl}/verify";
  Map<String,String> header = {
  "Accept": "application/json",
  "Content-Type": "application/json",
  "App-Language": app_language.$
  };
  print("URl:: $url  ::");
    ApiResponse response = await ApiRequest.post(url: url, header: header, body: postBody);
    debugPrint("Otp::: ${response.body} :::");
    if(response.result){
      return registrationResponseFromJson(response.body);
    }else{
      return registrationResponseDefault();
    }
  }



}