//
//  GMShareTopView.m
//  Gengmei
//
//  Created by yefengming on 2018/12/19.
//  Copyright © 2018年 更美互动信息科技有限公司. All rights reserved.
//

#import "GMShareTopView.h"
#import <GMKit/GMLabel.h>
#import <GMKit/UIColor+GMTheme.h>
#import <GMFoundation/NSString+GM.h>
#import <GMKit/GMFont.h>

@interface GMShareTopView () {
    GMLabel *_leftLabel;
    GMLabel *_rightLabel;
}
@property (nonatomic, strong) GMLabel *nameLabel;
@property (nonatomic, strong) GMLabel *titleLabel;
@property (nonatomic, strong) GMLabel *subTitleLabel;
@end

@implementation GMShareTopView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.layer.cornerRadius = 11;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];
        self.nameLabel = [GMLabel labelWithTextColor:RGBCOLOR_HEX(0x333333) fontSize:16];
        self.nameLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.nameLabel];
        self.titleLabel = [GMLabel labelWithTextColor:UIColor.auxiliaryTextLight fontSize:13];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.titleLabel];
        self.subTitleLabel = [GMLabel labelWithTextColor:[UIColor colorWithHexString:@"FF7690"] fontSize:13];
        self.subTitleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.subTitleLabel];
        
        _leftLabel = [[GMLabel alloc] init];
        _leftLabel.backgroundColor = UIColor.separatorLine;
        [self addSubview:_leftLabel];
        _rightLabel = [[GMLabel alloc] init];
        _rightLabel.backgroundColor = UIColor.separatorLine;
        [self addSubview:_rightLabel];
    }
    return self;
}

- (void)setSubViewsConstraints {
    [self.nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(24);
        make.height.mas_equalTo(16);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
    }];
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(12);
        make.height.mas_equalTo(13);
        make.left.right.mas_equalTo(self.nameLabel);
    }];
    [self.subTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(7);
        make.height.mas_equalTo(13);
        make.bottom.mas_equalTo(-11);
        make.left.right.mas_equalTo(self.nameLabel);
    }];
}

- (void)setName:(NSString *)name title:(NSString *)title subTitle:(NSString *)subTitle {
    self.nameLabel.text = name;
    self.titleLabel.text = title;
    self.subTitleLabel.text = subTitle;
    [self setSubViewsConstraints];
    if (!title.isNonEmpty && !subTitle.isNonEmpty) {
        [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-11);
        }];
    } else if (title.isNonEmpty && !subTitle.isNonEmpty) {
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-11);
        }];
    } else if (!title.isNonEmpty && subTitle.isNonEmpty) {
        [self.subTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(11);
        }];
    }
}

- (void)onlyTitle:(NSString *)title {
    self.titleLabel.text = title;
    self.titleLabel.textColor = UIColor.auxiliaryTextDark;
    self.titleLabel.font = [UIFont gmFont:16];
    self.layer.cornerRadius = 0;
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(28);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(16);
        make.bottom.mas_equalTo(0);
    }];
    [_leftLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_titleLabel);
        make.right.mas_equalTo(_titleLabel.mas_left).offset(-8);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(15);
    }];
    [_rightLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_titleLabel);
        make.left.mas_equalTo(_titleLabel.mas_right).offset(8);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(15);
    }];

}
- (void)updateSubtitleConstraintsWithBottom:(CGFloat)bottom {
    [self.subTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(bottom);
    }];
}

@end
