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

#import "ERTCBeautyItemBlurCollectionViewCell.h"
#import "ERTCBeautyModel.h"

@interface ERTCBeautyItemBlurCollectionViewCell ()

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

@end

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

-(void)layoutSubviews {
    [super layoutSubviews];
    if (self.model.selected == NO) {
        [self.pathLayer removeFromSuperlayer];
    } else {
        if (CGRectGetWidth(self.itemBGView.frame) > 0 && self.pathLayer == nil) {
            CAShapeLayer *layer = [CAShapeLayer new];
            layer.lineWidth = 4;
            //圆环的颜色
            layer.strokeColor = [UIColor colorWithRed:(0x3E/255.f) green:(0x80/255.f) blue:(0xFF/255.f) alpha:1].CGColor;
            //背景填充色
            layer.fillColor = [UIColor clearColor].CGColor;
            //设置半径为100
            CGFloat radius = CGRectGetWidth(self.itemBGView.frame)/2.f;
            //按照顺时针方向
            BOOL clockWise = true;
            //初始化一个路径
            CGPoint center = CGPointMake(CGRectGetMinX(self.itemBGView.frame) + radius, CGRectGetMinY(self.itemBGView.frame) + radius);
            UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:(0) endAngle:M_PI*2 clockwise:clockWise];
            layer.path = [path CGPath];
            self.pathLayer = layer;

            [self.itemBGView.layer addSublayer:layer];
        }
    }
}

-(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.itemTitleBtn setTitleColor:selectedColor forState:(UIControlStateNormal)];
    }
    else
    {
        [self.itemTitleBtn setTitleColor:unselectedColor forState:(UIControlStateNormal)];
    }
}

#pragma mark - UI Getter Method
-(UIView *)itemBGView {
    if (!_itemBGView)
    {
        _itemBGView = [[UIView alloc] init];
        _itemBGView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2];
    }
    
    return _itemBGView;
}

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

-(UIButton *)itemImageBtn {
    if (!_itemImageBtn) {
        _itemImageBtn = [[UIButton alloc] init];
        _itemImageBtn.backgroundColor = [UIColor clearColor];
        _itemImageBtn.clipsToBounds = YES;
        _itemImageBtn.userInteractionEnabled = NO;
        _itemImageBtn.layer.cornerRadius = 20;
    }
    
    return _itemImageBtn;
}

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

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

@end
