#!/usr/bin/perl -w
use warnings;
use strict;

use Image::Magick;
use File::HomeDir;
use MIME::Base64;

package montage
{
    my $image = Image::Magick->new;
    
    sub new
    {
        my $class = shift;
        my $self = {};
        bless($self, $class);
        return $self;
    }

    sub command
    {
        return "Montage";
    }

    sub do
    {
        my $home = File::HomeDir->my_home;
        
        if (! -d "$home/DIY_Streetview/thumbs")
        {
            system("mkdir $home/DIY_Streetview/thumbs");
        }
        
        my $imagepath = $_[1];
        my $filename = $imagepath;
        $imagepath =~ s/\.?\.//g; # Stop a Local File system Exploit, and error when the File path doesn't exist. Plus when did I ever use . in GUIDS!
        $imagepath =~ s/-/\//g;
        
        if ($imagepath eq "" | ! -d "$home/DIY_Streetview/images/$imagepath")
        {
            print $main::socket "{ \"success\":false }\n";
            print "ERR: Montage Set ID '$filename' doesn't exist\n";
        }
        else
        {
            if (! -f "$home/DIY_Streetview/thumbs/$filename.png")
            {
                undef @$image; #Send ImageMagick's Internal Image list to the moon. Since it doesn't behave and clean itself up.
                
                my ($DIR, @files);            
                opendir ($DIR, "$home/DIY_Streetview/images/$imagepath");
                @files = grep(m/.*\.png$/, readdir($DIR));
                closedir($DIR);
        
                if (scalar(@files) == 0)
                {
                    print $main::socket "{ \"success\":false }\n";
                    print "ERR: Montage Set ID '$filename' has no images.\n";
                }
                else
                {
            
                    my $count = 0;
                    while ($count < scalar(@files))
                    {
                        my $image_file;
                        my $cur_file = $files[$count];
                        my $file_ref;
                        open($file_ref, "$home/DIY_Streetview/images/$imagepath/$cur_file");
                        $image->Read(file=>\*$file_ref);
                        close($file_ref);
                        $count++;
                    }
                    
                    
                    if (defined @$image)
                    { 
                        my $montage = $image->Montage();
                        print "Montage file for '$filename' doesn't exist... Generating Montage...\n";
                        $montage->Write(filename=>"$home/DIY_Streetview/thumbs/$filename.png");

                    }
                    else
                    {
                        print "ImageMagick failed to load any images.";
                        print $main::socket "{ \"success\":false }\n";
                    }
                }
            }
            
            if (-f "$home/DIY_Streetview/thumbs/$filename.png")
            {
                print "Sending requested Montage to the client...\n"; 
                my $b64image;
                open ($b64image, "$home/DIY_Streetview/thumbs/$filename.png") or die "$!";
                my $raw_string = do{ local $/ = undef; <$b64image>; };
                my $encoded = MIME::Base64::encode_base64( $raw_string, ''); # Making sure the output is a little as possible. (Even though there is 33% overhead in Base64 encoded string.)
                print $main::socket "{ \"success\":true, \"image_data\":\"$encoded\" }\n";
                print "Sent requested Montage to the client.\n";
            }
        }
    }


    sub doupstart
    {
        return 0;
    }

    sub help
    {
        return "usage: Montage:<setID>.\nFinds and combines images to make a single image of thumbnails, and returns that image in a BASE64 representation.";
    }

    sub defaultsettings
    {
        return undef;
    }

1;
}
