//
//  KMBlankView.m
//  PDF Reader Pro Edition
//
//  Created by zhipeng jiang on 2020/12/23.
//

#import "KMBlankView.h"

@interface KMBlankView ()

@property (nonatomic, strong) NSButton *button;

@end

@implementation KMBlankView

-(void)awakeFromNib {
    [super awakeFromNib];
    self.titleLabel.font = [NSFont systemFontOfSize:14 weight:NSFontWeightSemibold];
    self.secondTitleLabel.font = [NSFont systemFontOfSize:14];
    
    self.titleLabel.stringValue = @"";
    self.secondTitleLabel.stringValue = @"";
    [self addTrackingArea];
    self.wantsLayer = YES;
    
    [self.customView addSubview:self.button];
    
    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
}

- (void)layout {
    [super layout];
    
    self.button.frame = self.imageView.frame;
}

#pragma mark -
#pragma mark - Drag


- (void)draggingExited:(nullable id <NSDraggingInfo>)sender {}

- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
    NSPasteboard *pboard = [sender draggingPasteboard];
    BOOL isCanDrag = NO;
    NSDragOperation result = NSDragOperationNone;
    if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]])
    {
        NSArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
        for (NSString* path in fileNames) {
            if ([self.allowedFileTypes containsObject:path.pathExtension.lowercaseString]) {
                isCanDrag = YES;
            } else {
                isCanDrag = NO;
                break;
            }
        }
    }
    self.layer.borderColor = [NSColor clearColor].CGColor;

    if (isCanDrag) {
        
        result = NSDragOperationCopy;
    }
    return result;
}

- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender
{
    NSPasteboard *pboard = [sender draggingPasteboard];
    if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]]) {
        NSMutableArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
        if (self.dragSuccessBlock) {
            self.dragSuccessBlock(fileNames);
        }
    }
    return YES;
}

#pragma mark -
#pragma mark - mouse Methods

- (void)addTrackingArea {
    NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.imageView.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways owner:self userInfo:nil];
    [self.imageView addTrackingArea:trackingArea];
}

- (void)mouseEntered:(NSEvent *)event {
    [super mouseEntered:event];
    if (self.mouseActionCallBack) {
        self.mouseActionCallBack(KMBlankViewMouseEventType_MouseEnter);
    }
}

- (void)mouseExited:(NSEvent *)event {
    [super mouseExited:event];
    
    if (self.mouseActionCallBack) {
        self.mouseActionCallBack(KMBlankViewMouseEventType_MouseExit);
    }
}

//- (void)mouseDown:(NSEvent *)event {
//    [super mouseDown:event];
//    NSPoint point = [event locationInWindow];
//    point = [self convertPoint:point toView:self.imageView];
//    if (self.mouseActionCallBack && CGRectContainsPoint(self.imageView.bounds, point)) {
//        self.mouseActionCallBack(KMBlankViewMouseEventType_MouseDown);
//    }
//}

- (void)mouseUp:(NSEvent *)event {
    [super mouseUp:event];
    NSPoint point = [event locationInWindow];
    point = [self convertPoint:point toView:self.imageView];
    if (self.mouseActionCallBack && CGRectContainsPoint(self.imageView.bounds, point)) {
        self.mouseActionCallBack(KMBlankViewMouseEventType_MouseUp);
    }
}

#pragma mark -
#pragma mark - User Actions

- (void)buttonDidClick {
    if (self.mouseActionCallBack) {
        self.mouseActionCallBack(KMBlankViewMouseEventType_MouseDown);
    }
}

#pragma mark -
#pragma mark - Getting

- (NSButton *)button {
    if (!_button) {
        _button = [[NSButton alloc] init];
        _button.bordered = false;
        _button.title = @"";
        _button.target = self;
        _button.action = @selector(buttonDidClick);
    }
    return _button;
}

@end