

import 'package:on_field_work/helpers/widgets/my_display_type.dart';
import 'package:on_field_work/helpers/widgets/my_screen_media_type.dart';

class MyScreenMedia {
  static int flexColumns = 12;
  static double flexSpacing = 24;

  static MyScreenMediaType getTypeFromWidth(double width) {
    for (var i in MyScreenMediaType.values) {
      if (width < i.width) {
        return i;
      }
    }
    return MyScreenMediaType.xxl;
  }

  static Map<MyScreenMediaType, T> getFilledMedia<T>(
      Map<MyScreenMediaType, T>? map, T defaultValue,
      [bool reversed = false]) {
    Map<MyScreenMediaType, T> d = {};
    map ??= {};
    List list = MyScreenMediaType.list;
    if (reversed) {
      list = list.reversed.toList();
    }
    for (var i = 0; i < list.length; i++) {
      d[list[i]] =
          map[list[i]] ?? (i > 0 ? d[list[i - 1]] : null) ?? defaultValue;
    }
    return d;
  }

  static Map<MyScreenMediaType, double> getFlexedDataFromString(String? string) {
    string ??= "";
    Map<MyScreenMediaType, double> d = {};

    List<String> data = string.split(" ");
    for (String item in data) {
      for (var type in MyScreenMediaType.values) {
        if (item.contains(type.className)) {
          double? flex = double.tryParse(item.replaceAll("${type.className}-", ""));
          if (flex != null) {
            d[type] = flex;
            break;
          }
        }
      }
    }

    return getFilledMedia<double>(d, MyScreenMedia.flexColumns.toDouble());
  }

  static Map<MyScreenMediaType, MyDisplayType> getDisplayDataFromString(
      String? string) {
    string ??= "";
    Map<MyScreenMediaType, MyDisplayType> d = {};

    List<String> data = string.split(" ");
    for (String item in data) {
      for (var type in MyScreenMediaType.values) {
        if (item.contains(type.className)) {
          MyDisplayType displayType = MyDisplayType.fromString(
              item.replaceAll("${type.className}-", ""));
          d[type] = displayType;
          break;
        }
      }
    }

    return getFilledMedia(d, MyDisplayType.block);
  }
}
