//
//  ERTCBeautyItemCollectionViewCell.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/8/16.
//

#import "ERTCBeautyItemCollectionViewCell.h"

@interface ERTCBeautyItemCollectionViewCell ()

@property (nonatomic, strong) UIView *itemBGView;
@property (nonatomic, strong) UIButton *itemImageBtn;
@property (nonatomic, strong) UIButton *itemTitleBtn;
@end

@implementation ERTCBeautyItemCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        
        [self.contentView addSubview:self.itemBGView];
        [self.contentView addSubview:self.itemImageBtn];
        [self.contentView addSubview:self.itemTitleBtn];
        [self.itemBGView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.width.height.ysmas_equalTo(40);
            make.top.equalTo(self.contentView).with.offset(13);
            make.centerX.equalTo(self.contentView);
        }];
        
        [self.itemImageBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.center.equalTo(self.itemBGView);
            make.width.height.ysmas_equalTo(30);
        }];
        
        [self.itemTitleBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.centerX.equalTo(self.itemBGView);
            make.bottom.equalTo(self.contentView);
        }];
    }
    
    return self;
}

-(void)setModel:(ERTCBeautyModel *)model {
    _model = model;
    [self.itemTitleBtn setTitle:model.title forState:(UIControlStateNormal)];
    
    UIImage *image = [UIImage imageNamed:model.normalImageName];
    [self.itemImageBtn setImage:image forState:(UIControlStateNormal)];
    
    UIColor *selectedColor = [UIColor colorWithRed:(0x3E/255.f) green:(0x80/255.f) blue:(0xFF/255.f) alpha:1];
    UIColor *unselectedColor = [UIColor whiteColor];
    if (model.selected == YES) {
        self.itemBGView.backgroundColor = selectedColor;
        [self.itemTitleBtn setTitleColor:selectedColor forState:(UIControlStateNormal)];
    }
    else
    {
        self.itemBGView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2];
        [self.itemTitleBtn setTitleColor:unselectedColor forState:(UIControlStateNormal)];
    }
}

#pragma mark - UI Getter Method
-(UIView *)itemBGView {
    if (!_itemBGView)
    {
        _itemBGView = [[UIView alloc] init];
        _itemBGView.layer.cornerRadius = 20;
    }
    
    return _itemBGView;
}

-(UIButton *)itemTitleBtn {
    if (!_itemTitleBtn) {
        _itemTitleBtn = [[UIButton alloc] init];
        _itemTitleBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _itemTitleBtn.enabled = NO;
    }
    
    return _itemTitleBtn;
}

-(UIButton *)itemImageBtn {
    if (!_itemImageBtn) {
        _itemImageBtn = [[UIButton alloc] init];
        _itemImageBtn.backgroundColor = [UIColor clearColor];
        _itemImageBtn.enabled = NO;
    }
    
    return _itemImageBtn;
}

-(void)dealloc {
    NSLog(@"ERTCBeautyItemCollectionViewCell dealloc");
}

+(NSString *)cellReuseId {
    return @"ERTCBeautyItemCollectionViewCellID";
}

@end
