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

class FilterApi {
  static String className = 'FilterApi';

  static Future<List<FilterModel>?> getFilters(int categoryId) async {
    final String fnName = 'getFilters';

    List<FilterModel> filters = [];
    try {
      String path = Constants.BASE_URL + 'categories/$categoryId/filters';

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

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

      for (final filter in response['attributes']) {
        filters.add(FilterModel.fromJson(filter));
      }

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

  static Future<List<SortModel>> getSortMethods() async {
    final String fnName = 'getSortMethods';

    // await Future.delayed(const Duration(milliseconds: 5000), () {});

    List<SortModel> list = [];

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

      list.add(
        new SortModel(
          id: 1,
          label: 'normal_order'.tr,
          queryParameters: {},
        ),
      );

      list.add(
        new SortModel(
          id: 2,
          label: 'expensive_first'.tr,
          queryParameters: {'sort': 'price', 'order': 'desc'},
        ),
      ); //?sort=price&order=desc

      list.add(
        new SortModel(
          id: 3,
          label: 'cheaper_first'.tr,
          queryParameters: {'sort': 'price', 'order': 'asc'},
        ),
      ); //?sort=price&order=asc

      // list.add(new SortModel(id: 4, label: 'Discount'));

      // String path = Constants.BASE_URL + 'categories/$categoryId/filters';

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

      // debugPrint('getFilters response $response');
      return list;
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName');
      return [];
    }
  }

  static Future<List<PriceFilterModel>> getPriceFilters() async {
    final String fnName = 'getPriceFilters';

    List<PriceFilterModel> list = [];

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

      list.add(new PriceFilterModel(
        id: 1,
        label: '0 m. - 100 m.',
        start: 0.0,
        end: 100.0,
        queryValue: '0,100',
      ));
      list.add(new PriceFilterModel(
        id: 2,
        label: '100 m. - 250 m.',
        start: 100.0,
        end: 250.0,
        queryValue: '100,250',
      ));
      list.add(new PriceFilterModel(
        id: 3,
        label: '250 m. - 500 m.',
        start: 250.0,
        end: 500.0,
        queryValue: '250,500',
      ));
      list.add(new PriceFilterModel(
        id: 4,
        label: '500 m. - 900 m.',
        start: 500.0,
        end: 900.0,
        queryValue: '500,900',
      ));
      list.add(new PriceFilterModel(
        id: 5,
        label: '900 m. - 3000 m.',
        start: 900.0,
        end: 3000.0,
        queryValue: '900,3000',
      ));

      list.add(new PriceFilterModel(
        id: 6,
        label: '3000 m. - 40000 m.',
        start: 3000.0,
        end: 40000.0,
        queryValue: '900,3000',
      ));

      return list;
    } catch (e) {
      debugPrint('ERROR: class: $className, method: $fnName');
      return [];
    }
  }

  static Future<List<FilterOption>> getOptions(int page, int itemsPerPage, FilterModel model, String searchKey) async {
    debugPrint('page:$page, model:${model.code}' ', id:${model.id}');
    // Attribute (Brand, Size, Style, Material ...)

    try {
      List<FilterOption> options = [];
      final String categoryId = getCategoryIdFromFilterableQueryParams();

      String path = Constants.BASE_URL + 'attribute-options';
      final Map<String, dynamic> params = {
        'locale': await getLocale(),
        'attribute_id': model.id,
        'category': categoryId,
        'limit': itemsPerPage,
        'page': page,
        'search': searchKey,
      };

      final response = await HttpUtil().get(path: path, queryParameters: params);
      for (final option in response['data']) {
        options.add(FilterOption.fromJson(option));
      }
      return options;
    } catch (e) {
      debugPrint('error : $e');
      throw e;
    }
  }

  static String getCategoryIdFromFilterableQueryParams() {
    FilterController fc = Get.find();
    final queryParams = fc.state.filterableQueryParams;

    String categoryId = '1';

    if (queryParams['category_id'] != null) {
      categoryId = queryParams['category_id'].toString();
    }

    return categoryId;
  }
}
