/*
# Created by Matteo Rattotti on 13/03/06.
# Copyright (c) 2006 - 2007 Shiny Frog. All rights reserved.
# Contact matteo.rattotti@shinyfrog.net for any problem

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
//
//  webImpressor.m
//  Netfixer
//
//  Created by Matteo Rattotti on 10/07/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "webImpressor.h"
#import "netFixerAppDelegate.h"
#import <WebKit/WebKit.h>


@implementation webImpressor

- (id) init{
    [super init];
    imgType = NSPNGFileType;
    rect = NSMakeRect(0,0,800,600);
    size = NSMakeSize(800,600);
	
	WebPreferences* wprefs = [WebPreferences standardPreferences];
	[wprefs setJavaEnabled:1];
	[wprefs setJavaScriptCanOpenWindowsAutomatically:0];
	
    return self;
}

- (void) dealloc{
    [super dealloc];
}

- (void) setProgWell: (NSProgressIndicator *)p{
    pwell = p;
}

- (void) setFileName: (NSString *) filename{
    fName = [[NSString alloc]initWithString:filename];
}

- (void) setFormat: (NSBitmapImageFileType) type{
    imgType = type;
}

- (void) setCanvas:(NSRect) imgRect canvasSize: (NSSize) cSize{
    rect = imgRect;
    size = cSize;
}

- (NSBitmapImageRep *) view2bmp: (NSView *) view{
    [view lockFocus];
    NSBitmapImageRep *image = [NSBitmapImageRep alloc];
    [image initWithFocusedViewRect:[view bounds]];
    [view unlockFocus];
    return image;
}

- (void) correctView: (NSView *) view{
    NSRect bound = [view bounds];
    NSSize s = bound.size;
    [[view window] display];
    [[view window] setContentSize:s];

    [view setFrame:[view bounds]];
}

- (void) resetWebView: (WebView *) webview{
    //NSRect rect = NSMakeRect(0,0,800,600);
    //NSSize size = NSMakeSize(800,600);
    [[webview window] setContentSize:size];
    [webview setFrame:rect];
}

- (void) saveImage: (NSString *) name imageRep: (NSBitmapImageRep *) img{
		NSLog(@"save image with path");
        if([[fName pathExtension] isEqualToString:@""]){
           if(imgType == NSPNGFileType){fName = [fName stringByAppendingString:@".png"];}
           else if(imgType == NSTIFFFileType){fName = [fName stringByAppendingString:@".tif"];}
           else if(imgType == NSGIFFileType){fName = [fName stringByAppendingString:@".gif"];}
           else if(imgType == NSJPEGFileType){fName = [fName stringByAppendingString:@".jpg"];}
           else if(imgType == NSBMPFileType){fName = [fName stringByAppendingString:@".bmp"];}
        }
		NSLog(@"caccamo");
		NSLog(@"%@", img);
		NSLog(@"%@", fName);
        [[img representationUsingType:imgType properties:nil] 
            writeToFile:fName atomically:YES];
       
		/* Stopping animation well*/
        [pwell stopAnimation:self];

}

- (void)webView:(WebView *)webview didFinishLoadForFrame:(WebFrame *)frame{
	NSLog(@"finished loading");
    if(frame == [webview mainFrame]){
		NSLog(@"entered");
        [self resetWebView:webview];
        /*NSString* url = [[[[frame dataSource]initialRequest]URL]absoluteString];
        NSView *view = [[frame frameView] documentView];
        [self correctView:view];*/
        
        NSTimeInterval ti = [[NSApp delegate] delayValue];
        [self performSelector:@selector(saveImage:) withObject:frame afterDelay:ti];
        //NSBitmapImageRep *image = [self view2bmp:view];
        //[self saveImage:url imageRep:image];
        }
}

- (void)saveImage:(WebFrame*) frame{
		NSLog(@"save image");
        NSString* url = [[[[frame dataSource]initialRequest]URL]absoluteString];
        NSView *view = [[frame frameView] documentView];
        [self correctView:view];
		
        NSBitmapImageRep *image = [self view2bmp:view];
        [self saveImage:url imageRep:image];
}

- (void)webView:(WebView *)webview didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame{
    if(frame == [webview mainFrame]){
            NSAlert *a = [NSAlert alertWithMessageText:@"Error loading this url" 
                 defaultButton:@"ok" 
                 alternateButton:nil 
                 otherButton:nil 
                 informativeTextWithFormat:@"Please check address for typo, or maybe your connection..."];
        [a runModal];
        /* Stopping animation well*/
		
        [pwell stopAnimation:self];
    }
}

- (void)webView:(WebView *)webview didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame{
    if(frame == [webview mainFrame]){
            NSAlert *a = [NSAlert alertWithMessageText:@"Error loading this url" 
                 defaultButton:@"ok" 
                 alternateButton:nil 
                 otherButton:nil 
                 informativeTextWithFormat:@"Please check address for typo, or maybe your connection..."];
        [a runModal];
        /* Stopping animation well*/
        [pwell stopAnimation:self];
    }
}

@end
