//
//  SLInterestView.m
//  SLAiELTS
//
//  Created by Gusont on 2023/2/28.
//

#import "SLInterestView.h"

@implementation SLInterestView

static NSInteger SLInterestTag = 10888;

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

- (void)confignView {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = @"请选择你兴趣的标签";
    [self addSubview:titleLabel];
    UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    subTitleLabel.font = [UIFont systemFontOfSize:14];
    subTitleLabel.textColor = [UIColor colorFormString:@"#8E8E93"];
    subTitleLabel.alpha = 0.6f;
    subTitleLabel.tag = 666888;
    subTitleLabel.text = @"可为你匹配共同标签的好友";
    [self addSubview:subTitleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.equalTo(self).offset(26 * KScaleH);
    }];
    [subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.equalTo(titleLabel.mas_bottom).offset(6);
    }];
   
//    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    cancelBtn.backgroundColor = [UIColor colorWithRed:199 / 255.0 green:235 / 255.0 blue:253 / 255.0 alpha:1];
//    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
//    cancelBtn.layer.borderColor = [SLBlueColor CGColor];
//    cancelBtn.layer.borderWidth = 1.0f;
//    cancelBtn.layer.cornerRadius = 25.0f;
//    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:cancelBtn];
//    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self);
//        make.left.mas_equalTo(22);
//        make.height.mas_equalTo(50);
//        make.bottom.mas_equalTo(self.mas_bottom).offset(-20 * KScaleH);
//    }];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(subTitleLabel.mas_bottom).offset(10);
        make.left.right.equalTo(self);
        make.bottom.equalTo(self.mas_bottom).offset(-10);
    }];
    [self.scrollBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.height.equalTo(self.scrollView);
    }];
}

- (void)interestBtnClick:(UIButton *)btn {
    btn.selected = !btn.selected;
    btn.backgroundColor = btn.selected ? SLBlueColor : UIColor.whiteColor;
    btn.layer.borderWidth = btn.selected ? 0 : 1;
    InterestModel *model = [self.interestArr objectAtIndex:btn.tag - SLInterestTag];
    model.isSeleted = btn.selected;
}

- (void)cancelBtnClick {
    [self.interestArr enumerateObjectsUsingBlock:^(InterestModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.isSeleted = NO;
        UIButton *btn = [self viewWithTag:SLInterestTag + idx];
        btn.selected = NO;
        btn.backgroundColor = btn.selected ? SLBlueColor : UIColor.whiteColor;
    }];
}

- (void)confignIntersetBtn:(NSArray<InterestModel *> *)interestListModel {
    self.interestArr = [NSMutableArray arrayWithArray:interestListModel];
    
    [self.scrollBgView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.tag >= SLInterestTag && obj.tag < SLInterestTag + 100) {
            [obj removeFromSuperview];
        }
    }];
    __block CGFloat x = 20;
    __block CGFloat y = 20;
    __block UILabel *leftStationLab = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 0, 40)];
    [self addSubview:leftStationLab];
    __block UILabel *stationLab;
    stationLab = leftStationLab;
    [self.interestArr enumerateObjectsUsingBlock:^(InterestModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = SLInterestTag + idx;
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.layer.borderColor = [SLColor(@"#CFD8E3") CGColor];
        btn.layer.borderWidth = 1.0f;
        btn.layer.cornerRadius = 20.0f;
        [btn setTitle:obj.interestName forState:UIControlStateNormal];
        [btn setTitleColor:SLColor(@"#080F1B") forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        btn.backgroundColor = SLColor(@"#F5F8F7");
        CGSize size = [btn sizeThatFits:CGSizeMake(MAXFLOAT, 40)];
        CGFloat h = size.width + 24;
        CGFloat w = btn.frame.size.width + MAX(h, 30);
        if (x + w  > kSCREEN_WIDTH - 20) {
            UIButton *lastBtn = [self viewWithTag:10888 + idx - 1];
            if (lastBtn) {
                CGFloat moreX = kSCREEN_WIDTH - 20 - CGRectGetMaxX(lastBtn.frame);
                stationLab.frame = CGRectMake(20, y, moreX / 2.0, 40);
            }
            y += 52;
            x = 20;
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
            [self.scrollBgView addSubview:label];
            stationLab = label;
        }
        
        [btn addTarget:self action:@selector(interestBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = CGRectMake(x, y, w, 40);
        [self.scrollBgView addSubview:btn];
        if (idx == self.interestArr.count - 1) {
            CGFloat moreX = kSCREEN_WIDTH - 20 - CGRectGetMaxX(btn.frame);
            stationLab.frame = CGRectMake(20, y, moreX / 2.0, 40);
            self.scrollView.contentSize = CGSizeMake(kSCREEN_WIDTH, y + 60);
        }
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(stationLab.mas_right).offset(x - 20);
            make.top.equalTo(self.scrollBgView).offset(y);
            make.width.mas_equalTo(w);
            make.height.mas_equalTo(40);
        }];
        if (CGRectGetWidth(leftStationLab.frame) > 1) {
            
        }
        x += w + 16;
    }];
}

- (NSMutableArray<InterestModel *> *)interestArr {
    if (!_interestArr) {
        _interestArr = [NSMutableArray array];
    }
    return _interestArr;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
        _scrollView.scrollEnabled = YES;
        [self addSubview:_scrollView];
    }
    return _scrollView;
}

- (UIView *)scrollBgView {
    if (!_scrollBgView) {
        _scrollBgView = [[UIView alloc] initWithFrame:CGRectZero];
        [self.scrollView addSubview:_scrollBgView];
    }
    return _scrollBgView;
}

@end
