

#import "SLMeHeaderCollectionViewCell.h"
#import "SLUserCenterViewController.h"
#import "SLLoginVCViewController.h"

@interface SLMeHeaderCollectionViewCell()

@property (nonatomic, strong) UIImageView *bgImageView;

@property (nonatomic, strong) UIImageView *headView;//头像

@property (nonatomic, strong) UILabel *nameLabel;//名称

@property (nonatomic, strong) UILabel *loginTimeLabel;//登录时间

@property (nonatomic, strong) UIButton *setUpBtn;//设置

@property (nonatomic, strong) UIScrollView *interestcrollView;
@property (nonatomic, strong) UIButton *loginBtn;//登录

@end

static const NSInteger btnTag = 888888;

@implementation SLMeHeaderCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self confignUI];
    }
    return self;
}

- (void)headViewClick
{
   
}

- (void)btnClick:(UIButton *)btn
{
    [self.viewController navPushViewController:[SLLoginVCViewController loadViewControllewWithNib] animated:YES];
}

- (void)setBtnClick:(UIButton *)btn
{
    SLLoginInfo *targetModel = [SLGlobalInfo SharedInstance].loginInfo;
    if (targetModel) {
        [self.viewController navPushViewController:[SLUserCenterViewController loadViewControllewWithNib] animated:YES];
    }else {
        [self.viewController navPushViewController:[SLLoginVCViewController loadViewControllewWithNib] animated:YES];
    }
}

- (void)drawRect:(CGRect)rect
{
    
}

- (void)confignUI
{
    self.bgImageView.backgroundColor = [UIColor whiteColor];
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(16));
        make.width.height.equalTo(@(52 * KScaleW));
        make.bottom.equalTo(self.bgImageView.mas_bottom).offset(-60);
    }];
    //
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView);
        make.left.equalTo(self.headView.mas_right).offset(10 * KScaleW);
    }];
    
    [self.loginTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(7);
        make.left.equalTo(self.nameLabel.mas_left);
    }];
    

    [self.setUpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.headView);
        make.right.equalTo(self.bgImageView.mas_right).offset(-20);
    }];
    
    
    WS(weakSelf);
    [self.KVOController observe:[SLGlobalInfo SharedInstance] keyPath:@"loginInfo" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
        [weakSelf setHeaderWithLoginInfo];
    }];
    [self setHeaderWithLoginInfo];
    
    _interestcrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    _interestcrollView.bounces = NO;
    _interestcrollView.showsVerticalScrollIndicator = NO;
    _interestcrollView.showsHorizontalScrollIndicator = NO;
    [self.bgImageView addSubview:_interestcrollView];
    
    NSMutableArray *marr = [NSMutableArray arrayWithArray:@[@"运动",@"运动",@"运动",@"运动"]];
    [marr enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.text = obj;
        label.frame = CGRectMake(60 * idx, 0, 48, 26);
        label.textColor = SLColor(@"#3A3A3C");
        label.font = [UIFont systemFontOfSize:10];
        label.backgroundColor = SLColor(@"#F2F2F7");
        label.layer.cornerRadius = 13.0;
        label.textAlignment = NSTextAlignmentCenter;
        label.clipsToBounds = YES;
        [_interestcrollView addSubview:label];
    }];
    [_interestcrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.equalTo(self.bgImageView.mas_right).offset(-50);
        make.height.mas_equalTo(26);
        make.bottom.equalTo(self.bgImageView.mas_bottom).offset(-16);
    }];
    _interestcrollView.contentSize = CGSizeMake(marr.count * 60, 26);
    
    UIButton *moreInterestBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreInterestBtn setImage:ImageName(@"icon_my_heard_right_arrow") forState:UIControlStateNormal];
    [moreInterestBtn handleEvent:UIControlEventTouchUpInside withBlock:^{
            
    }];
    [self.bgImageView addSubview:moreInterestBtn];
    [moreInterestBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgImageView.mas_right).offset(-16);
        make.height.width.mas_equalTo(32);
        make.centerY.equalTo(_interestcrollView);
    }];
}

- (void)setHeaderWithLoginInfo {
    SLLoginInfo *targetModel = [SLGlobalInfo SharedInstance].loginInfo;
    if (targetModel) {
        self.nameLabel.text = [NSString stringWithFormat:@"%@",targetModel.user.userName];
        self.loginTimeLabel.text = [NSString stringWithFormat:@"账号：%@",targetModel.user.phone];
        NSString *headUrl = [NSString stringWithFormat:@"%@%@",[SLHttpCenter SharedInstance].serverUrl, [SLGlobalInfo SharedInstance].loginInfo.user.userHead];
        [self.headView sd_setImageWithURL:[NSURL URLWithString:headUrl] placeholderImage:ImageName(@"icon_ellipse")];
    }else {
        self.nameLabel.text = @"尊敬的客户，您好！";
        self.loginTimeLabel.text = @"未登录";
        self.headView .image = ImageName(@"icon_ellipse");
    }
}

- (void)confignCellWithModel:(SLPageModel *)pageModel {
   
}

- (UIImageView *)bgImageView
{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
//        _bgImageView.image = [UIImage imageNamed:@"icon_me_bg"];
        _bgImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:_bgImageView];
    }
    return _bgImageView;
}

- (UIImageView *)headView
{
    if (!_headView) {
        _headView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _headView.image = [UIImage imageNamed:@"icon_ellipse"];
        _headView.userInteractionEnabled = YES;
        CGFloat radius = 26;
        _headView.layer.cornerRadius = radius * KScaleW;
        _headView.clipsToBounds = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headViewClick)];
        [_headView addGestureRecognizer:tap];
        [self.bgImageView addSubview:_headView];
    }
    return _headView;
}

- (UILabel *)loginTimeLabel
{
    if (!_loginTimeLabel) {
        _loginTimeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _loginTimeLabel.font = [UIFont systemFontOfSize:16];
        _loginTimeLabel.textColor = SLColor(@"#8E8E93");
        _loginTimeLabel.text = @"未登录";
        [self.bgImageView addSubview:_loginTimeLabel];
    }
    return _loginTimeLabel;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _nameLabel.font = [UIFont boldSystemFontOfSize:20];
        _nameLabel.textColor = SLColor(@"#080F1B");
        _nameLabel.text = @"尊敬的客户，您好！";
        [self.bgImageView addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UIButton *)setUpBtn
{
    if (!_setUpBtn) {
        _setUpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_setUpBtn addTarget:self action:@selector(setBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _setUpBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _setUpBtn.backgroundColor = UIColor.clearColor;
        _setUpBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_setUpBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_setUpBtn setImage:[UIImage imageNamed:@"icon_chevron_right"] forState:UIControlStateNormal];
        [self.bgImageView addSubview:_setUpBtn];
    }
    return _setUpBtn;
}

@end
