package Piece;
use strict;
1;

our $pieces = {
  'i0'   => { width => 1, height => 4, map => [['i'], ['i'], ['i'], ['i']] },
  'i90'  => { width => 4, height => 1, map => [['i','i','i','i']] },
  'i180' => { width => 1, height => 4, map => [['i'], ['i'], ['i'], ['i']] }, # dup
  'i270' => { width => 4, height => 1, map => [['i','i','i','i']] }, # dup
  'j0'   => { width => 2, height => 3, map => [['.','j'], ['.','j'], ['j','j']] },
  'j90'  => { width => 3, height => 2, map => [['j','.','.'], ['j','j','j']] },
  'j180' => { width => 2, height => 3, map => [['j','j'], ['j','.'], ['j','.']] },
  'j270' => { width => 3, height => 2, map => [['j','j','j'], ['.','.','j']] },
  'l0'   => { width => 2, height => 3, map => [['l','.'], ['l','.'], ['l','l']] },
  'l90'  => { width => 3, height => 2, map => [['l','l','l'], ['l','.','.']] },
  'l180' => { width => 2, height => 3, map => [['l','l'], ['.','l'], ['.','l']] },
  'l270' => { width => 3, height => 2, map => [['.','.','l'], ['l','l','l']] },
  'o0'   => { width => 2, height => 2, map => [['o','o'], ['o','o']] },
  'o90'  => { width => 2, height => 2, map => [['o','o'], ['o','o']] }, # dup
  'o180' => { width => 2, height => 2, map => [['o','o'], ['o','o']] }, # dup
  'o270' => { width => 2, height => 2, map => [['o','o'], ['o','o']] }, # dup
  's0'   => { width => 3, height => 2, map => [['.','s','s'], ['s','s','.']] },
  's90'  => { width => 2, height => 3, map => [['s','.'], ['s','s'], ['.','s']] },
  's180' => { width => 3, height => 2, map => [['.','s','s'], ['s','s','.']] }, # dup
  's270' => { width => 2, height => 3, map => [['s','.'], ['s','s'], ['.','s']] }, # dup
  't0'   => { width => 3, height => 2, map => [['t','t','t'], ['.','t','.']] },
  't90'  => { width => 2, height => 3, map => [['.','t'], ['t','t'], ['.','t']] },
  't180' => { width => 3, height => 2, map => [['.','t','.'], ['t','t','t']] },
  't270' => { width => 2, height => 3, map => [['t','.'], ['t','t'], ['t','.']] },
  'z0'   => { width => 3, height => 2, map => [['z','z','.'], ['.','z','z']] },
  'z90'  => { width => 2, height => 3, map => [['.','z'], ['z','z'], ['z','.']] },
  'z180' => { width => 3, height => 2, map => [['z','z','.'], ['.','z','z']] }, # dup
  'z270' => { width => 2, height => 3, map => [['.','z'], ['z','z'], ['z','.']] }, # dup
};

our $usefulAngles = {
    'i' => [0, 90],
    'j' => [0, 90, 180, 270],
    'l' => [0, 90, 180, 270],
    'o' => [0],
    's' => [0, 90],
    't' => [0, 90, 180, 270],
    'z' => [0, 90],
};

sub new {
    my($class, $piece) = @_;
    die unless $piece =~ m/^[ijlostz]$/os;
    my $self = {
        kind => $piece,
    };
    bless $self, $class;
    return $self;
}

sub width {
    return $pieces->{"$_[0]->{kind}$_[1]"}->{width};
}

sub height {
    return $pieces->{"$_[0]->{kind}$_[1]"}->{height};
}

sub cell {
    return $pieces->{"$_[0]->{kind}$_[1]"}->{map}->[$_[3]]->[$_[2]];
}

sub usefulAngles {
    return @{$usefulAngles->{$_[0]->{kind}}};
}