//
//  SLBaseCellView.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/9.
//

#import "SLBaseCellView.h"

@implementation SLBaseCellView

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addContentView];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addContentView];
    }
    return self;
}

- (void)addContentView {
    [[NSBundle mainBundle] loadNibNamed:@"SLBaseCellView" owner:self options:nil];
    self.contentView.frame = self.bounds;
    [self addSubview:self.contentView];
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.equalTo(self.contentView);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.leftImageView.mas_right).offset(12);
    }];
    [self.rightImageViw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-16);
        make.centerY.equalTo(self.contentView);
    }];
    [self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.rightImageViw.mas_left).offset(-12);
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftImageView);
        make.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-1);
        make.height.mas_equalTo(1);
    }];
}

- (void)updateConstraintsWith:(SLBaseCellViewType)type {
    self.rightImageViw.hidden = NO;
    self.lineView.hidden = NO;
    self.leftImageView.hidden = NO;
    switch (type) {
        case SLBaseCellView1:
        {
            self.leftImageView.hidden = YES;
//            self.lineView.backgroundColor = [UIColor redColor];
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(12);
                make.centerY.equalTo(self.contentView);
            }];
            [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.titleLabel);
                make.right.equalTo(self.contentView);
                make.bottom.equalTo(self.contentView.mas_bottom).offset(0);
                make.height.mas_equalTo(1);
            }];
        }
            break;
        case SLBaseCellView2:
        {
            self.rightImageViw.hidden = YES;
            self.lineView.hidden = YES;
            self.leftImageView.hidden = YES;
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.contentView);
            }];
        }
            break;
        case SLBaseCellView3://练习专项提示
        {
            self.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            self.subTitleLabel.font = [UIFont systemFontOfSize:14];
            self.titleLabel.textColor = [UIColor blackColor];
            self.subTitleLabel.textColor = SLColor(@"#8E8E93");
            self.lineView.hidden = YES;
            self.rightImageViw.hidden = YES;
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.leftImageView.mas_right).offset(25);
                make.top.equalTo(self.leftImageView.mas_top);
            }];
            [self.subTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.titleLabel.mas_left);
                make.top.equalTo(self.titleLabel.mas_bottom).offset(2);
            }];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.backgroundColor = [SLBlueColor colorWithAlphaComponent:0.15];
            [btn setTitle:@"去练习" forState:UIControlStateNormal];
            [btn setTitleColor:SLBlueColor forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            [btn setImage:ImageName(@"icon_lx_blue_arrow") forState:UIControlStateNormal];
            btn.layer.cornerRadius = 18;
            [self.contentView addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.contentView);
                make.right.equalTo(self.contentView.mas_right).offset(-16);
                make.width.mas_equalTo(76);
                make.height.mas_equalTo(38);
            }];
            btn.tag = 88668;
            
        }
            break;
        case SLBaseCellView4://头像
        {
            self.leftImageView.hidden = NO;
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(12);
                make.centerY.equalTo(self.contentView);
            }];
            [self.leftImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self);
                make.right.equalTo(self.rightImageViw.mas_left).offset(-2);
                make.width.height.mas_equalTo(52);
            }];
            [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.titleLabel);
                make.right.equalTo(self.contentView);
                make.bottom.equalTo(self.contentView.mas_bottom).offset(0);
                make.height.mas_equalTo(1);
            }];
            self.leftImageView.layer.cornerRadius = 26;
        }
            break;
        default:
            break;
    }
}

- (void)drawRect:(CGRect)rect {
    UIButton *btn = [self viewWithTag:88668];
    if (btn) {
        [btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:4];
    }
}

@end
