import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/network/request_helper.dart';
// ignore: depend_on_referenced_packages
import 'package:collection/collection.dart';

class ServerDataProvider {
  static final List<MapEntry> directTypes = [];

  static Future<void> getData() async {
    await _getDirectTypes();
  }

  static int labelToTypeId(String label) {
    if (label.contains('می‌خواهم بیشتر بدانم')) {
      return 9;
    } else if (label.contains('پشتیبانی اپلیکیشن')) {
      return 8;
    } else if (label.contains('پشتیبانی محتوا')) {
      return 7;
    } else {
      return directTypes
              .firstWhereOrNull((element) => element.value.contains(label))
              ?.key ??
          7;
    }
  }

  static Future<void> _getDirectTypes() async {
    final service = RequestService(RequestHelper.directTypes);
    await service.httpGet();
    if (service.isSuccess) {
      final types = service.result['types'];
      for (var i = 0; i < types.length; i++) {
        directTypes.add(MapEntry(types[i]['id'], types[i]['label']));
      }
    } else {
      throw 'Fetching direct types failed!';
    }
  }
}
