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

import '../../app.dart';
import 'package:flutter/material.dart';

class WishlistApi {
  static String className = 'WishlistApi';

  static Future<List<WishlistModel>> get(Map<String, dynamic> params) async {
    final String fnName = 'get';

    try {
      String path = Constants.BASE_URL + 'customer/wishlist';

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

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

      return WishlistModel.listFromJson(response['data'] as List);
    } 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 throw new Exception('Error occurred');
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName');
      showSnack('error'.tr, 'error_occurred'.tr, SnackType.ERROR);
      throw new Exception('Error occurred');
    }
  }

// add and remove from wishlist
  static Future<bool> addRemove(int productId) async {
    final String fnName = 'addRemove';

    try {
      String path = Constants.BASE_URL + 'customer/wishlist/$productId';

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

      final response = await HttpUtil().post(
        path,
        queryParameters: {
          'locale': await getLocale(),
        },
      );

      showSnack('success'.tr, response['message']);
      return true;
    } 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 false;
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName');
      showSnack('error', 'can_not_add_item_to_wishlist'.tr, SnackType.ERROR);
      return false;
    }
  }
}
