//
//  HomeServiceTableViewCell.m
//  Open
//
//  Created by 雷俊博 on 2021/2/26.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "HomeServiceTableViewCell.h"

@interface HomeServiceTableViewCell ()

@property (nonatomic, strong)UIView* titleView;

@property (nonatomic, strong)UIView* view1;

@property (nonatomic, strong)UIView* view2;

@property (nonatomic, strong)UIView* view3;


@property (nonatomic, strong)UIView* lb_line1;

@property (nonatomic, strong)UIView* lb_line2;

@property (nonatomic, strong)UIView* lb_line3;

@end

@implementation HomeServiceTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        //添加view
        [self.contentView addSubview:self.titleView];
        [self.titleView addSubview:self.lb_title];
        [self.contentView addSubview:self.view1];
        [self.contentView addSubview:self.view2];
        [self.contentView addSubview:self.view3];
        
        [self.view1 addSubview:self.imageView1];
        [self.view1 addSubview:self.imageView2];
        [self.view1 addSubview:self.lb_line1];
        
        [self.view2 addSubview:self.imageView3];
        [self.view2 addSubview:self.imageView4];
        [self.view2 addSubview:self.lb_line2];
        
        [self.view3 addSubview:self.imageView5];
        [self.view3 addSubview:self.imageView6];
        [self.view3 addSubview:self.lb_line3];
        
        
        [self setupUI];
    }
    return self;
}
- (void)setupUI{
    //布局
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(50 * WIDTHRADIU);
    }];
    
    [self.lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleView);
        make.left.offset(10 * WIDTHRADIU);
    }];
	// 为容器行添加约束，替代基于 frame 的布局
	[self.view1 mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.offset(10 * WIDTHRADIU);
		make.right.offset(-10 * WIDTHRADIU);
		make.top.equalTo(self.titleView.mas_bottom).offset(0);
		make.height.offset(90 * WIDTHRADIU);
	}];
	[self.view2 mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.offset(10 * WIDTHRADIU);
		make.right.offset(-10 * WIDTHRADIU);
		make.top.equalTo(self.view1.mas_bottom).offset(10 * WIDTHRADIU);
		make.height.offset(90 * WIDTHRADIU);
	}];
	[self.view3 mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.offset(10 * WIDTHRADIU);
		make.right.offset(-10 * WIDTHRADIU);
		make.top.equalTo(self.view2.mas_bottom).offset(10 * WIDTHRADIU);
		make.height.offset(90 * WIDTHRADIU);
		make.bottom.equalTo(self.contentView.mas_bottom).offset(-10 * WIDTHRADIU);
	}];
    //添加阴影
    [self addShadowToView:self.view1 withColor:[UIColor colorWithHex:0x6c6c6c]];
    [self addShadowToView:self.view2 withColor:[UIColor colorWithHex:0x6c6c6c]];
    [self addShadowToView:self.view3 withColor:[UIColor colorWithHex:0x6c6c6c]];
    
    [self.imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.width.offset((ScreenWidth - 20 * WIDTHRADIU)/2.0);
    }];
    [self.imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.right.offset(0);
        make.width.offset((ScreenWidth - 20 * WIDTHRADIU)/2.0);
    }];
    
    [self.lb_line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view1).offset(5);;
        make.width.offset(1);
        make.top.offset(10);
        make.bottom.offset(-10);
    }];
    
    [self.imageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.width.offset((ScreenWidth - 20 * WIDTHRADIU)/2.0);
    }];
    [self.imageView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.right.offset(0);
        make.width.offset((ScreenWidth - 20 * WIDTHRADIU)/2.0);
    }];
    
    [self.lb_line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view2).offset(5);
        make.width.offset(1);
        make.top.offset(10);
        make.bottom.offset(-10);
    }];
    
    [self.imageView5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.width.offset((ScreenWidth - 20 * WIDTHRADIU)/2.0);
    }];
    [self.imageView6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.right.offset(0);
        make.width.offset((ScreenWidth - 20 * WIDTHRADIU)/2.0);
    }];
    
    [self.lb_line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view3).offset(5);
        make.width.offset(1);
        make.top.offset(10);
        make.bottom.offset(-10);
    }];
    
    UITapGestureRecognizer * tag1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
    UITapGestureRecognizer * tag2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
    UITapGestureRecognizer * tag3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
    UITapGestureRecognizer * tag4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
    UITapGestureRecognizer * tag5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
    UITapGestureRecognizer * tag6 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
    [self.imageView1 addGestureRecognizer:tag1];
    [self.imageView2 addGestureRecognizer:tag2];
    [self.imageView3 addGestureRecognizer:tag3];
    [self.imageView4 addGestureRecognizer:tag4];
    [self.imageView5 addGestureRecognizer:tag5];
    [self.imageView6 addGestureRecognizer:tag6];

    
}


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

