//
//  NvShapeButton.m
//  SDKDemo
//
//  Created by shizhouhu on 2018/7/18.
 //
//

#import "NvShapeButton.h"
#import "Masonry.h"
#import "NvUtils.h"
#import "UIColor+NvColor.h"

@implementation NvShapeButtonItem

@end

@implementation NvShapeButton {
    UIView *mask;
    NvShapeEnum shape;
}

- (id)initWithFrame:(CGRect)frame item: (NvShapeButtonItem *)item{
    self = [super initWithFrame:frame];
    
    self.image = UIImageView.new;
    [self addSubview:self.image];
    [self.image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(45*SCREANSCALE));
        make.height.equalTo(@(45*SCREANSCALE));
        make.top.and.left.and.right.equalTo(self);
    }];
    
    mask = UIView.new;
    mask.backgroundColor = [UIColor nv_colorWithHexRGBA:@"#4A90E277"];
    [self.image addSubview:mask];
    [mask mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.and.left.and.right.equalTo(self.image);
    }];
    
    self.label = UILabel.new;
    [self addSubview:self.label];
    self.label.textAlignment = NSTextAlignmentCenter;
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(45*SCREANSCALE));
        make.height.equalTo(@(27*SCREANSCALE));
        make.top.equalTo(self.image.mas_bottom).offset((2*SCREANSCALE));
        make.centerX.equalTo(self.image);
    }];
    
    [self.image setImage:[UIImage imageNamed:item.imagePath]];
    [self.label setText:item.text];
    self.label.textColor = item.selected ? [UIColor nv_colorWithHexRGB:@"#4A90E2"] : [UIColor nv_colorWithHexRGB:@"#909293"];
    mask.hidden = !item.selected;
    shape = item.shape;
    
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonClicked)];
    [self addGestureRecognizer:tap];
    
    return self;
}

- (void)onButtonClicked {
    if ([self.delegate respondsToSelector:@selector(onButtonClicked:)]) {
        [self.delegate onButtonClicked:shape];
    }
}

- (void)setSelect:(BOOL)select {
    self.label.textColor = select ? [UIColor nv_colorWithHexRGB:@"#4A90E2"] : [UIColor nv_colorWithHexRGB:@"#909293"];
    mask.hidden = !select;
}

@end
