//
//  ICeCoffEEWebKit.m
//  ICeCoffEE APE
//
//  Created by Nicholas Riley on Sun Jan 19 2003.
//  Copyright (c) 2003 Nicholas Riley. All rights reserved.
//

#import "ICeCoffEEWebKit.h"
#import <unistd.h>

// from WebCoreBridge.h
@interface WebCoreBridge

- (NSDictionary *)elementAtPoint:(NSPoint)point;

- (void)setSelectionFrom:(id /* WebDOMNode */)start startOffset:(int)startOffset to:(id /* WebDOMNode */)end endOffset:(int) endOffset;
- (void)deselectAll;

- (void)forceLayout;

- (NSString *)selectedString;

- (id /* WebDOMNode */)selectionStart;
- (int)selectionStartOffset;
- (id /* WebDOMNode */)selectionEnd;
- (int)selectionEndOffset;

@end

// from WebKit, not yet public
@interface WebHTMLView

- (WebCoreBridge *)_bridge;
- (NSDictionary *)_elementAtPoint:(NSPoint)point;

@end

@implementation ICeCoffEEWebKit

- (NSMenu *)menuForEvent:(NSEvent *)e;
{
    NSMenu *myMenu = [super menuForEvent: e];
    return ICCF_MenuForEvent(self, myMenu, e);
}

static NSEvent *downEvent = nil;
typedef struct {
    int startOffset;
    int endOffset;
    id fromNode;
    id toNode;
} ICCF_WebCoreSelection;
static NSString *selectedString = nil; // XXX remove when we can do this properly

static ICCF_WebCoreSelection selection = {nil, nil, 0, 0};

void ICCF_GetWebCoreBridgeSelection(WebCoreBridge *bridge, ICCF_WebCoreSelection *ioSel) {
    [ioSel->fromNode release]; ioSel->fromNode = nil;
    [ioSel->toNode release]; ioSel->toNode = nil;
    ioSel->startOffset = [bridge selectionStartOffset];
    ioSel->endOffset = [bridge selectionEndOffset];
    if (ioSel->startOffset != 0 && ioSel->endOffset != 0) {
        ioSel->fromNode = [[bridge selectionStart] retain];
        ioSel->toNode = [[bridge selectionEnd] retain];
    }
}

void ICCF_SetWebCoreBridgeSelection(WebCoreBridge *bridge, ICCF_WebCoreSelection *inSel) {
    if (inSel->fromNode == nil || inSel->toNode == nil)
        [bridge deselectAll];
    [bridge setSelectionFrom: inSel->fromNode
                 startOffset: inSel->startOffset
                          to: inSel->toNode
                   endOffset: inSel->endOffset];
}

- (void)mouseDown:(NSEvent *)e;
{
    // don't want command-option-click, command-shift-click, etc. to trigger
    if (ICCF_enabled && ICCF_EventIsCommandMouseDown(e)) {
        // save selection: it will be deselected on super mouseDown
        WebCoreBridge *bridge = [(WebHTMLView *)self _bridge];
        ICCF_GetWebCoreBridgeSelection(bridge, &selection);
        // XXX hack until we have something better
        [selectedString release]; selectedString = nil;
        selectedString = [[bridge selectedString] retain];
        downEvent = e;
    } else {
        downEvent = nil;
    }
    [super mouseDown: e];
}

- (void)mouseUp:(NSEvent *)e;
{
    [super mouseUp: e];
    if (downEvent != nil) {
        NSPoint downPt = [downEvent locationInWindow];
        NSPoint upPt = [e locationInWindow];
        downEvent = nil;
        // XXX make hysterisis a constant
        if (abs(downPt.x - upPt.x) > 4 && abs(downPt.y - upPt.y) > 4)
            return;

        NS_DURING
            WebCoreBridge *bridge = [(WebHTMLView *)self _bridge];
            NSPoint viewClickPt = [self convertPoint: downPt fromView: nil];
            NSDictionary *elementDict = [(WebHTMLView *)self _elementAtPoint: viewClickPt];
            ICLog(@"elementDict: %@", elementDict);
            NSAssert([elementDict count] != 0, @"ShouldnÕt get empty element dictionary"); // XXX clarify
            if ([elementDict objectForKey: @"WebElementLinkURL"] != nil) {
                ICLog(@"got a link");
                NS_VOIDRETURN; // donÕt activate on links
            }
            // XXX works, but doesn't update
            if (selectedString == nil || [selectedString length] == 0)
                NS_VOIDRETURN;
            int i;
            for (i = 0 ; i < 3 ; i++) {
                [bridge deselectAll];
                [self display];
                usleep(60000);
                ICCF_SetWebCoreBridgeSelection(bridge, &selection);
                [self display];
                usleep(60000);
            }
            ICCF_SetWebCoreBridgeSelection(bridge, &selection);
            [self display];
            ICCF_StartIC();
            ICCF_LaunchURL(selectedString);
        NS_HANDLER
            ICCF_HandleException(localException);
        NS_ENDHANDLER

        ICCF_StopIC();

        // ICLog(@"selection start %d end %d string %@", [bridge selectionStartOffset], [bridge selectionEndOffset], [bridge selectedString]);
        // [bridge forceLayout];
        // ICLog(@"force start %d end %d string %@", [bridge selectionStartOffset], [bridge selectionEndOffset], [bridge selectedString]);

        // check selected string, first
        // fabricate triple-click, examine selected string

        // setSelectionFrom:startOffset:to:endOffset:

        // ICCF_LaunchURLFromTextView(self);
    }        
}



@end
