package Greenstone::Site;

use strict;
use warnings;
use utf8;
use base 'Exporter';

use Greenstone::Config;
use XML::Tidy;

our $VERSION = 1.00;
our @EXPORT = ( 'new', 'get_sites' );
my $configTemplate;

sub new {
    my ($class, $name, %args) = @_;
    ref $class and die "Constructor used as object method\n";
    defined $name or die "A site must have a name\n";
    my $self = bless (
        {
            name => $name,
        },
        $class
    );
    for my $param (keys %args) {
        $self->{$param} = $args{$param};
    }
    return $self;
}

sub get {
    ref shift and die "Static method called on object\n";
    my $sites = {};
    my $dir = "$Greenstone::Config::GSDL3HOME/sites";
    opendir DH, $dir or die "Could not open sites directory '$dir'\n";
    while (my $site = readdir DH) {
        # We don't really need to store anything more than the fact that the site exists
        # So we just store nothing, which is enough to put the key in the hash
        # (i.e. it acts as a hashset)
        $sites->{$site} = ();
    }
    closedir DH;
    return $sites;
}

sub exists {
    my $self = shift;
    my $sites = get;
    return exists $sites->{$self->{name}};
}

sub create {
    my $self = shift;
    $self->exists and return 0;
    my $dir = "$Greenstone::Config::GSDL3HOME/sites/$self->{name}";
    mkdir $dir or die "Failed to create site directory: $!\n";
    mkdir "$dir/collect" or die "Failed to create collections directory: $!\n";
    open FH, '>', "$dir/siteConfig.xml" or die "Failed to create site config: $!\n";
    write_xml $configTemplate, *FH;
    close FH;
    return 1;
}

