import 'package:fis_common/logger/logger.dart';

class DSize {
  static final DSize empty =
      DSize(double.negativeInfinity, double.negativeInfinity);

  late double _width;
  late double _height;

  bool get isEmpty => width < 0.0;

  double get width => _width;
  set width(double value) {
    if (isEmpty) {
      throw UnsupportedError("Size_CannotModifyEmptySize");
    }
    if (value < 0.0) {
      throw ArgumentError("Size_WidthCannotBeNegative");
    }
    _width = value;
  }

  double get height => _height;
  set height(double value) {
    if (isEmpty) {
      throw UnsupportedError("Size_CannotModifyEmptySize");
    }
    if (value < 0.0) {
      throw ArgumentError("Size_HeightCannotBeNegative");
    }
    _height = value;
  }

  DSize(double width, double height) {
    if (width < 0.0 || height < 0.0) {
      logger.e("Size_WidthAndHeightCannotBeNegative($width,$height)");
      width = height = 0;
    }
    _width = width;
    _height = height;
  }

  static DSize parse(String source) {
    DSize size;
    if (source == "Empty") {
      size = empty;
    } else {
      var items = source.split(',');
      if (items.length != 2) {
        throw ArgumentError("$source can not be parsed to DSize");
      }
      size = DSize(double.parse(items[0]), double.parse(items[1]));
    }
    return size;
  }

  @override
  String toString() => '$width,$height';

  @override
  int get hashCode {
    if (isEmpty) {
      return 0;
    }
    return width.hashCode ^ height.hashCode;
  }

  @override
  bool operator ==(Object other) {
    return other is DSize && width == other.width && height == other.height;
  }
}
