//
//  KMTableRowView.m
//  PDF Reader Pro Edition
//
//  Created by zhipeng jiang on 2020/11/25.
//

#import "KMTableRowView.h"

@implementation KMTableRowView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self addTrackingArea];
        
        self.selectionInset = NSEdgeInsetsZero;
        self.selectionRadius = 0.f;
    }
    return self;
}

- (void)setHasBottomLine:(BOOL)hasBottomLine {
    _hasBottomLine = hasBottomLine;
    
    [self setNeedsDisplay:YES];
}

- (void)setBottomLineColor:(NSColor *)bottomLineColor {
    _bottomLineColor = bottomLineColor;
    
    [self setNeedsDisplay:YES];
}

#pragma mark - Private Methods

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

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    if (self.hasBottomLine) {
        NSColor *color = self.bottomLineColor;
        if (color == nil) {
            color = [NSColor lightGrayColor];
        }
        [color setStroke];
        [color setFill];
        NSRect lineRect = NSMakeRect(0, NSHeight(self.bounds)-1, NSWidth(self.bounds), 1);
        NSBezierPath *linePath = [NSBezierPath bezierPathWithRoundedRect:lineRect xRadius:0 yRadius:0];
        [linePath fill];
        [linePath stroke];
    }
}

- (void)drawSelectionInRect:(NSRect)dirtyRect {
    if (!self.selectionBackgroundColorBlock) {
//        [super drawSelectionInRect:dirtyRect];
        NSRect selectionRect = self.bounds;
        [[NSColor lightGrayColor] setFill];
        NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:selectionRect xRadius:0 yRadius:0];
        [selectionPath fill];
        return;
    }

//    NSRect selectionRect = NSInsetRect(self.bounds, 0, 0);
    NSRect selectionRect = NSMakeRect(self.selectionInset.left, self.selectionInset.top, NSWidth(self.bounds)-self.selectionInset.left-self.selectionInset.right, NSHeight(self.bounds)-self.selectionInset.top-self.selectionInset.bottom);
    NSColor *color = self.selectionBackgroundColorBlock();
    [color setStroke];
    [color setFill];
    
    NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:selectionRect xRadius:self.selectionRadius yRadius:self.selectionRadius];
    [selectionPath fill];
    [selectionPath stroke];
}

- (void)mouseEntered:(NSEvent *)event {
    [super mouseEntered:event];
}

@end