//
//  TargetMouseBtn.m
//  Enjoy
//
//  Created by Yifeng Huang on 7/27/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "TargetMouseBtn.h"

@implementation TargetMouseBtn

@synthesize which;

+ (NSString *)serializationCode {
    return @"mbtn";
}

- (NSDictionary *)serialize {
    return @{ @"type": @"mbtn", @"which": @(self.which) };
}

+ (Target *)targetDeserialize:(NSDictionary *)serialization
                  withConfigs:(NSArray *)configs {
	TargetMouseBtn *target = [[TargetMouseBtn alloc] init];
    target.which = [serialization[@"which"] intValue];
	return target;
}

-(void) trigger {
    NSRect screenRect = [[NSScreen mainScreen] frame];
    NSInteger height = screenRect.size.height;
    NSPoint mouseLoc = [NSEvent mouseLocation];
    CGEventType eventType = (which == kCGMouseButtonLeft) ? kCGEventLeftMouseDown : kCGEventRightMouseDown;
    CGEventRef click = CGEventCreateMouseEvent(NULL,
                                               eventType,
                                               CGPointMake(mouseLoc.x, height - mouseLoc.y),
                                               which);
    CGEventPost(kCGHIDEventTap, click);
    CFRelease(click);
}

-(void) untrigger {
    NSRect screenRect = [[NSScreen mainScreen] frame];
    NSInteger height = screenRect.size.height;
    NSPoint mouseLoc = [NSEvent mouseLocation];
    CGEventType eventType = (which == kCGMouseButtonLeft) ? kCGEventLeftMouseUp : kCGEventRightMouseUp;
    CGEventRef click = CGEventCreateMouseEvent(NULL,
                                               eventType,
                                               CGPointMake(mouseLoc.x, height - mouseLoc.y),
                                               which);
    CGEventPost(kCGHIDEventTap, click);
    CFRelease(click);
}

@end
