package Mojolicious::Plugin::P;
use Mojo::Base 'Mojolicious::Plugin';

use strict;
use warnings;

sub register {

	my ($self, $app, $conf) = @_;

    $app->helper(mypluginhelper => sub { 
    	return 'I am your helper and I live in a plugin!'; 
    });

    $app->helper(getimages => sub {
		my $self = shift;
  		my ($getthumbs, $seriesnames, $imagesfolder) = @_;

  		if(!defined($imagesfolder) || $imagesfolder eq ''){
  			$imagesfolder = 'img'; # image folders defaults to /img
  		}

	  	# get a list of all files in the /img folder and its subfolders
	  	my $filesref =  $self->app->home->list_files($imagesfolder);

		my @images = ();
  		if(!defined($getthumbs) || !$getthumbs){
			# extracts all NON-thumbnail .jpg files located in $self->app->home->to_string/$imagesfolder (and not in /$imagesfolder subfolders)
  			@images = sort map /^(?!thumb)(.*?\d+-\d+x\d+\.jpg)$/i, @{$filesref};
  		}
  		else{
			# extracts all thumbnail .jpg files located in $self->app->home->to_string/$imagesfolder (and not in /$imagesfolder subfolders)
  			@images = sort map /^thumb(.*?\d+-\d+x\d+\.jpg)$/i, @{$filesref};
  		}

	  	my $seriesref = {}; # resulting data structure
		my ($dname,$dindex) = ('',-1); 
		my @dwidths = (); 
		my @dheights = ();
		my $n = scalar @images; # number of elements
		my ($name,$index,$width,$height,$datacomplete,$skip,$found) = (undef,undef,undef,undef,0,0,0);
		foreach my $image (@images){
			$n--;
			$image =~ m/^(.*?)(\d+)-(\d+)x(\d+)\.jpg$/i;
			($name,$index,$width,$height) = ($1,$2,$3,$4);
			if(defined($seriesnames)){
				$skip = 0;
				$found = 0;
				my $seriesname;
				foreach $seriesname (@{$seriesnames}){
					if($seriesname !~ m/^!(.*)/){
						$found = 1; # found a non-exclude parameter: this means all includes must be defined in the array
					}					
				}
				if(!$found){
					push @{$seriesnames}, '*'; # only excludes are defined: this means by default we will includes all others
				}
				else{
					$found = 0;
				}
				foreach my $seriesname (@{$seriesnames}){
					if($seriesname =~ m/^!(.*)/){ # excludes start with '!'
						if(lc($name) eq lc($1)){ # case-insensitive
							$skip = 1;
							last;
						}
					}
					else{
						if(lc($name) eq lc($seriesname) || $seriesname eq '*'){ # includes
							$found = 1;
							last; # $skip remains 0
						}
					}
				}
				if(!$found){
					$skip = 1;
				}
				next if($skip && $n); # skips that image
			}
			if($dname eq ''){
				$dname = $name; 
			}elsif($name ne $dname){ # start of another series
				$datacomplete = 1;
			}
			if($dindex == -1){
				$dindex = $index;
			}elsif($index != $dindex){ # start of another index
				$datacomplete = 1;
			}
			if(!$datacomplete){
				push @dwidths, $width;
				push @dheights, $height;
			}
			if($datacomplete || !$n){ # we gathered all the data for the current index
				push @{$seriesref->{$dname}->[$dindex-1]->{w}}, @dwidths;
				push @{$seriesref->{$dname}->[$dindex-1]->{h}}, @dheights;
				$dname = $name;
				$dindex = $index;
				@dwidths = ();
				push @dwidths, $width;
				@dheights = (); 
				push @dheights, $height;
				if(!$n && !$skip){
					if($datacomplete){
						push @{$seriesref->{$dname}->[$dindex-1]->{w}}, @dwidths;
						push @{$seriesref->{$dname}->[$dindex-1]->{h}}, @dheights;
					}
				}
				else{
					$datacomplete = 0;
				}
			}
		}

		foreach $dname (keys %{$seriesref}){
			next unless -e $self->app->home->rel_file("$imagesfolder/$dname.txt");
			my @lines = split '\n', $self->app->home->slurp_rel_file("$imagesfolder/$dname.txt");
			foreach my $line (@lines){
				my ($i,$c) = split ':', $line;
				$seriesref->{$dname}->[$i-1]->{caption} = $c;
			}
		}
		
    	return $seriesref; 
    });
}

1;
