import 'dart:convert';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;

import '../../constant/credential.dart';

class CRUD {
  Future<dynamic> get({
    required String link,
    Map<String, dynamic>? payload,
  }) async {
    var url = Uri.parse(
      link,
    );
    var response = await http.post(
      url,
      body: payload,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        'Authorization':
            'Basic ${base64Encode(utf8.encode(AppCredintials.basicAuthCredentials))}',
      },
    );
    print("--------------" + response.request.toString());
    print("--------------" + payload.toString());
    if (response.statusCode == 200) {
      var jsonData = jsonDecode(response.body);
      if (jsonData['status'] == 'success') {
        print(jsonData);
        return response.body;
      }
      return jsonData['status'];
    }
  }

  Future<dynamic> postStripe({
    required String link,
    Map<String, dynamic>? payload,
  }) async {
    var url = Uri.parse(
      link,
    );
    var response = await http.post(
      url,
      body: payload,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        'Authorization': 'Bearer ${StripeKey.secretKey}',
      },
    );

    return response.body;
  }

  Future<dynamic> post({
    required String link,
    Map<String, dynamic>? payload,
  }) async {
    var url = Uri.parse(
      link,
    );
    var response = await http.post(
      url,
      body: payload,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        'Authorization':
            'Basic ${base64Encode(utf8.encode(AppCredintials.basicAuthCredentials))}',
      },
    );
    // print(response.request);
    // print(payload);

    var jsonData = jsonDecode(response.body);
    if (response.statusCode == 200) {
      if (jsonData['status'] == 'success') {
        Get.snackbar(
          jsonData['status'],
          jsonData['message'],
        );

        return response.body;
      }
    }
    return (jsonData['status']);
  }

  Future<dynamic> postFromDialogue({
    required String link,
    Map<String, dynamic>? payload,
  }) async {
    var url = Uri.parse(
      link,
    );
    var response = await http.post(
      url,
      body: payload,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        'Authorization':
            'Basic ${base64Encode(utf8.encode(AppCredintials.basicAuthCredentials))}',
      },
    );
    print(response.request);
    print('body=========================');
    print(response.body);

    if (response.body.isNotEmpty) {
      var jsonData = jsonDecode(response.body);
      if (response.statusCode == 200) {
        if (jsonData['status'] == 'success') {
          Get.back();
          Get.snackbar(
            jsonData['status'],
            jsonData['message'],
          );

          return response.body;
        }
      }
      return (jsonData['status']);
    }
  }

  Future<dynamic> getGoogleApi({
    required String link,
    Map<String, dynamic>? payload,
  }) async {
    var url = Uri.parse(
      link,
    );
    var response = await http.post(
      url,
      body: payload,
    );
    print(response.request);
    var jsonData = jsonDecode(response.body);
    // print(jsonData);
    if (jsonData['status'] == 'OK') {
      return jsonData;
    }
    return (jsonData['status']);
  }

  Future<dynamic> update({
    required String endpoint,
    required Map<String, dynamic> data,
    required String id,
  }) async {
    var url = Uri.parse('$endpoint/$id');
    var response = await http.put(
      url,
      body: json.encode(data),
      headers: {
        'Authorization':
            'Basic ${base64Encode(utf8.encode(AppCredintials.basicAuthCredentials))}',
      },
    );
    return json.decode(response.body);
  }

  Future<dynamic> delete({
    required String endpoint,
    required String id,
  }) async {
    var url = Uri.parse('$endpoint/$id');
    var response = await http.delete(
      url,
      headers: {
        'Authorization':
            'Basic ${base64Encode(utf8.encode(AppCredintials.basicAuthCredentials))}',
      },
    );
    return json.decode(response.body);
  }
}
