use GD;
use XML::Simple;
use Data::Dumper;
use Image::Resize;
use List::Util 'shuffle';

my $imgsrc = "people2";
my $ref = XMLin('/usr/local/freeswitch/conf/autoload_configs/conference_layouts.conf.xml', ForceArray => [ 'image' ]);
my $layouts = $ref->{'layout-settings'}->{'layouts'}->{'layout'};
my $imgdir = "/mnt/freeswitch/common/images/layouts/$imgsrc";
my @layout_names = sort keys %{$layouts};
my $destdir = "/mnt/freeswitch/common/layout-renderings/$imgsrc";

system("mkdir -p $destdir");

opendir DIR, $imgdir or die "can't open $imgdir";
my @IMG = map {$imgdir . "/" . $_} grep { -f "$imgdir/$_" } readdir(DIR);
closedir DIR;

sub render($) {
  my $name = shift;
  my $WIDTH = 1920;
  my $HEIGHT = 1080;
  my $SCALE = 360;
  my $layout = $layouts->{"$name"};
  
  my $im = new GD::Image($WIDTH, $HEIGHT);
  my $white = $im->colorAllocate(204,204,204);
  my $black = $im->colorAllocate(0,0,0);
  my $red = $im->colorAllocate(255,0,0);
  my $blue = $im->colorAllocate(20, 46, 85);
  my $gray = $im->colorAllocate(51,51,51);
  my $x = 0;

  #$im->fill(0, 0, $gray);
  #print $name . "\n" . Dumper $layout;
  my @PIX = shuffle(@IMG);
  

  foreach my $screen (@{$layout->{image}}) {
    $screen->{width} = $WIDTH * $screen->{scale} / $SCALE;
    $screen->{height} = $HEIGHT * $screen->{scale} / $SCALE;
    $screen->{aspect} = $screen->{width} / $screen->{height};
    $screen->{x_pos} = $WIDTH * $screen->{x} / $SCALE;
    $screen->{y_pos} = $HEIGHT * $screen->{y} / $SCALE;

    my $avatar = newFromPng GD::Image($PIX[$x++]);
    my @message = ();

    if ($x > $#PIX) {
      $x = 0;
    }


    if ($layout->{'auto-3d-position'} && !$screen->{"audio-position"}) {
      my $center_x = $screen->{x_pos} + $screen->{width} / 2;
      my $center_y = $screen->{y_pos} + $screen->{height} / 2;

      my $half_x = $WIDTH / 2;
      my $half_y = $HEIGHT / 2;

      my $xv = 0;
      my $yv = 0;

      if ($center_x > $half_x) {
	$xv = sprintf "%0.2f", ($center_x - $half_x) / $half_x;
      } else {
	$xv = sprintf "%0.2f", -1 - ($center_x / $half_x) * -1;
      }

      if ($center_y > $half_y) {
	$yv = sprintf "%0.2f", -1 - (($center_y - $half_y) / $half_y) * -1;
      } else {
	$yv = sprintf "%0.2f", $center_y / $half_y;
      }

      $screen->{"audio-position"} = "$xv:0.0:$yv";
    }

    $im->copyResized($avatar,
		     $screen->{x_pos}, $screen->{y_pos},
		     0,0,
		     $screen->{width}, $screen->{height},
		     $avatar->width, $avatar->height );

    #$im->arc($center_x, $center_y, 100, 100, 0,360, $blue);


    if ($screen->{floor}) {
      push @message, "Floor Holder";
    }

    if ($screen->{"floor-only"}) {
      push @message, "Floor Only";
    }

    if ($screen->{"audio-position"}) {
      push @message, "Audio Position: " . $screen->{"audio-position"};
    }

    if ($screen->{"reservation_id"}) {
      push @message, "Res ID: " . $screen->{"reservation_id"};
    }

    if ($screen->{"overlap"}) {
      push @message, "Overlap";
    }

    if ($screen->{"file-only"}) {
      push @message, "File-Only";
    }

    if (@message) {
      $message = join(" | ", @message);
    }

    if ($message) {
      my $pct = .08;
      my $bgheight = int($screen->{height} * $pct);
      my $fontsize = $bgheight / 2;
      my $fontoff = $fontsize / 2;

      $im->filledRectangle($screen->{x_pos} ,$screen->{y_pos} + $screen->{height} - $bgheight, $screen->{x_pos} + $screen->{width}, $screen->{y_pos} + $screen->{height}, $blue);

      $im->stringFT($white, '/usr/share/fonts/truetype/Microsoft/Verdana.ttf', $fontsize, 0,
		    $screen->{x_pos} + $fontoff, $screen->{y_pos} + $screen->{height} - $fontoff, $message);
    }

    #print Dumper $screen;
  }


  open O, ">$destdir/$name.png" || die "can't open file";
  binmode O;
  print O $im->png;
  close O;
  printf "Saved $destdir/$name.png\n";

}

#render("1up_top_left+5");
#render("2x2");

#__END__
open HTML, ">$destdir/index.html";

foreach(@layout_names) {
  print HTML "<h2>$_</h2>";
  print HTML "<img src=\"$_.png\">\n<br><br>";
  render("$_");
}

close HTML;
