//
//  KMBatchTableRowView.m
//  PDF Reader Pro Edition
//
//  Created by zhipeng jiang on 2020/11/2.
//

#import "KMBatchTableRowView.h"

@interface KMBatchTableRowView ()

@property (nonatomic, strong) NSView *backgroundView;

@end

@implementation KMBatchTableRowView

- (instancetype)initWithFrame:(NSRect)frameRect {
    if (self = [super initWithFrame:frameRect]) {
        [self addSubview:self.backgroundView];
        
        self.backgroundView.hidden = YES;
    }
    return self;
}

- (void)layout {
    [super layout];
    
    self.backgroundView.frame = 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);
}

- (void)setSelectionInset:(NSEdgeInsets)selectionInset {
    [super setSelectionInset:selectionInset];
    
    [self setNeedsLayout:YES];
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    
    if (selected) {
        self.backgroundView.hidden = YES;
    }
}

#pragma mark - Mouse Event

- (void)mouseExited:(NSEvent *)event {
    [super mouseExited:event];
    
    self.backgroundView.hidden = YES;
    for (NSUInteger i = 0; i < self.subviews.count; i++) {
        NSView *v = [self.subviews objectAtIndex:i];
        for (NSUInteger j = 0; j < v.subviews.count; j++) {
            NSView *sub = [v.subviews objectAtIndex:j];
            if ([sub isKindOfClass:[NSButton class]]) {
                sub.hidden = YES;
            }
        }
    }
}

- (void)mouseEntered:(NSEvent *)event {
    [super mouseEntered:event];
    
    if (self.isSelected) {
        self.backgroundView.hidden = YES;
    } else {
        self.backgroundView.hidden = NO;
    }
    
    for (NSUInteger i = 0; i < self.subviews.count; i++) {
        NSView *v = [self.subviews objectAtIndex:i];
        for (NSUInteger j = 0; j < v.subviews.count; j++) {
            NSView *sub = [v.subviews objectAtIndex:j];
            if ([sub isKindOfClass:[NSButton class]]) {
                sub.hidden = NO;
            }
        }
    }
}


#pragma mark -
#pragma mark - Getting

- (NSView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[NSView alloc] init];
        _backgroundView.wantsLayer = YES;
        _backgroundView.layer.backgroundColor = [NSColor clearColor].CGColor;
    }
    return _backgroundView;
}

@end