// BWThumbnailHTMLAccumululator -- Builder object that accumulates HTML
//				   based on a set of HTML snippets.
//				   Once the HTML is accumulated, it can be
//				   written to a file

// Copyright (C) 2005 Borkware, LLC
// We use the BSD License.  Check out http://borkware.com/license

#import "BWThumbnailHTMLAccumulator.h"
#import "BWSnippetKeeper.h"


@implementation BWThumbnailHTMLAccumulator

// create a new accumulator based on a set of snippets

- (id) initWithSnippets: (BWSnippetKeeper *) s
{
    if (self = [super init]) {
        snippets = [s retain];
        html = [[NSMutableString alloc] init];

        NSString *x = [snippets valueForKey: @"snippetEveryX"];
        everyX = [x intValue];
    }
    
    return (self);

} // setSnippets



// convenience builder for making a new accumulator

+ (id) accumulatorWithSnippets: (BWSnippetKeeper *) s
{
    BWThumbnailHTMLAccumulator *accumulator;

    accumulator = [[BWThumbnailHTMLAccumulator alloc] initWithSnippets: s];

    return ([accumulator autorelease]);

} // accumulatorWithSnippets



// clean up our mess

- (void) dealloc
{
    [snippets release];
    [html release];

    [super dealloc];

} // dealloc



// add an arbitrary hunk of text (html please) to the accumulated string
- (void) addHtml: (NSString *) h
{
    [html appendString: h];
    [html appendString: @"\n"];

} // addHtml



// add the header

- (void) addHeaderWithTitle:(NSString *)title
{
    NSString *string;
    string = [snippets valueForKey: @"snippetDocumentHeader"];
    
    [self addHtml: string];
	[self addHtml:[NSString stringWithFormat:@"<title>%@</title>", title]];
	string = [snippets valueForKey:@"snippetMetaTags"];
	[self addHtml:string];
	[self addHtml:[snippets valueForKey:@"snippetStylesheet"]];
	[self addHtml:@"</head>\n<body>"];
	// Add style
	

} // addHeader



// add the html that should appear before any thumbnails

- (void) addBeforeThumbnails
{
    NSString *string;
    string = [snippets valueForKey: @"snippetBeforeThumbnails"];
    
    [self addHtml: string];

} // addBeforeThumbnails



// add this after every X images.  Handy for making new table rows

- (void) addForEveryXImages
{
    NSString *string;
    string = [snippets valueForKey: @"snippetForEveryXImages"];
    
    [self addHtml: string];

} // addForEveryXImages



// add an image to the html.  The thumbName becomes the target of an
// <a> link.  Can be nil.  thumbName is the <img> tag

- (void) addEnlargementNamed: (NSString *) enlargementName
        thumbnailNamed: (NSString *) thumbName
                  size: (NSSize) dimensions
{
    // see if we need to do the 'do this after every X thumbs'
    if (everyX > 0 && imagesSeen > 0) {
        if ((imagesSeen % everyX) == 0) {
            [self addForEveryXImages];
        }
    }

    // contains the %@ where to put the <a> / <img> contents
    NSString *template;
    template = [snippets valueForKey: @"snippetForEachImage"];

    NSString *filename;
    filename = [enlargementName lastPathComponent];
        
    NSString *imgString;

    if (enlargementName != nil) {
        // construct the <a href=> tag
        // !!! should put this in the snippets editor window
        imgString = [NSString 
                        stringWithFormat: @"<a href='%@'><img src='%@' width='%d' height='%d' alt='Artwork Image'></a>",
                        enlargementName, thumbName, 
                        (int)dimensions.width, (int)dimensions.height];
    } else {
        imgString = [NSString 
                        stringWithFormat: @"<img src='%@' width='%d' height='%d' alt='Artwork Image'/>",
                        thumbName, 
                        (int)dimensions.width, (int)dimensions.height];
    }

    // what to add to the html
    NSString *finalString;
    finalString = [NSString stringWithFormat: template, imgString];

    [self addHtml: finalString];

    imagesSeen++;

} // addImageNamed



// stuff to add after the thumbnails, to close tables or whatnot

- (void) addAfterThumbnails
{
    NSString *string;
    string = [snippets valueForKey: @"snippetAfterThumbnails"];
    
    [self addHtml: string];

} // addAfterThumbnails



// finish off the html

- (void) addFooter
{
    NSString *string;
    string = [snippets valueForKey: @"snippetDocumentFooter"];
    
    [self addHtml: string];

} // addFooter



- (void) addGalleryFooter
{
	[self addHtml:[snippets valueForKey:@"snippetGalleryFooter"]];
}


- (void) addSnippetWithKey:(NSString *)snippet
{
	[self addHtml:[snippets valueForKey:snippet]];
}

// add any kind of html

- (void) addArbitraryHTML: (NSString *) newstuff
{
    [self addHtml: newstuff];

} // addArbitraryHTML



// get the html accumnulated so far

- (NSString *) html
{
    return ([[html retain] autorelease]);

} // html



// write the html to the given file

- (void) writeToFile: (NSString *) path
{
    [html writeToFile: path  atomically: YES encoding:NSUTF8StringEncoding error:NULL];

} // writeToFile


@end // BWThumbnailHTMLAccumulator
