import 'dart:convert';
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';
import 'package:traderscircuit/Utils/utils.dart';
import '../../Utils/base_manager.dart';
import 'base_api_services.dart';

class NetworkApiServices extends BaseApiServices {
  Dio dio = Dio();
  String basicAuth = 'Basic ' +
      base64.encode(
          utf8.encode('traderCircuitUser:71%@L%es^bUX94`J9XT*@bh,._WWM{'));
  @override
  Future<ResponseData> getApi(String url, {bool isAuth = false}) async {
    if (kDebugMode) {
      print("api url is >>> $url");
    }
    Response response;
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? token = prefs.getString('accessToken').toString();
    log(token);
    try {
      response = await dio.get(url,
          options: (token == null || token == "")
              ? Options(
                  headers: {
                    "Authorization": basicAuth,
                  },
                )
              : Options(headers: {
                  "Authorization": basicAuth,
                  'access-token': token,
                }));

      if (response.statusCode == 200) {
        return ResponseData<dynamic>(
          "success",
          data: response.data,
          ResponseStatus.SUCCESS,
        );
      } else {
        try {
          return ResponseData<dynamic>(
              response.data['message'].toString(), ResponseStatus.FAILED);
        } catch (_) {
          return ResponseData<dynamic>(
              response.statusMessage!, ResponseStatus.FAILED);
        }
      }
    } on Exception catch (e) {
      if (e is DioException) {
        if (e.response == null) {
          //   Get.to(() => const ErrorScreen());
        } else {}
      }
      return ResponseData<dynamic>(
          'Oops something Went Wrong', ResponseStatus.FAILED);
    }
  }

  @override
  Future<ResponseData> postApi(
    data,
    String url,
  ) async {
    if (kDebugMode) {
      print("data >>> $data");
      print("api url is >>> $url");
    }
    Response response;

    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? token = prefs.getString('accessToken');
    try {
      response = await dio.post(url,
          data: data,
          options: (token == null || token == "")
              ? Options(
                  headers: {
                    "Authorization": basicAuth,
                  },
                )
              : Options(headers: {
                  "Authorization": basicAuth,
                  'access-token': token,
                }));
    } on Exception catch (e) {
      if (e is DioException) {
        log(e.response.toString());
        if (e.response == null) {
          //  Get.to(() => const ErrorScreen());
        }
      }
      return ResponseData<dynamic>(
          'Oops something Went Wrong', ResponseStatus.FAILED);
    }

    if (response.statusCode == 200) {
      return ResponseData<dynamic>("success", ResponseStatus.SUCCESS,
          data: response.data);
    } else if (response.statusCode == 203) {
      print(response.data);
      return ResponseData<dynamic>("validation", ResponseStatus.PRIVATE,
          data: response.data);
    } else if (response.statusCode == 202) {
      print(response.data);
      return ResponseData<dynamic>("success", ResponseStatus.PRIVATE,
          data: response.data);
    } else {
      try {
        return ResponseData<dynamic>(
            response.data['message'].toString(), ResponseStatus.FAILED);
      } catch (_) {
        return ResponseData<dynamic>(
            response.statusMessage!, ResponseStatus.FAILED);
      }
    }
  }

  @override
  Future<ResponseData> deleteApi(String url, data) async {
    if (kDebugMode) {
      print("api url is >>> $url");
    }
    Response response;
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? token = prefs.getString('accessToken').toString();
    log(token);
    try {
      response = await dio.delete(url,
          data: data,
          options: Options(headers: {
            'authorization': "Bearer $token",

            // "device-id": deviceId
          }));
    } on Exception catch (_) {
      return ResponseData<dynamic>(
          'Oops something Went Wrong', ResponseStatus.FAILED);
    }
    if (response.statusCode == 200) {
      return ResponseData<dynamic>(
        "success",
        data: response.data,
        ResponseStatus.SUCCESS,
      );
    } else if (response.statusCode == 203) {
      Utils.showToast(response.data["message"]);
      return ResponseData<dynamic>("validation", ResponseStatus.PRIVATE,
          data: response.data);
    } else {
      try {
        return ResponseData<dynamic>(
            response.data['message'].toString(), ResponseStatus.FAILED);
      } catch (_) {
        return ResponseData<dynamic>(
            response.statusMessage!, ResponseStatus.FAILED);
      }
    }
  }
}