$configTemplate = {
    'siteConfig' => {
        'siteList' => {},
        'serviceRackList' => {
            'serviceRack' => [
                {
                    '.attr' => {
                        'name' => 'Authentication'
                    },
                    'recaptcha' => [
                        {
                            '.attr' => {
                                'name' => 'public_key',
                                'value' => '6LckI88SAAAAACUYjj97WMcnz5HPjVp3lI-x-ue8'
                            }
                        },
                        {
                            '.attr' => {
                                'value' => '6LckI88SAAAAAGnGy1PwuXYZzIMXZYoPxN51bWWG',
                                'name' => 'private_key'
                            }
                        }
                    ]
                },
                {
                    '.attr' => {
                        'name' => 'ArchiveIO'
                    }
                },
                {
                    '.attr' => {
                        'name' => 'DocumentMaker'
                    }
                },
                {
                    '.attr' => {
                        'name' => 'DocXMLUtil'
                    }
                },
                {
                    '.attr' => {
                        'name' => 'DocumentBasket'
                    }
                },
                {
                    '.attr' => {
                        'name' => 'BerryBasket'
                    }
                },
                {
                    '.attr' => {
                        'name' => 'GS2Construct'
                    }
                },
                {
                    '.attr' => {
                        'name' => 'DebugService'
                    }
                },
                {
                    '.attr' => {
                        'name' => 'UserTracker'
                    }
                },
                {
                    'format' => {
                        'gsf:template' => {
                            '.attr' => {
                                'match' => 'documentNode'
                            },
                            'td' => [
                                {
                                    'a' => {
                                        'xsl:attribute' => {
                                            'xsl:value-of' => [
                                                {
                                                    '.attr' => {
                                                        'select' => '$library_name'
                                                    }
                                                },
                                                {
                                                    '.attr' => {
                                                        'select' => '@collection'
                                                    }
                                                },
                                                {
                                                    '.attr' => {
                                                        'select' => '@nodeID'
                                                    }
                                                }
                                            ],
                                            '.value' => '/collection//document/',
                                            '.attr' => {
                                                'name' => 'href'
                                            }
                                        },
                                        'gsf:icon' => {}
                                    }
                                },
                                {
                                    'gsf:metadata' => {
                                        '.attr' => {
                                            'name' => 'Title'
                                        }
                                    }
                                },
                                {
                                    'a' => {
                                        'xsl:value-of' => {
                                            '.attr' => {
                                                'select' => '/page/pageResponse/service/paramList/param[@name=\'collection\']/option[@name=$collname]/displayItem[@name=\'name\']'
                                            }
                                        },
                                        '.attr' => {
                                            'href' => '{$library_name}/collection/{$collname}/page/about'
                                        }
                                    }
                                }
                            ],
                            'xsl:variable' => {
                                '.attr' => {
                                    'name' => 'collname',
                                    'select' => '@collection'
                                }
                            }
                        }
                    },
                    '.attr' => {
                        'name' => 'CrossCollectionSearch'
                    }
                }
            ]
        },
        '.attr' => {
            'xmlns:gsf' => 'http://www.greenstone.org/greenstone3/schema/ConfigFormat',
            'xmlns:xsl' => 'http://www.w3.org/1999/XSL/Transform'
        },
        'serviceClusterList' => {},
        'metadataList' => {
            'metadata' => [
                {
                    '.attr' => {
                        'lang' => 'en',
                        'name' => 'siteName'
                    },
                    '.value' => 'My Greenstone Library'
                },
                {
                    '.attr' => {
                        'lang' => 'fr',
                        'name' => 'siteName'
                    },
                    '.value' => "Mon site de d\x{e9}velopment"
                },
                {
                    '.attr' => {
                        'name' => 'siteDescription',
                        'lang' => 'en'
                    },
                    '.value' => 'This is just a random site for testing stuff...'
                },
                {
                    '.attr' => {
                        'lang' => 'fr',
                        'name' => 'siteDescription'
                    },
                    '.value' => 'C\'est juste un site pour tester des trucs...'
                },
                {
                    '.value' => 'admin@example.com',
                    '.attr' => {
                        'name' => 'siteAdmin'
                    }
                }
            ],
            '.attr' => {
                'xmlns:xsl' => 'http://www.w3.org/1999/XSL/Transform',
                'xmlns:gsf' => 'http://www.greenstone.org/greenstone3/schema/MetadataFormat'
            }
        },
        'replaceList' => [
            {
                'replace' => [
                    {
                        '.attr' => {
                            'scope' => 'metadata',
                            'text' => '<img src=\'interfaces/default/images/ipdf.gif\' border=\'0\'/>',
                            'macro' => '_iconpdf_',
                            'resolve' => 'false'
                        }
                    },
                    {
                        '.attr' => {
                            'macro' => '_icondoc_',
                            'resolve' => 'false',
                            'text' => '<img src=\'interfaces/default/images/imsword.gif\' border=\'0\'/>',
                            'scope' => 'metadata'
                        }
                    },
                    {
                        '.attr' => {
                            'scope' => 'metadata',
                            'resolve' => 'false',
                            'macro' => '_iconps_',
                            'text' => '<img src=\'interfaces/default/images/ips.gif\' border=\'0\'/>'
                        }
                    },
                    {
                        '.attr' => {
                            'text' => '<img src=\'interfaces/default/images/irtf.gif\' border=\'0\'/>',
                            'macro' => '_iconrtf_',
                            'resolve' => 'false',
                            'scope' => 'metadata'
                        }
                    },
                    {
                        '.attr' => {
                            'scope' => 'metadata',
                            'text' => '<img src=\'interfaces/default/images/iexcel.gif\' border=\'0\'/>',
                            'resolve' => 'false',
                            'macro' => '_iconxls_'
                        }
                    },
                    {
                        '.attr' => {
                            'macro' => '_iconppt_',
                            'resolve' => 'false',
                            'text' => '<img src=\'interfaces/default/images/ippt.gif\' border=\'0\'/>',
                            'scope' => 'metadata'
                        }
                    },
                    {
                        '.attr' => {
                            'text' => '<img src=\'interfaces/default/images/itext.gif\' border=\'0\'/>',
                            'resolve' => 'false',
                            'macro' => '_icontext_',
                            'scope' => 'metadata'
                        }
                    },
                    {
                        '.attr' => {
                            'scope' => 'metadata',
                            'text' => '<img src=\'interfaces/default/images/ibtext.gif\' border=\'0\'/>',
                            'macro' => '_iconblanktext_',
                            'resolve' => 'false'
                        }
                    },
                    {
                        '.attr' => {
                            'scope' => 'metadata',
                            'text' => '<img src=\'interfaces/default/images/imp3.gif\' border=\'0\'/>',
                            'macro' => '_iconmp3_',
                            'resolve' => 'false'
                        }
                    },
                    {
                        '.attr' => {
                            'scope' => 'metadata',
                            'text' => '<img src=\'interfaces/default/images/imidi.gif\' border=\'0\'/>',
                            'macro' => '_iconmidi_',
                            'resolve' => 'false'
                        }
                    },
                    {
                        '.attr' => {
                            'text' => '<img src=\'interfaces/default/images/iworld.gif\' border=\'0\'/>',
                            'macro' => '_iconworld_',
                            'resolve' => 'false',
                            'scope' => 'metadata'
                        }
                    },
                    {
                        '.attr' => {
                            'text' => '<img src=\'interfaces/default/images/iunknown.gif\' border=\'0\'/>',
                            'resolve' => 'false',
                            'macro' => '_iconunknown_',
                            'scope' => 'metadata'
                        }
                    },
                    {
                        '.attr' => {
                            'resolve' => 'false',
                            'macro' => '[archivedir]',
                            'scope' => 'all',
                            'metadata' => 'assocfilepath'
                        }
                    },
                    {
                        '.attr' => {
                            'metadata' => 'assocfilepath',
                            'scope' => 'all',
                            'macro' => '[assocfilepath]',
                            'resolve' => 'false'
                        }
                    },
                    {
                        '.attr' => {
                            'resolve' => 'false',
                            'macro' => '[srcurl]',
                            'metadata' => 'srcurl',
                            'scope' => 'metadata'
                        }
                    },
                    {
                        '.attr' => {
                            'resolve' => 'false',
                            'macro' => '[SourceFile]',
                            'metadata' => 'SourceFile',
                            'scope' => 'metadata'
                        }
                    },
                    {
                        '.attr' => {
                            'scope' => 'metadata',
                            'text' => '_httpsite_/collect/_clustername_',
                            'macro' => '_httpcollection_',
                            'resolve' => 'true'
                        }
                    },
                    {
                        '.attr' => {
                            'scope' => 'metadata',
                            'text' => '_httpsite_',
                            'macro' => '_httpprefix_',
                            'resolve' => 'true'
                        }
                    },
                    {
                        '.attr' => {
                            'text' => '_httpsite_/collect/_clustername_/index/assoc/[assocfilepath]/',
                            'resolve' => 'true',
                            'macro' => '_httpdocimg_',
                            'scope' => 'text'
                        }
                    },
                    {
                        '.attr' => {
                            'scope' => 'text',
                            'text' => '_libraryname_/collection/_clustername_/document/[DocOID]',
                            'resolve' => 'true',
                            'macro' => '_httpsamepagelink_'
                        }
                    },
                    {
                        '.attr' => {
                            'text' => '_clustername_',
                            'resolve' => 'true',
                            'macro' => '[collection]',
                            'scope' => 'all'
                        }
                    },
                    {
                        '.attr' => {
                            'scope' => 'all',
                            'macro' => '_httpextlink_',
                            'resolve' => 'true',
                            'text' => '_libraryname_?el=&a=d&c=_clustername_&d='
                        }
                    }
                ],
                '.attr' => {
                    'id' => 'gs2-standard'
                }
            },
            {
                'replace' => [
                    {
                        '.attr' => {
                            'macro' => '[Image]',
                            'resolve' => 'false',
                            'metadata' => 'Image',
                            'scope' => 'metadata'
                        }
                    },
                    {
                        '.attr' => {
                            'metadata' => 'Thumb',
                            'scope' => 'metadata',
                            'resolve' => 'false',
                            'macro' => '[Thumb]'
                        }
                    },
                    {
                        '.attr' => {
                            'scope' => 'metadata',
                            'metadata' => 'ThumbWidth',
                            'macro' => '[ThumbWidth]',
                            'resolve' => 'false'
                        }
                    },
                    {
                        '.attr' => {
                            'metadata' => 'ThumbHeight',
                            'scope' => 'metadata',
                            'resolve' => 'false',
                            'macro' => '[ThumbHeight]'
                        }
                    },
                    {
                        '.attr' => {
                            'macro' => '[Screen]',
                            'resolve' => 'false',
                            'metadata' => 'Screen',
                            'scope' => 'metadata'
                        }
                    },
                    {
                        '.attr' => {
                            'metadata' => 'ScreenWidth',
                            'scope' => 'metadata',
                            'macro' => '[ScreenWidth]',
                            'resolve' => 'false'
                        }
                    },
                    {
                        '.attr' => {
                            'resolve' => 'false',
                            'macro' => '[ScreenHeight]',
                            'metadata' => 'ScreenHeight',
                            'scope' => 'metadata'
                        }
                    }
                ],
                '.attr' => {
                    'id' => 'gs2-image'
                }
            }
        ]
    }
};

