//
//  backgroundView.m
//  Umbra
//
//  Created by Matteo Rattotti on 5/30/07.
//  Copyright 2007 Shinyfrog. All rights reserved.
//

#import "backgroundView.h"


@implementation backgroundView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Alloc and init for the background color and of NSImageview
        bgColor = [[NSColor colorWithCalibratedWhite:0.1 alpha:0.75]retain];
        bgImage = [[NSImageView alloc]initWithFrame:[self frame]];
    }
    return self;
}

- (void)addBgImage:(NSDictionary*) Options{

    // Getting user prefs
    NSString* imagePath = [Options objectForKey:@"path"];
    NSString* placement = [Options objectForKey:@"placement"];
    bgColor = [[Options objectForKey:@"bgColor"] retain];
    NSLog(@"%@", bgColor);

    // Loading image
    NSImage* bg = [[NSImage alloc]initWithContentsOfFile:imagePath];

    // Getting size of image
    //NSSize imgSize = [bg size];

    // i'm using the imagerep for having the real pixel X pixel size
    NSImageRep *rep = [NSImageRep imageRepWithContentsOfFile:imagePath];
    NSLog(@"rep -> %@", rep);
    NSLog(@"pixels: %d, %d", [rep pixelsHigh], [rep pixelsWide]);
    
    // Getting image size
    NSSize imgSize;
    imgSize.width = [rep pixelsWide];
    imgSize.height = [rep pixelsHigh];
    
    NSLog(@"%f, %f", imgSize.width, imgSize.height);
    NSLog(@"%f, %f", [self frame].size.width, [self frame].size.height);
    
    NSSize immSize = imgSize;
    
    // Checking Desktop & Screensave prefs for the placement
    if([placement isEqualToString:@"Crop"])
    {
        // if the image is bigger than screen we will use simply the fillscreen
        /*if(imgSize.height > [self frame].size.height || imgSize.width > [self frame].size.width)
        {
            immSize = [self fillScreen:immSize imgView:bgImage];
        }*/
        // Otherwise we need to stretch
        //else{
            immSize = [self stretchToFillScreen:bg imgView:bgImage];
        //}
    }
    else if ([placement isEqualToString:@"FillScreen"])
    {
        immSize = [self stretchToFillScreen:bg imgView:bgImage];
    }
  
    else if ([placement isEqualToString:@"Centered"])
    {
        [bgImage setAlignment:NSImageAlignCenter];
        [bgImage setImageScaling:NSScaleNone];
    }

    
    // Setting the new size    
    [bg setSize:immSize];
    
    // Setting the image in the view
    [bgImage setImage:bg];
    // Adding the NSImageview in the NSView on background
    [self addSubview:bgImage];
    
    // Releasing memory that we don't use anymore
    [bg release];

}
- (NSSize) stretchToFillScreen:(NSImage*) bImage imgView:(NSImageView*) bView{
    NSSize imgSize = [bImage size];
    NSSize immSize;

    
    // if the image is more big than the monitor
    if(imgSize.height > [self frame].size.height || imgSize.width > [self frame].size.width){
        // more width than height
        if(imgSize.width > imgSize.height){
                immSize.width = ( imgSize.width * [self frame].size.height ) / imgSize.height;
                immSize.height = [self frame].size.height;

                [bView setImageScaling:NSScaleNone];
                [bView setImageAlignment:NSImageAlignCenter];
                
        }
        // more height than width
        else{    
            immSize.height = [self frame].size.height;
            immSize.width = [self frame].size.width;
                
            [bView setImageScaling:NSScaleToFit];
        }
    
    }
    else{
        immSize.height = [self frame].size.height;
        immSize.width = [self frame].size.width;
                
        [bView setImageScaling:NSScaleToFit];

    }
    
    [bImage setScalesWhenResized:1];
    
    return immSize;
}

// Function for Fill Screen option
- (NSSize) fillScreen:(NSSize)imgSize imgView:(NSImageView*) bView{
    NSSize immSize;
    // Checking the Ration of the image for find the appropriate proportion
    float frameRatio = [self frame].size.width / [self frame].size.height;
    float imageRatio = imgSize.width / imgSize.height;
    
    // if the image has a ration less than screen we need to find a new heigth
    if(imageRatio < frameRatio){
        immSize.height = ([self frame].size.width * imgSize.height ) / imgSize.width;
        immSize.width = [self frame].size.width;
        [bgImage setImageScaling:NSScaleNone];
    }
    // if the ration is equal we only need to fit the screensize
    else if(imageRatio == frameRatio)
    {
        immSize.height = [self frame].size.height;
        immSize.width = [self frame].size.width;
        [bgImage setImageScaling:NSScaleToFit];
    }
    // if is more, we need to find another width
    else{
        immSize.height = [self frame].size.height;
        immSize.width = ([self frame].size.height * imgSize.width) / imgSize.height;
        [bgImage setImageScaling:NSScaleNone];
    }
    return immSize;
}

- (void)drawRect:(NSRect)rect {
    // Setting color for the background window (always from Desktop & Screensaver prefs)
    [bgColor set];

    // Filling the view with the color
    [NSBezierPath fillRect:[self frame]];

}

@end
