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

class ProductApi {
  static String className = 'ProductApi';

  static Future<List<ProductModel>> getHomePageProducts(Map<String, dynamic> params, String url) async {
    final String fnName = 'getHomePageProducts';

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

      String path = Constants.BASE_URL + url;

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

      return ProductModel.listFromJson(response['data'] as List);
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName, params: $params, error: $e ');
      throw new Exception('Error occurred');
    }
  }

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

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

      String path = Constants.BASE_URL + 'products';

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

      return ProductModel.listFromJson(response['data'] as List);
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName, params: $params, error: $e ');
      throw new Exception('Error occurred');
    }
  }

  static Future<ProductModel?> getProductById(int id) async {
    final String fnName = 'getProductById';

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

      String path = Constants.BASE_URL + 'products/$id';

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

      return ProductModel.fromJson(response['data']);
    } catch (e) {
      return null;
    }
  }

  static Future<Map<String, dynamic>?> getProductVariants(int productId) async {
    final String fnName = 'getProductVariants';

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

    try {
      final String path = Constants.BASE_URL + 'products/$productId/variants';
      // final String path = Constants.BASE_URL + 'products/3098/variants';

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

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

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

  static Future<List<ProductAdditionalInfoModel>?> getProductAdditionalInfo(
      int productId, Map<String, dynamic> params) async {
    final String fnName = 'getProductAdditionalInfo';

    final List<ProductAdditionalInfoModel> list = [];

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

      String path = Constants.BASE_URL + 'product-additional-information/$productId';

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

      for (final json in response['data']) {
        list.add(ProductAdditionalInfoModel.fromJson(json));
      }

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

  static Future<List<ReviewModel>?> getProductReviews(int productId) async {
    final String fnName = 'getProductReviews';
    try {
      debugPrint('class: $className, method: $fnName');

      String path = Constants.BASE_URL + 'products/$productId/reviews';

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

      return ReviewModel.listFromJson(response['data'] as List);
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName, productId: $productId, error: $e ');
      return null;
    }
  }

  static Future<bool> createProductReviews(Map<String, dynamic> params, int productId) async {
    final String fnName = 'createProductReviews';
    try {
      debugPrint('class: $className, method: $fnName');

      String path = Constants.BASE_URL + 'customer/reviews/create/$productId';

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

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

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