#pragma mark - event response
-(void)onClick:(UITapGestureRecognizer *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(cell:didSelectImage:)]) {
        [self.delegate cell:self didSelectImage:(UIImageView *)sender.view];
    }
}

#pragma mark - 添加阴影
/// 添加四边阴影效果
- (void)addShadowToView:(UIView *)theView withColor:(UIColor *)theColor {
    // 阴影颜色
    theView.layer.shadowColor = theColor.CGColor;
    // 阴影偏移，默认(0, -3)
    theView.layer.shadowOffset = CGSizeMake(0,0);
    // 阴影透明度，默认0
    theView.layer.shadowOpacity = 0.25;
    // 阴影半径，默认3
    theView.layer.shadowRadius = 3;
}


#pragma mark - getters and setters
- (UIView *)titleView {
    
    if (!_titleView) {
        _titleView = [[UIView alloc]init];
    }
    return _titleView;
}

- (UILabel *)lb_title {
    
    if (!_lb_title) {
        _lb_title = [[UILabel alloc]init];
        _lb_title.font = [UIFont boldSystemFontOfSize:20 * WIDTHRADIU];
        _lb_title.textColor = [UIColor colorWithHex:0x4d4d4d];
    }
    return _lb_title;
}

- (UIView *)view1 {
    if (!_view1) {
        _view1 = [[UIView alloc]init];
        _view1.backgroundColor = [UIColor whiteColor];
        _view1.layer.cornerRadius = 5;
    }
    return _view1;
}

- (UIView *)view2 {
    if (!_view2) {
        _view2 = [[UIView alloc]init];
        _view2.backgroundColor = [UIColor whiteColor];
        _view2.layer.cornerRadius = 5;
    }
    return _view2;
}

- (UIView *)view3 {
    if (!_view3) {
        _view3 = [[UIView alloc]init];
        _view3.backgroundColor = [UIColor whiteColor];
        _view3.layer.cornerRadius = 5;
    }
    return _view3;
}

- (void)setShowThirdView:(BOOL)showThirdView {
    _showThirdView = showThirdView;
    self.view3.hidden = !showThirdView;

	if (!showThirdView) {
		// 隐藏第三行：让第二行贴底，保持高度 90
		[self.view2 mas_remakeConstraints:^(MASConstraintMaker *make) {
			make.left.offset(10 * WIDTHRADIU);
			make.right.offset(-10 * WIDTHRADIU);
			make.top.equalTo(self.view1.mas_bottom).offset(10 * WIDTHRADIU);
			make.height.offset(90 * WIDTHRADIU);
			make.bottom.equalTo(self.contentView.mas_bottom).offset(-10 * WIDTHRADIU);
		}];
		// 将第三行高度置为 0，移除与底部的约束，避免冲突
		[self.view3 mas_remakeConstraints:^(MASConstraintMaker *make) {
			make.left.right.equalTo(self.view2);
			make.top.equalTo(self.view2.mas_bottom).offset(0);
			make.height.offset(0);
		}];
	} else {
		// 显示第三行：恢复第二行位置，并让第三行贴底
		[self.view2 mas_remakeConstraints:^(MASConstraintMaker *make) {
			make.left.offset(10 * WIDTHRADIU);
			make.right.offset(-10 * WIDTHRADIU);
			make.top.equalTo(self.view1.mas_bottom).offset(10 * WIDTHRADIU);
			make.height.offset(90 * WIDTHRADIU);
		}];
		[self.view3 mas_remakeConstraints:^(MASConstraintMaker *make) {
			make.left.offset(10 * WIDTHRADIU);
			make.right.offset(-10 * WIDTHRADIU);
			make.top.equalTo(self.view2.mas_bottom).offset(10 * WIDTHRADIU);
			make.height.offset(90 * WIDTHRADIU);
			make.bottom.equalTo(self.contentView.mas_bottom).offset(-10 * WIDTHRADIU);
		}];
	}
}

