import 'package:fis_measure/interfaces/date_types/point.dart';
import 'package:fis_measure/interfaces/process/viewports/image_boundary.dart';
import 'package:flutter/foundation.dart';

class BoundaryBase implements IImageBoundary {
  late final List<DPoint> _pointList;
  factory BoundaryBase.fromCorners(
    DPoint topLeft,
    DPoint bottomLeft,
    DPoint bottomRight,
    DPoint topRight,
  ) {
    final boundary = BoundaryBase()
      ..topLeft = topLeft
      ..bottomLeft = bottomLeft
      ..bottomRight = bottomRight
      ..topRight = topRight;
    return boundary;
  }

  BoundaryBase() {
    _pointList = List.generate(4, (index) => DPoint.zero);
  }

  @override
  List<DPoint> get points => _pointList;

  @override
  DPoint get bottomLeft => _pointList[1];
  set bottomLeft(DPoint value) => _pointList[1] = value;

  @override
  DPoint get bottomRight => _pointList[2];
  set bottomRight(DPoint value) => _pointList[2] = value;

  @override
  DPoint get topLeft => _pointList[0];
  set topLeft(DPoint value) => _pointList[0] = value;

  @override
  DPoint get topRight => _pointList[3];
  set topRight(DPoint value) => _pointList[3] = value;

  @override
  double get width => topRight.x - topLeft.x;

  @override
  double get height => bottomLeft.y - topLeft.y;

  @protected
  bool equalTo(BoundaryBase other) {
    return topLeft == other.topLeft &&
        bottomLeft == other.bottomLeft &&
        bottomRight == other.bottomRight &&
        topRight == other.topRight;
  }

  @override
  bool operator ==(Object other) {
    return other is BoundaryBase && equalTo(other);
  }

  @override
  int get hashCode {
    return topLeft.hashCode ^
        bottomLeft.hashCode ^
        bottomRight.hashCode ^
        topRight.hashCode;
  }
}

/// 线阵边界
class LinearBoundary extends BoundaryBase {
  @protected
  @override
  bool equalTo(BoundaryBase other) {
    return other is LinearBoundary && super.equalTo(other);
  }
}

/// 扇阵边界
class ConvexBoundary extends BoundaryBase {
  ConvexBoundary() : super() {
    points.add(DPoint.zero);
  }

  /// 扇阵顶点
  DPoint get vertex => points[4];

  @override
  bool operator ==(Object other) {
    return other is ConvexBoundary && super.equalTo(other);
  }

  @override
  int get hashCode {
    return super.hashCode ^ vertex.hashCode;
  }
}
