#import "UmbraController.h"
#import "SFTranFadeWindow.h"
#import "backgroundView.h"

@implementation UmbraController
- (void)awakeFromNib{
    NSLog(@"Umbra awake!!");
    NSLog(@"%@", [self desktopInfo]);
    
    [self createWindow];
    
    [bView addBgImage:[self desktopInfo]];

}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
    NSString *asd = @"asd";
    
    NSLog(asd);
    
    [self toggleBackground];

}

- (void) createWindow{
    
    // Getting screen bounds
    NSScreen* mainScreen = [NSScreen mainScreen];
    NSRect screenBounds = [mainScreen frame];

    // Creating the transparent and fading window
    tWindow = [[SFTranFadeWindow alloc] initWithContentRect:screenBounds 
                                                    styleMask: NSBorderlessWindowMask 
                                                    backing:NSBackingStoreBuffered 
                                                    defer:NO];
    // Creating the window view
    bView = [[backgroundView alloc]initWithFrame:screenBounds];

    // Setting the View and putting the window on desktop level
    [tWindow setContentView:bView];
    [tWindow setLevel:kCGDesktopIconWindowLevel];
    [tWindow orderFront:self];
}

- (void) toggleBackground{
    if([tWindow alphaValue] == 0){
        [tWindow fadeIn];
    }
    else{
        [tWindow fadeOut];
    }
    
}

- (NSDictionary *) desktopInfo{
    // Getting the user preferencies for the background
    NSDictionary* d = [[NSUserDefaults standardUserDefaults] persistentDomainForName:@"com.apple.Desktop"];
    NSString* imagePath = [[[d objectForKey:@"Background"] objectForKey:@"default"] objectForKey:@"ImageFilePath"];
    NSString* placement = [[[d objectForKey:@"Background"] objectForKey:@"default"] objectForKey:@"Placement"];
    NSColor* bgColor = [NSColor colorWithDeviceRed: [[[[[d objectForKey:@"Background"] objectForKey:@"default"]objectForKey:@"BackgroundColor"]objectAtIndex:0]floatValue]
                                            green:[[[[[d objectForKey:@"Background"] objectForKey:@"default"]objectForKey:@"BackgroundColor"]objectAtIndex:1]floatValue] 
                                            blue:[[[[[d objectForKey:@"Background"] objectForKey:@"default"]objectForKey:@"BackgroundColor"]objectAtIndex:2]floatValue]
                                            alpha:1];
    
    //NSLog(@"%f", [[[[[d objectForKey:@"Background"] objectForKey:@"default"]objectForKey:@"BackgroundColor"]objectAtIndex:0]floatValue]);
    
    // Returning only what we need
    return [NSDictionary dictionaryWithObjectsAndKeys:
                                                imagePath, @"path",
                                                placement, @"placement",
                                                bgColor, @"bgColor",
                                                nil];
}

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
{
    [self toggleBackground];
    return 1;
}


@end
