//
//  PlaylistController.m
//  Hootenanny
//
//  Created by Noah Sorscher on 1/1/05.
//  Copyright 2005 __MyCompanyName__. All rights reserved.
//

#import "PlaylistController.h"
#import "tPlaylist.h"

#define CELLHEIGHT 35

@implementation PlaylistController


- (NSString *)windowTitleForDocumentDisplayName:(NSString *)disp
{
	return [[self document] name];
}

- (IBAction) okay:(id)sender
{
    NSMutableArray *options = [NSMutableArray array];
    NSArray *rows = [[[bigBox subviews] objectAtIndex:0] subviews];
    int i, n = [rows count];
    for (i=0;i<n;i++) {
        NSView *row = [[[rows objectAtIndex:i] subviews] objectAtIndex:0];
        NSString *cat = [[[row subviews] objectAtIndex:0] titleOfSelectedItem];
        NSString *con = [[[row subviews] objectAtIndex:1] titleOfSelectedItem];
        NSString *val = [[[row subviews] objectAtIndex:2] stringValue];
        if (val && [val length])
            [options addObject:[NSString stringWithFormat:@"%@|%@|%@", cat, con, val]];
		else if ([cat isEqualToString:@"All Edited Movies"])
			[options addObject:cat];
    }
    if ([live state])
        [options addObject:@"live"];
    [options addObject:[NSString stringWithFormat:@"Match %@", [anyAll titleOfSelectedItem]]];
    [[self document] setOptions:options];
    [plWindow close];
}

- (IBAction) add:(id)sender
{
    NSArray *sviews = [[[bigBox subviews] objectAtIndex:0] subviews];
    int n = [sviews count];
    float y = 35 * n;
    NSBox *box = [[[NSBox alloc] initWithFrame:NSMakeRect(5, y, 579, 50)] autorelease];
    //[box setAutoresizingMask:NSViewMinYMargin];
    [box setBorderType:NSNoBorder];
    [box setTitlePosition:NSNoTitle];
    NSPopUpButton *p = [[[NSPopUpButton alloc] initWithFrame:NSMakeRect(2,3,135,26)] autorelease];
    NSMutableArray *cats = [NSMutableArray array];
    [cats addObjectsFromArray:[MHMovie supportedMetadata]];
    [cats addObject:@"Chapters"];
    [cats addObject:@"Text Tracks"];
    [cats addObjectsFromArray:[[self document] supportedData]];
    [cats addObject:@"Sync With Folder"];
	[cats addObject:@"All Edited Movies"];
    [p addItemsWithTitles:cats];
    [box addSubview:p];
    p = [[[NSPopUpButton alloc] initWithFrame:NSMakeRect(139,3,160,26)] autorelease];
    [p addItemsWithTitles:[NSArray arrayWithObjects:@"Contains", @"Does Not Contain", @"Is", @"Is Not", @"Starts With", @"Ends With", @"Is Higher Than", @"Is Lower Than", nil]];
    [box addSubview:p];
    NSTextField *t = [[[NSTextField alloc] initWithFrame:NSMakeRect(304,7,172,22)] autorelease];
    [[t cell] setScrollable:YES];
    [box addSubview:t];
    NSButton *b = [[[NSButton alloc] initWithFrame:NSMakeRect(487,-3,39,38)] autorelease];
    [b setTitle:@"+"];
    [b setBezelStyle:NSCircularBezelStyle];
    [box addSubview:b];
    [b setTarget:self];
    [b setAction:@selector(add:)];
    b = [[[NSButton alloc] initWithFrame:NSMakeRect(524,-3,39,38)] autorelease];
    [b setTitle:@"-"];
    [b setBezelStyle:NSCircularBezelStyle];
    [box addSubview:b];
    [b setTarget:self];
    [b setAction:@selector(remove:)];

    NSRect frame = [plWindow frame];
    frame.origin.y -= CELLHEIGHT / 2;
    frame.size.height += CELLHEIGHT;
    NSRect sframe = [[plWindow screen] frame];
    float sheight = sframe.size.height + sframe.origin.y;
    NSArray *screens = [NSScreen screens];
    if ([screens count] && [plWindow screen] == [screens objectAtIndex:0]) sheight -= 22;
    if (frame.origin.y + frame.size.height > sheight) frame.origin.y = sheight - frame.size.height;
    [plWindow setFrame:frame display:YES animate:YES];
    
    [bigBox addSubview:box];
}

- (IBAction) remove:(id)sender
{
    NSArray *rows = [[[bigBox subviews] objectAtIndex:0] subviews];
    int i, n = [rows count];
    BOOL found = NO;
    NSRect frame;
    for (i=0;i<n;i++) {
        if ([[[[[rows objectAtIndex:i] subviews] objectAtIndex:0] subviews] containsObject:sender]) {
            [[rows objectAtIndex:i] removeFromSuperview];
            found = YES;
            n--;
            i--;
        } else if (found) {
            frame = [[rows objectAtIndex:i] frame];
            frame.origin.y -= CELLHEIGHT;
            [[rows objectAtIndex:i] setFrame:frame];
        }
    }
    frame = [plWindow frame];
    frame.size.height -= CELLHEIGHT;
    [plWindow setFrame:frame display:YES animate:YES];
}

- (IBAction) syncWidgets
{
    NSArray *options = [(tPlaylist *)[self document] options];
    if (options && [options count]) {
        int i, n = [options count];
        for (i=0;i<n;i++) {
            NSArray *anOption = [[options objectAtIndex:i] componentsSeparatedByString:@"|"];
            NSString *key = [anOption objectAtIndex:0];
            if (![key isEqualToString:@"live"] && ![key hasPrefix:@"Match"]) {
                [self add:nil];
                NSString *s = key;
                NSView *row = [[[[[[bigBox subviews] objectAtIndex:0] subviews] lastObject] subviews] objectAtIndex:0];
                [[[row subviews] objectAtIndex:0] selectItemWithTitle:s];
                if (![key isEqualToString:@"Sync With Folder"] && ![key isEqualToString:@"All Edited Movies"]) {
                    s = [anOption objectAtIndex:1];
                    [[[row subviews] objectAtIndex:1] selectItemWithTitle:s];
                }
                s = [anOption lastObject];
                [[[row subviews] objectAtIndex:2] setStringValue:s];
            }
        }
    } else {
        [self add:nil];
    }
    [live setState:[options containsObject:@"live"]];
    if ([options containsObject:@"Match All"])
        [anyAll selectItemWithTitle:@"All"];
    else
        [anyAll selectItemWithTitle:@"Any"];
}

- (void)awakeFromNib
{
    // populate menus
    //[self add:nil];
    //[[[bigBox subviews] objectAtIndex:0] setAutoresizingMask:NSViewHeightSizable];
    [self syncWidgets];
}

@end
