import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../app.dart';

class ShoppingCartApi {
  static String className = 'ShoppingCartApi';

  static Future<CartModel?> get() async {
    final String fnName = 'get';

    try {
      debugPrint('class: $className, method: $fnName');

      final String path = Constants.BASE_URL + 'customer/cart';

      final response = await HttpUtil().get(
        path: path,
        queryParameters: {'currency': 'tmt'},
      );

      if (response['data'] == null) return null;

      CartModel cartModel = CartModel.fromJson(response['data']);

      return cartModel;
    } on DioError catch (error) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $error ');

      int? errCode = error.response?.statusCode;

      if (errCode == 401) {
        showSnack('warning'.tr, 'please_login'.tr, SnackType.WARNING);
      }

      return null;
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $e ');
      showSnack('error'.tr, 'error_occurred'.tr, SnackType.ERROR);
      return null;
    }
  }

  static Future<CartModel?> add(BuildContext context, Map<String, dynamic> params, int productId) async {
    final String fnName = 'add';

    try {
      debugPrint('class: $className, method: $fnName , params: $params');

      final String path = Constants.BASE_URL + 'customer/cart/add/$productId';

      final response = await HttpUtil().post(path, queryParameters: params);

      final cartModel = CartModel.fromJson(response['data']);

      // showSnack('SUCCESS', 'Item added to cart');
      showSnack('success'.tr, 'item_added'.tr);

      return cartModel;
    } on DioError catch (error) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $error ');

      int? errCode = error.response?.statusCode;

      if (errCode == 401) {
        Get.offNamedUntil('/', (route) => false);
        navigateToLoginScreen();

        showSnack('warning'.tr, 'please_login'.tr, SnackType.WARNING);
      }

      if (errCode == 405) {
        showSnack('warning'.tr, 'qty_not_available'.tr, SnackType.WARNING);
      }

      return null;
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $e ');

      showSnack('error'.tr, 'error_occurred'.tr, SnackType.ERROR);
      return null;
    }
  }

  static Future<CartModel?> update(int cartItemId, int quantity) async {
    final String fnName = 'update';

    try {
      final Map<String, dynamic> params = {
        'qty': {
          '$cartItemId': quantity,
        },
        'locale': await getLocale(),
      };

      debugPrint('class: $className, method: $fnName , params: $params');

      final String path = Constants.BASE_URL + 'customer/cart/update';

      final response = await HttpUtil().put(path, queryParameters: params);

      if (!response['success']) {
        showSnack('warning'.tr, response['message'], SnackType.WARNING);
        return null;
      }

      showSnack('success'.tr, response['message']);

      CartModel cartModel = CartModel.fromJson(response['data']);

      return cartModel;
    } on DioError catch (error) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $error ');

      int? errCode = error.response?.statusCode;

      debugPrint('error $error');

      if (errCode == 401) showSnack('warning'.tr, 'please_login'.tr, SnackType.WARNING);

      return null;
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $e ');
      showSnack('error'.tr, 'error_occurred'.tr, SnackType.ERROR);
      return null;
    }
  }

  static Future<CartModel?> remove(int cartItemId) async {
    final String fnName = 'remove';

    try {
      debugPrint('class: $className, method: $fnName');

      final String path = Constants.BASE_URL + 'customer/cart/remove/$cartItemId';

      final response = await HttpUtil().delete(path);

      showSnack('success'.tr, 'item_removed'.tr);

      if (response['data'] == null) {
        return null;
      }

      return CartModel.fromJson(response['data']);
    } on DioError catch (error) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $error ');

      int? errCode = error.response?.statusCode;

      if (errCode == 401) {
        showSnack('warning'.tr, 'please_login'.tr, SnackType.WARNING);
      }

      return null;
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $e ');
      showSnack('error'.tr, 'error_occurred'.tr, SnackType.ERROR);
      return null;
    }
  }

  static Future<bool> empty() async {
    final String fnName = 'empty';

    try {
      debugPrint('class: $className, method: $fnName');

      final String path = Constants.BASE_URL + 'customer/cart/empty';

      await HttpUtil().delete(path);

      return true;
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $e ');
      return false;
    }
  }

  static Future<CartModel?> applyCoupon(String code) async {
    final String fnName = 'applyCoupon';

    try {
      debugPrint('class: $className, method: $fnName , code: $code');

      String path = Constants.BASE_URL + 'customer/cart/coupon';

      final Map<String, dynamic> params = {
        'code': code,
        'locale': await getLocale(),
      };

      final response = await HttpUtil().post(path, queryParameters: params);

      if (response['success']) {
        showSnack('success'.tr, response['message']);
        return CartModel.fromJson(response['data']);
      } else {
        showSnack('warning'.tr, response['message'], SnackType.WARNING);
        return null;
      }
    } on DioError catch (error) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $error ');

      int? errCode = error.response?.statusCode;

      if (errCode == 401) {
        navigateToLoginScreen();

        showSnack('warning'.tr, 'please_login'.tr, SnackType.WARNING);
      }

      if (errCode == 405) {
        showSnack('warning'.tr, 'invalid_coupon_code'.tr, SnackType.WARNING);
      }

      return null;
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $e ');

      showSnack('error'.tr, 'error_occurred'.tr, SnackType.ERROR);
      return null;
    }
  }

  static Future<CartModel?> deleteCoupon() async {
    final String fnName = 'deleteCoupon';

    try {
      debugPrint('class: $className, method: $fnName ');

      String path = Constants.BASE_URL + 'customer/cart/coupon';

      final response = await HttpUtil().delete(path);

      if (response['success']) {
        showSnack('success'.tr, response['message']);
        return CartModel.fromJson(response['data']);
      } else {
        showSnack('warning'.tr, response['message'], SnackType.WARNING);
        return null;
      }
    } on DioError catch (error) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $error ');

      int? errCode = error.response?.statusCode;

      if (errCode == 401) {
        navigateToLoginScreen();

        showSnack('warning'.tr, 'please_login'.tr, SnackType.WARNING);
      }

      if (errCode == 405) {
        showSnack('warning'.tr, 'invalid_coupon_code'.tr, SnackType.WARNING);
      }

      return null;
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName, error: $e ');

      showSnack('error'.tr, 'error_occurred'.tr, SnackType.ERROR);
      return null;
    }
  }
}