- (void)setShowOnlyFirstRow:(BOOL)showOnlyFirstRow {
    _showOnlyFirstRow = showOnlyFirstRow;
    
    if (showOnlyFirstRow) {
        // 只显示第一行：隐藏第二行和第三行
        self.view2.hidden = YES;
        self.view3.hidden = YES;
        
        // 让第一行贴底，减少高度，并添加底部边距
        [self.view1 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(10 * WIDTHRADIU);
            make.right.offset(-10 * WIDTHRADIU);
            make.top.equalTo(self.titleView.mas_bottom).offset(0);
            make.height.offset(90 * WIDTHRADIU); // 恢复原来的高度90
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-80 * WIDTHRADIU); // 增加更多底部边距，给TabBar留出更多空间
        }];
        
        // 将第二行和第三行高度置为 0
        [self.view2 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view1);
            make.top.equalTo(self.view1.mas_bottom).offset(0);
            make.height.offset(0);
        }];
        [self.view3 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view1);
            make.top.equalTo(self.view2.mas_bottom).offset(0);
            make.height.offset(0);
        }];
    } else {
        // 显示所有行：恢复原来的布局
        self.view2.hidden = NO;
        self.view3.hidden = !self.showThirdView;
        
        [self.view1 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(10 * WIDTHRADIU);
            make.right.offset(-10 * WIDTHRADIU);
            make.top.equalTo(self.titleView.mas_bottom).offset(0);
            make.height.offset(90 * WIDTHRADIU);
        }];
        
        if (self.showThirdView) {
            [self.view2 mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(10 * WIDTHRADIU);
                make.right.offset(-10 * WIDTHRADIU);
                make.top.equalTo(self.view1.mas_bottom).offset(10 * WIDTHRADIU);
                make.height.offset(90 * WIDTHRADIU);
            }];
            [self.view3 mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(10 * WIDTHRADIU);
                make.right.offset(-10 * WIDTHRADIU);
                make.top.equalTo(self.view2.mas_bottom).offset(10 * WIDTHRADIU);
                make.height.offset(90 * WIDTHRADIU);
                make.bottom.equalTo(self.contentView.mas_bottom).offset(-10 * WIDTHRADIU);
            }];
        } else {
            [self.view2 mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(10 * WIDTHRADIU);
                make.right.offset(-10 * WIDTHRADIU);
                make.top.equalTo(self.view1.mas_bottom).offset(10 * WIDTHRADIU);
                make.height.offset(90 * WIDTHRADIU);
                make.bottom.equalTo(self.contentView.mas_bottom).offset(-10 * WIDTHRADIU);
            }];
            [self.view3 mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self.view2);
                make.top.equalTo(self.view2.mas_bottom).offset(0);
                make.height.offset(0);
            }];
        }
    }
}


- (UIImageView *)imageView1
{
    if (!_imageView1) {
        _imageView1 = [[UIImageView alloc]init];
        _imageView1.userInteractionEnabled = YES;
    }
    return _imageView1;
}

- (UIImageView *)imageView2
{
    if (!_imageView2) {
        _imageView2 = [[UIImageView alloc]init];
        _imageView2.userInteractionEnabled = YES;
    }
    return _imageView2;
}

- (UIImageView *)imageView3
{
    if (!_imageView3) {
        _imageView3 = [[UIImageView alloc]init];
        _imageView3.userInteractionEnabled = YES;
    }
    return _imageView3;
}

- (UIImageView *)imageView4
{
    if (!_imageView4) {
        _imageView4 = [[UIImageView alloc]init];
        _imageView4.userInteractionEnabled = YES;
    }
    return _imageView4;
}

- (UIImageView *)imageView5
{
    if (!_imageView5) {
        _imageView5 = [[UIImageView alloc]init];
        _imageView5.userInteractionEnabled = YES;
    }
    return _imageView5;
}

- (UIImageView *)imageView6
{
    if (!_imageView6) {
        _imageView6 = [[UIImageView alloc]init];
        _imageView6.userInteractionEnabled = YES;
    }
    return _imageView6;
}

- (UIView *)lb_line1
{
    if (!_lb_line1) {
        _lb_line1 = [[UIView alloc]init];
        _lb_line1.backgroundColor = [UIColor colorWithHex:0xd9d9d9];
    }
    return _lb_line1;
}

- (UIView *)lb_line2
{
    if (!_lb_line2) {
        _lb_line2 = [[UIView alloc]init];
        _lb_line2.backgroundColor = [UIColor colorWithHex:0xd9d9d9];
    }
    return _lb_line2;
}

- (UIView *)lb_line3
{
    if (!_lb_line3) {
        _lb_line3 = [[UIView alloc]init];
        _lb_line3.backgroundColor = [UIColor colorWithHex:0xd9d9d9];
    }
    return _lb_line3;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
