//
//  HWBookmarkHeader.m
//  双子星云手机
//
//  Created by 余衡武 on 2022/3/24.
//

#import "HWBookmarkHeader.h"

@interface HWBookmarkHeader ()

@property (nonatomic,strong) UILabel  *titleLabel;
@property (nonatomic,strong) UIButton *expandBtn;
@property (nonatomic,strong) UIView   *lineView;
@property (nonatomic,strong) UIButton *deleteBtn;

@end

@implementation HWBookmarkHeader

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self)
    {
        [self drawAnyView];
    }
    
    return self;
}

- (void)drawAnyView {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W - 30, 50)];
    header.backgroundColor = [UIColor hwColor:@"#1C1C1E"];
    header.layer.mask = [iTools getCornerRoundWithSelfView:header byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRect:CGRectMake(0, 0, SCREEN_W - 30, 50)];
    [self addSubview:header];
    
    UIButton *expandBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [expandBtn setImage:[UIImage imageNamed:@"Icon06"] forState:(UIControlStateNormal)];
    [expandBtn setImage:[UIImage imageNamed:@"Icon05"] forState:(UIControlStateSelected)];
    expandBtn.userInteractionEnabled = NO;
    [expandBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [header addSubview:expandBtn];
    self.expandBtn = expandBtn;
    [expandBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(35);
        make.centerY.mas_equalTo(header.mas_centerY);
    }];
    
    UILabel *titleLabel =  [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.numberOfLines = 1;
    titleLabel.textColor = HWFFFFFF80Color;
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [header addSubview:titleLabel];
    self.titleLabel = titleLabel;
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(35);
        make.right.mas_equalTo(20);
        make.centerY.mas_equalTo(header.mas_centerY);
    }];
    
    UIView *lineView =  [[UIView alloc] init];
    lineView.backgroundColor = HWFFFFFF08Color;
    [header addSubview:lineView];
    self.lineView = lineView;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(35);
        make.right.mas_equalTo(-35);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    UIButton *deleteBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [deleteBtn setImage:[UIImage imageNamed:@"shanchu_icon"] forState:(UIControlStateNormal)];
    [deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [deleteBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [header addSubview:deleteBtn];
    self.deleteBtn = deleteBtn;
    deleteBtn.hidden = YES;
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(35);
        make.centerY.mas_equalTo(header.mas_centerY);
    }];
}

- (void)setNormalModel {
    
    self.deleteBtn.hidden = YES;
    
    [self.expandBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
    }];

    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(35);
    }];
}

- (void)setDeleteModel {
    
    self.deleteBtn.hidden = NO;
    
    [self.expandBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
    }];

    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(60);
    }];
}

- (void)setFolderModel:(HWFolderModel *)folderModel {
    
    _folderModel = folderModel;
    
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerDidClick:)]];
    
    self.titleLabel.text = folderModel.name;
    self.expandBtn.selected = folderModel.isExpand;
    
    // 编辑模式
    if (folderModel.isEditing) {
        [self setDeleteModel];
    }else {
        [self setNormalModel];
    }
    
    if (folderModel.isExpand && folderModel.bookmarkArray.count != 0) {
        self.lineView.hidden = NO;
    }else {
        self.layer.mask = [iTools getCornerRoundWithSelfView:self byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRect:CGRectMake(0, 0, SCREEN_W - 30, 50)];
        self.lineView.hidden = YES;
    }
}

- (void)deleteBtnClick:(UIButton *)btn {
    HLog(@"删除");
    if ([_delegate respondsToSelector:@selector(bookmarkHeaderDidClickDeleteBtn:)]) {
        [_delegate bookmarkHeaderDidClickDeleteBtn:self.folderModel];
    }
}

- (void)headerDidClick:(UITapGestureRecognizer *)tap {
    
    self.expandBtn.selected = !self.expandBtn.selected;
    self.folderModel.isExpand = self.expandBtn.selected;
    
    if ([_delegate respondsToSelector:@selector(bookmarkHeaderDidClickExpandBtn:)]) {
        [_delegate bookmarkHeaderDidClickExpandBtn:self.folderModel];
    }
}

@end
