# Copyright (c) 2023 Yuki Kimoto
# MIT License

class Point {
  version_from SPVM;

  # Interfaces
  interface Stringable;
  interface Cloneable;
  interface EqualityCheckable;
  
  # Fields
  has x : protected ro int;
  has y : protected ro int;
  
  # Class methods
  static method new : Point ($x : int = 0, $y : int = 0) {
    my $self = new Point;
    
    $self->init($x, $y);
    
    return $self;
  }
  
  # Instance methods
  protected method init : Point ($x : int = 0, $y : int = 0) {
    $self->{x} = $x;
    $self->{y} = $y;
  }
  
  method clear : void () {
    $self->{x} = 0;
    $self->{y} = 0;
  }
  
  method clone : Point () {
    my $self_clone = Point->new($self->x, $self->y);
    
    return $self_clone;
  }
  
  method to_string : string () {
    my $x = $self->x;
    my $y = $self->y;
    
    my $string = "($x,$y)";
    
    return $string;
  }
  
  method eq : int ($a : Point, $b : Point) {
    
    my $eq = 0;
    if ($a && $b) {
      if ($a->{x} == $b->{x} && $a->{y} == $b->{y}) {
        $eq = 1;
      }
    }
    elsif ($a) {
      $eq = 0;
    }
    elsif ($b) {
      $eq = 0;
    }
    else {
      $eq = 1;
    }
    
    return $eq;
  }
  
}
