//
//  SLHorScrBtnsCell.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/13.
//

#import "SLHorScrBtnsCell.h"
#import "SLBaseCellView.h"
#import "SLNestViewController.h"

@implementation SLHorScrBtnsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)confignCellWithModel:(SLPageModel *)pageModel {
    self.horScrBtnsView.titleLabel.text = pageModel.itemTitel;
    self.horScrBtnsView.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    if ([pageModel.itemTitel isEqualToString:@"专项提升"]) {
        [self.horScrBtnsView.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj != self.horScrBtnsView.titleLabel) {
                [obj removeFromSuperview];
                obj = nil;
            }
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            CGFloat leftSpace = 16.0 * KScaleW;
            CGFloat width = self.horScrBtnsView.bounds.size.width - leftSpace * 2;
            CGFloat itemWidth = (width + 10) / 2 * 1.0;
            CGFloat height = 70.0 * KScaleW;
            NSArray *titleColors = @[SLColor(@"#374C92"), SLColor(@"#833D23"), SLColor(@"#22617C"), SLColor(@"#356697"), SLColor(@"#1B5F1F")];
            NSArray *subTitleColors = @[SLColor(@"#5668A0"), SLColor(@"#A15D43"), SLColor(@"#3D809D"), SLColor(@"#4E7CAA"), SLColor(@"#2A702D")];
            [pageModel.modelDetails enumerateObjectsUsingBlock:^(SLPageModelDetail * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setBackgroundImage:ImageName(obj.bgImage) forState:UIControlStateNormal];
                WS(weakSelf);
                [btn addTapWithBlock:^{
                    NSLog(@"-------");
                    SLNestViewController *vc = [[SLNestViewController alloc] init];
                    [weakSelf.viewController navPushViewController:vc animated:YES];
                }];
                if (idx == 0) {
                    btn.frame = CGRectMake(leftSpace, 49 * KScaleW, width, height);
                }else {
                    NSInteger row = (fmod(idx,2) == 0) ? 1 : 0;
                    NSInteger scr = (idx + 1) / 2;
                    btn.frame = CGRectMake(leftSpace + row * itemWidth, scr * (height + 10) + 49 * KScaleW, itemWidth - 10, height);
                }
                [self.horScrBtnsView addSubview:btn];
                
                UILabel *lable = [[UILabel alloc] initWithFrame:CGRectZero];
                lable.text = obj.title;
                
                UIColor *titleColor = idx >= titleColors.count ? [UIColor whiteColor] : [titleColors objectAtIndex:idx];
                lable.textColor = titleColor;
                lable.font = [UIFont systemFontOfSize:16];
                [lable sizeToFit];
                [btn addSubview:lable];
                [lable mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(16 * KScaleW);
                    make.top.mas_equalTo(10 * KScaleW);
                    make.height.mas_equalTo(24 * KScaleW);
                }];
                
                UILabel *subLable = [[UILabel alloc] initWithFrame:CGRectZero];
                subLable.text = obj.subTitle;
                UIColor *subTitleColor = idx >= subTitleColors.count ? [UIColor whiteColor] : [subTitleColors objectAtIndex:idx];
                subLable.textColor = subTitleColor;
                subLable.font = [UIFont systemFontOfSize:12];
                [subLable sizeToFit];
                [btn addSubview:subLable];
                [subLable mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(16 * KScaleW);
                    make.top.equalTo(lable.mas_bottom).offset(2);
                }];
            }];
        });
    }else if ([pageModel.itemTitel isEqualToString:@"专项提升  "]) {
        [self.horScrBtnsView.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (![obj isEqual:self.horScrBtnsView.titleLabel]) {
                [obj removeFromSuperview];
                obj = nil;
            }
        }];
        [pageModel.modelDetails enumerateObjectsUsingBlock:^(SLPageModelDetail * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            SLBaseCellView *baseView = [[SLBaseCellView alloc] initWithFrame:CGRectZero];
            baseView.leftImageView.image = ImageName(obj.bgImage);
            baseView.titleLabel.text = obj.title;
            baseView.subTitleLabel.text = obj.subTitle;
            [self.horScrBtnsView addSubview:baseView];
            [baseView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self.horScrBtnsView);
                make.height.mas_equalTo(80 * KScaleW);
                make.top.equalTo(self.horScrBtnsView.mas_top).offset((45 + 80 * idx) * KScaleW);
            }];
            [baseView updateConstraintsWith:SLBaseCellView3];
        }];
    }else if ([pageModel.itemTitel isEqualToString:@"剑雅真题"]) {
        [self.horScrBtnsView.btnsScrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj removeFromSuperview];
            obj = nil;
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            CGFloat itemWidth = 120.f;
            CGFloat height = 130.f;
            [self.horScrBtnsView.btnsScrollView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(height);
            }];
            [self.horScrBtnsView.btnsScrollView setContentSize:CGSizeMake(132 * pageModel.modelDetails.count, height)];
            self.horScrBtnsView.btnsScrollView.backgroundColor = [UIColor clearColor];
            
            [pageModel.modelDetails enumerateObjectsUsingBlock:^(SLPageModelDetail * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setBackgroundImage:ImageName(obj.bgImage) forState:UIControlStateNormal];
                btn.frame = CGRectMake((itemWidth + 12) * idx, 0, itemWidth, height);
                [self.horScrBtnsView.btnsScrollView addSubview:btn];
                
                UILabel *lable = [[UILabel alloc] initWithFrame:CGRectZero];
                lable.text = obj.title;
                lable.textColor = [UIColor blackColor];
                lable.font = [UIFont boldSystemFontOfSize:16];
                [lable sizeToFit];
                lable.textAlignment = NSTextAlignmentCenter;
                [btn addSubview:lable];
                [lable mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(btn);
                    make.top.equalTo(btn.mas_top).offset(67);
                    make.height.mas_equalTo(28);
                    make.left.equalTo(btn);
                }];
                
                UILabel *subLable = [[UILabel alloc] initWithFrame:CGRectZero];
                subLable.text = obj.subTitle;
                subLable.textColor = [UIColor colorFormString:@"#8E8E93"];
                subLable.font = [UIFont systemFontOfSize:13];
                [subLable sizeToFit];
                subLable.textAlignment = NSTextAlignmentCenter;
                [btn addSubview:subLable];
                [subLable mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(btn);
                    make.top.equalTo(lable.mas_bottom).offset(2);
                    make.left.equalTo(btn);
                }];
            }];
        });
    }else {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.horScrBtnsView.btnsScrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [obj removeFromSuperview];
                obj = nil;
            }];
            CGFloat width = self.horScrBtnsView.btnsScrollView.bounds.size.width;
            CGFloat height = self.horScrBtnsView.btnsScrollView.bounds.size.height;
            [self.horScrBtnsView.btnsScrollView setContentSize:CGSizeMake(width, height)];
            self.horScrBtnsView.btnsScrollView.backgroundColor = [UIColor clearColor];
            CGFloat itemWidth = (width + 7) / pageModel.modelDetails.count * 1.0;
            [pageModel.modelDetails enumerateObjectsUsingBlock:^(SLPageModelDetail * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setBackgroundImage:ImageName(obj.bgImage) forState:UIControlStateNormal];
                btn.frame = CGRectMake(itemWidth * idx, 0, itemWidth - 7, height);
                [self.horScrBtnsView.btnsScrollView addSubview:btn];
                
                UILabel *lable = [[UILabel alloc] initWithFrame:CGRectZero];
                lable.text = obj.title;
                lable.textColor = [UIColor whiteColor];
                lable.font = [UIFont systemFontOfSize:13];
                [lable sizeToFit];
                lable.textAlignment = NSTextAlignmentCenter;
                [btn addSubview:lable];
                [lable mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(btn);
                    make.bottom.equalTo(btn.mas_bottom).offset(-2);
                    make.height.mas_equalTo(26);
                    make.left.equalTo(btn);
                }];
            }];
        });
    }
}

@end
