//
//  KMPopMenuViewController.m
//  SignFlow
//
//  Created by Kdan on 2020/10/23.
//

#import "KMCustomButtonPopMenuViewController.h"
#import "NSButton+TitleColor.h"
#import <Masonry/Masonry.h>
#import "KMPopMenuButtonCell.h"

@interface KMPopMenuButton : NSButton

@end

@implementation KMPopMenuButton

- (instancetype)initWithFrame:(NSRect)frameRect {
    self = [super initWithFrame:frameRect];
    if (self) {
        [self addTrackingArea];
        self.wantsLayer = YES;
        self.layer.backgroundColor = [NSColor clearColor].CGColor;
        self.font = [NSFont systemFontOfSize:14];
    }
    return self;
}

+ (Class)cellClass {
    return [KMPopMenuButtonCell class];
}

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

- (void)mouseEntered:(NSEvent *)event {
    [super mouseEntered:event];
    
    if (self.enabled) {
        if (@available(macOS 10.14, *)) {
            self.layer.backgroundColor = [NSColor controlAccentColor].CGColor;
        } else {
            self.layer.backgroundColor = [NSColor blueColor].CGColor;
        }
        [self setTitleColor:[NSColor whiteColor]];
    }
}

- (void)mouseExited:(NSEvent *)event {
    [super mouseExited:event];
    
    if (self.enabled) {
        self.layer.backgroundColor = [NSColor clearColor].CGColor;
        [self setTitleColor:[NSColor labelColor]];
    }
}

- (NSSize)intrinsicContentSize {
    CGSize size = [super intrinsicContentSize];
    size.width += 45;
    return size;
}

- (void)setState:(NSControlStateValue)state {
    [super setState:state];
    if (state == NSControlStateValueOn) {
        if (@available(macOS 10.14, *)) {
            self.layer.backgroundColor = [NSColor controlAccentColor].CGColor;
        } else {
            self.layer.backgroundColor = [NSColor blueColor].CGColor;
        }
        [self setTitleColor:[NSColor whiteColor]];
    } else {
        self.layer.backgroundColor = [NSColor clearColor].CGColor;
        [self setTitleColor:[NSColor labelColor]];
    }
}

- (void)setTitle:(NSString *)title {
    [super setTitle:title];
    
    [self setTitleColor:[NSColor labelColor]];
}

@end

@interface KMCustomButtonPopMenuViewController ()

@end

@implementation KMCustomButtonPopMenuViewController

#pragma mark - Life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSInteger numberOfLine = 0;
    if ([self.dataSources respondsToSelector:@selector(numberOfLine)]) {
        numberOfLine = [self.dataSources numberOfLine];
    }
    if (numberOfLine < 1) {
        return;
    }
    NSView *referenceView = self.view;
    for (NSInteger i = 0; i < numberOfLine ; i++) {
        NSImage *image = nil;
        NSString *title = nil;
        if ([self.dataSources respondsToSelector:@selector(imageForLineAtIndex:)]) {
            image = [self.dataSources imageForLineAtIndex:i];
        }
        if ([self.dataSources respondsToSelector:@selector(stringForLineAtIndex:)]) {
            title = [self.dataSources stringForLineAtIndex:i];
        }

        KMPopMenuButton *v = nil;
        if (image) {
            v = [KMPopMenuButton buttonWithTitle:title image:image target:self action:@selector(buttonClicked:)];
        } else {
            v = [KMPopMenuButton buttonWithTitle:title target:self action:@selector(buttonClicked:)];
        }

        v.bordered = NO;
        v.tag = i;
        if ([self.dataSources respondsToSelector:@selector(itemEnableAtIndex:)]) {
            v.enabled = [self.dataSources itemEnableAtIndex:i];
        }
        
        [self.view addSubview:v];
        
        [v mas_makeConstraints:^(MASConstraintMaker *make) {
            if ([referenceView isEqual:self.view]) {
                make.top.equalTo(referenceView).offset(10);
            } else {
                make.top.equalTo(referenceView.mas_bottom).offset(2);
            }
            make.left.right.equalTo(self.view);
            make.height.offset(24);
        }];
        referenceView = v;
        
        if ([self.dataSources respondsToSelector:@selector(needHightLightLineAtIndex:)]) {
            if ([self.dataSources needHightLightLineAtIndex:i]) {
                v.state = NSControlStateValueOn;
            }
        }
        
        if ([self.dataSources respondsToSelector:@selector(needInsertSeperateLineAtIndex:)]) {
            if ([self.dataSources needInsertSeperateLineAtIndex:i]) {
                NSBox *box = [[NSBox alloc] initWithFrame:CGRectZero];
                box.boxType = NSBoxSeparator;
                [self.view addSubview:box];
                [box mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(referenceView.mas_bottom).offset(2);
                    make.left.equalTo(self.view).offset(21);
                    make.centerX.equalTo(self.view);
                }];
                referenceView = box;
            }
        }

        if (i == numberOfLine - 1) {
            [referenceView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.view).offset(-10);
            }];
        } 
    }
}

- (void)buttonClicked:(NSButton *)sender {
    if ([self.delegate respondsToSelector:@selector(customViewButtonPopDidSelectIndex:)]) {
        [self.delegate customViewButtonPopDidSelectIndex:sender.tag];
    }
}

@end