package CPANPLUS::Dist::Gentoo::Atom;

use strict;
use warnings;

our $VERSION = '0.08';

use Carp         ();
use Scalar::Util ();

use overload (
 '<=>' => \&cmp,
 '""'  => \&as_string,
);

use CPANPLUS::Dist::Gentoo::Version;

my $version_rx = $CPANPLUS::Dist::Gentoo::Version::version_rx;

sub new {
 my $class = shift;
 $class = ref($class) || $class;

 my %args = @_;

 my ($category, $name, $version);
 if (defined $args{name}) {
  ($category, $name, $version) = @args{qw/category name version/};
  Carp::confess('Category unspecified') unless defined $category;
  /[^\w-]/ and Carp::confess('Invalid argument') for $name, $category;
 } elsif (defined $args{atom}) {
  my $atom = $args{atom};
  $atom =~ m{^([\w-]+)/([\w-]+)-v?($version_rx)$}
                                               or Carp::confess('Invalid atom');
  ($category, $name, $version) = ($1, $2, $3);
 } elsif (defined $args{ebuild}) {
  my $ebuild = $args{ebuild};
  $ebuild =~ m{/([\w-]+)/([\w-]+)/\2-v?($version_rx)\.ebuild$}
                                             or Carp::confess('Invalid ebuild');
  ($category, $name, $version) = ($1, $2, $3);
 } else {
  Carp::confess('Not enough information for building an atom object');
 }

 if (defined $version) {
  unless (Scalar::Util::blessed($version)
                              and $_->isa('CPANPLUS::Dist::Gentoo::Version')) {
   $version = CPANPLUS::Dist::Gentoo::Version->new($version);
  }
 }

 my $range = $args{range};
 if (defined $version) {
  if (defined $range) {
   Carp::confess("Invalid range $range")
                                      unless grep $range eq $_, qw|< <= = >= >|;
  } else {
   $range = '=';
  }
 } else {
  Carp::confess('Range atoms require a valid version')
                                            if defined $range and length $range;
 }

 bless {
  category => $category,
  name     => $name,
  version  => $version,
  range    => $range,
  ebuild   => $args{ebuild},
 }, $class;
}

BEGIN {
 eval "sub $_ { \$_[0]->{$_} }" for qw/category name version range ebuild/;
}

sub cmp {
 my ($a1, $a2, $r) = @_;

 my $v1 = $a1->version;

 my $v2;
 if (Scalar::Util::blessed($a2) and $a2->isa(__PACKAGE__)) {
  Carp::confess('Can\'t compare atoms of different packages')
                    if $a1->category ne $a2->category or $a1->name ne $a2->name;
  $v2 = $a2->version;
 } else {
  $v2 = $a2;
 }

 ($v1, $v2) = ($v2, $v1) if $r;

 return $v1 <=> $v2;
}

sub as_string {
 my ($a) = @_;

 my $atom = $a->category . '/' . $a->name;

 my $version = $a->version;
 $atom = $a->range . $atom . '-' . $version if defined $version;

 return $atom;
}

my %order = (
 '<'  => -2,
 '<=' => -1,
  '=' =>  0,
 '>=' =>  1,
 '>'  =>  2,
);

sub and {
 shift unless length ref $_[0];

 my $a1 = shift;
 my $a2 = shift;
 $a2 = $a2->and(@_) if @_;

 my $p1 = join '/', $a1->category, $a1->name;
 my $p2 = join '/', $a2->category, $a2->name;
 Carp::confess("Atoms for different packages $p1 and $p2") unless $p1 eq $p2;

 my $v1 = $a1->version;
 return $a2 unless defined $v1;
 my $r1 = $a1->range; # Defined if $v1 is defined

 my $v2 = $a2->version;
 return $a1 unless defined $v2;
 my $r2 = $a2->range; # defined if $v2 is defined

 my $o1 = $order{$r1};
 my $o2 = $order{$r2};

 Carp::confess("Incompatible ranges $r1$p1 and $r2$p2") if $o1 * $o2 < 0;

 if ($r2 eq '=') {
  ($a1, $a2) = ($a2, $a1);
  ($v1, $v2) = ($v2, $v1);
  ($r1, $r2) = ($r2, $r1);
  ($o1, $o2) = ($o2, $o1);
 }

 if ($r1 eq '=') {
  my $r = $r2 eq '=' ? '==' : $r2;
  Carp::confess("Version mismatch $v1 $r $v2") unless eval "\$a1 $r \$a2";
  return $a1;
 } elsif ($o1 > 0) {
  return $a1 < $a2 ? $a2 : $a1;
 } else {
  return $a1 < $a2 ? $a1 : $a2;
 }
}

sub fold {
 shift unless length ref $_[0];

 my %seen;
 for my $atom (@_) {
  my $key = join '/', $atom->category, $atom->name;

  my $cur = $seen{$key};
  $seen{$key} = defined $cur ? $cur->and($atom) : $atom;
 }

 return values %seen;
}

1;
