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

import 'point.dart';

class RectRegion {
  double _left = 0;
  double _top = 0;
  double _right = 0;
  double _bottom = 0;

  RectRegion();

  factory RectRegion.fill(
    double x,
    double y,
    double width,
    double height,
  ) {
    if (width < 0.0 || height < 0.0) {
      logger.e("Size_WidthAndHeightCannotBeNegative($x,$y,$width,$height)");
      width = height = 0;
    }
    final rect = RectRegion()
      .._left = x
      .._top = y;
    rect._right = rect.left + width;
    rect._bottom = rect.top + height;
    return rect;
  }

  factory RectRegion.pointTLBR(
    DPoint topLeft,
    DPoint bottomRight,
  ) {
    final rect = RectRegion()
      .._left = topLeft.x
      .._top = topLeft.y
      .._right = bottomRight.x
      .._bottom = bottomRight.y;
    return rect;
  }

  double get left => _left;
  double get right => _right;
  double get top => _top;
  double get bottom => _bottom;
  double get width => (_right - _left).abs();
  double get height => (_bottom - _top).abs();
  DPoint get topLeft => DPoint(left, top);
  DPoint get topRight => DPoint(right, top);
  DPoint get bottomLeft => DPoint(left, bottom);
  DPoint get bottomRight => DPoint(right, bottom);

  @override
  bool operator ==(Object other) {
    return other is RectRegion &&
        _left == other._left &&
        _top == other._top &&
        _right == other._right &&
        _bottom == other._bottom;
  }

  @override
  int get hashCode =>
      top.hashCode ^ bottom.hashCode ^ left.hashCode ^ right.hashCode;

  @override
  String toString() {
    return 'left:$left,right:$right,top:$top,bottom:$bottom';
  }
}
