#  Copyright (c) 1997-2021
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://polymake.org
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------

# == SUMMARY ==
# - In Polytope:
#   * replace AMBIENT_DIM by CONE_AMBIENT_DIM
#   * replace DIM by CONE_DIM or COMBINATORIAL_DIM, depending on presence of geometric information
# - In PointConfiguration:
#   * rename POLYTOPE_AMBIENT_DIM and POLYTOPE_DIM to AMBIENT_DIM and DIM resp.
# - In PolyhedralFan:
#   * adjust values of COMBINATORIAL_DIM and MAXIMAL_CONES_DIMS, rename the latter to MAXIMAL_CONES_COMBINATORIAL_DIMS
#   * rename AMBIENT_DIM and DIM to FAN_AMBIENT_DIM and FAN_DIM resp.
# - Move FACET_TRIANGULATIONS to BOUNDARY.FACET_TRIANGULATIONS in TRIANGULATION
# - In Polytope and PointConfiguration with WEIGHTS, either create a POLYTOPAL_SUBDIVISION or move weights into TRIANGULATION

upgrade 2.9.10 polytope::Polytope.AMBIENT_DIM | DIM {
   my ($obj, $prop) = @_;
   if (defined(my $dim = delete $obj->{AMBIENT_DIM})) {
      $obj->{CONE_AMBIENT_DIM} = $dim+1;
   }
   if (defined(my $dim = delete $obj->{DIM})) {
      my $new_prop_name = "COMBINATORIAL_DIM";
      for (qw(POINTS VERTICES INEQUALITIES FACETS EQUATIONS AFFINE_HULL LINEALITY_SPACE)) {
         if (defined($obj->{$_})) {
            $new_prop_name = "CONE_DIM";
            ++$dim;
            last;
         }
      }
      $obj->{$new_prop_name} = $dim;
   }
   true
}


upgrade 2.9.11 polytope::PointConfiguration.POLYTOPE_AMBIENT_DIM = rename AMBIENT_DIM;

upgrade 2.9.11 polytope::PointConfiguration.POLYTOPE_DIM = rename DIM;


upgrade 2.9.12 fan::PolyhedralFan.COMBINATORIAL_DIM {
   my ($obj, $prop) = @_;
   if (defined($obj->{$prop})) {
      $obj->{$prop} -= 1;
      true
   }
}

upgrade 2.9.12 fan::PolyhedralFan.MAXIMAL_CONES_DIMS {
   my ($obj, $prop) = @_;
   my $dims = delete $obj->{$prop};
   if (defined($dims)) {
      my $deduct = 1 + ($obj->{LINEALITY_DIM} // 0);
      $_ -= $deduct for @$dims;
   }
   $obj->{MAXIMAL_CONES_COMBINATORIAL_DIMS} = $dims;
   true
}


upgrade 2.9.13 polytope::Cone.TRIANGULATION.FACET_TRIANGULATIONS {
   my ($obj, $prop) = @_;
   $obj->{BOUNDARY}{$prop} = delete $obj->{$prop};
   true
}

upgrade 2.9.13 polytope::PointConfiguration.TRIANGULATION.FACET_TRIANGULATIONS {
   my ($obj, $prop) = @_;
   $obj->{BOUNDARY}{$prop} = delete $obj->{$prop};
   true
}


upgrade 2.9.14 fan::PolyhedralFan.DIM = rename FAN_DIM;

upgrade 2.9.14 fan::PolyhedralFan.AMBIENT_DIM = rename FAN_AMBIENT_DIM;

upgrade 2.9.14 topaz::SimplicialComplex.BALANCED = rename FOLDABLE;


upgrade 2.9.15 polytope::Polytope.POLYTOPAL_SUBDIVISION {
   my ($obj, $prop) = @_;
   create_subdivision($obj, delete $obj->{$prop});
}

upgrade 2.9.15 polytope::PointConfiguration.POLYTOPAL_SUBDIVISION {
   my ($obj, $prop) = @_;
   create_subdivision($obj, delete $obj->{$prop});
}

# upgrade rules are executed in the definition order,
# so that the next two rules will only be fired when there was no POLYTOPAL_SUBDIVISION

upgrade 2.9.15 polytope::Polytope.WEIGHTS = \&move_weights;

upgrade 2.9.15 polytope::PointConfiguration.WEIGHTS = \&move_weights;

sub create_subdivision {
   my ($obj, $cells) = @_;
   my %subdiv;
   if (defined($cells)) {
      $subdiv{MAXIMAL_CELLS} = $cells;
   }
   if (defined(my $splits = delete $obj->{SPLITS_IN_SUBDIVISION})) {
      $subdiv{REFINED_SPLITS} = $splits;
   }
   if (defined(my $weights = delete $obj->{WEIGHTS})) {
      $subdiv{WEIGHTS} = $weights;
   }
   delete $obj->{WEIGHTS_GENERIC};
   $obj->{POLYTOPAL_SUBDIVISION} = \%subdiv;
   push @{$obj->{_load}}, "fan";
   true
}

sub move_weights {
   my ($obj, $prop) = @_;
   if (delete $obj->{WEIGHTS_GENERIC}) {
      $obj->{TRIANGULATION}{$prop} = delete $obj->{$prop};
   } else {
      create_subdivision($obj);
   }
}


# Local Variables:
# mode: perl
# cperl-indent-level: 3
# indent-tabs-mode:nil
# End:
