//
//  SDTimeLineTableHeaderView.m
//  GSD_WeiXin(wechat)
//
//  Created by gsd on 16/2/25.
//  Copyright © 2016年 GSD. All rights reserved.
//

/*
 
 *********************************************************************************
 *
 * GSD_WeiXin
 *
 * QQ交流群: 362419100(2群) 459274049（1群已满）
 * Email : gsdios@126.com
 * GitHub: https://github.com/gsdios/GSD_WeiXin
 * 新浪微博:GSD_iOS
 *
 * 此“高仿微信”用到了很高效方便的自动布局库SDAutoLayout（一行代码搞定自动布局）
 * SDAutoLayout地址：https://github.com/gsdios/SDAutoLayout
 * SDAutoLayout视频教程：http://www.letv.com/ptv/vplay/24038772.html
 * SDAutoLayout用法示例：https://github.com/gsdios/SDAutoLayout/blob/master/README.md
 *
 *********************************************************************************
 
 */

#import "SDTimeLineTableHeaderView.h"
#import "UIView+SDAutoLayout.h"

@implementation SDTimeLineTableHeaderView

{
    UIImageView *_backgroundImageView;
    UIImageView *_iconView;
    UILabel *_nameLabel;
    UIScrollView *_interestcrollView;
}

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

- (void)setHeaderWithLoginInfo {
    SLUserModel *model = [SLGlobalInfo SharedInstance].loginInfo.user;
    NSString *urlStr = [model getUserHeadUrl];
    [_iconView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:ImageName(@"icon_ellipse")];
    _nameLabel.text = model.userName;
}

- (void)setup
{
    _backgroundImageView = [UIImageView new];
    _backgroundImageView.image = ImageName(@"icon_moment_bg_01");
    _backgroundImageView.userInteractionEnabled = YES;
//    _backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
//    _backgroundImageView.clipsToBounds = YES;
    [self addSubview:_backgroundImageView];
    
    _iconView = [UIImageView new];
//    NSString *urlStr = [[SLGlobalInfo SharedInstance].loginInfo.user getUserHeadUrl];
//    [_iconView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:ImageName(@"icon_ellipse")];
    [self addSubview:_iconView];
//    
//    WS(weakSelf);
//    [self.KVOController observe:[SLGlobalInfo SharedInstance] keyPath:@"loginInfo" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
//        [weakSelf setHeaderWithLoginInfo];
//    }];
    
    _nameLabel = [UILabel new];
    SLLoginInfo *targetModel = [SLGlobalInfo SharedInstance].loginInfo;
    _nameLabel.text = targetModel.user.userName;
    _nameLabel.textColor = [UIColor whiteColor];
    _nameLabel.textAlignment = NSTextAlignmentRight;
    _nameLabel.font = [UIFont boldSystemFontOfSize:15];
    [self addSubview:_nameLabel];
    
    
    _interestcrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    _interestcrollView.bounces = NO;
    _interestcrollView.showsVerticalScrollIndicator = NO;
    _interestcrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_interestcrollView];
    
    NSMutableArray *marr = [NSMutableArray arrayWithArray:@[@"运动",@"运动",@"运动",@"运动"]];
    [marr removeAllObjects];
    [marr enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.text = obj;
        label.frame = CGRectMake(60 * idx, 0, 48, 26);
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:10];
        label.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.3];
        label.layer.cornerRadius = 13.0;
        label.textAlignment = NSTextAlignmentCenter;
        label.clipsToBounds = YES;
        [_interestcrollView addSubview:label];
    }];
    
    UIButton *moreInterestBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreInterestBtn setImage:ImageName(@"icon_moents_right_arrow") forState:UIControlStateNormal];
    [moreInterestBtn handleEvent:UIControlEventTouchUpInside withBlock:^{
            
    }];
    [self addSubview:moreInterestBtn];

    _backgroundImageView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 30, 0));
    UIView *topView = [[UIView alloc] init];
    topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:topView];
    topView.sd_layout
    .heightIs(54)
    .leftSpaceToView(self, 0)
    .rightSpaceToView(self, 0)
    .topSpaceToView(self, -54);
//
    _iconView.sd_layout
    .widthIs(64)
    .heightIs(64)
    .leftSpaceToView(self, 16)
    .bottomSpaceToView(self, 84);
    _iconView.layer.cornerRadius = 32.0f;
    _iconView.clipsToBounds = YES;
    _iconView.contentMode = UIViewContentModeScaleAspectFill;


    _nameLabel.tag = 1000;
    [_nameLabel setSingleLineAutoResizeWithMaxWidth:200];
    _nameLabel.sd_layout
    .leftSpaceToView(_iconView, 9)
    .centerYEqualToView(_iconView)
    .heightIs(24);

    _interestcrollView.sd_layout
    .heightIs(26)
    .leftSpaceToView(self, 16)
    .rightSpaceToView(self, 50)
    .bottomSpaceToView(self, 54);


    moreInterestBtn.sd_layout
    .widthIs(26)
    .heightIs(26)
    .rightSpaceToView(self, 12)
    .centerYEqualToView(_interestcrollView);
}

- (void)confignHeaderWith:(SLUserModel *)userModel {
    NSString *urlStr = [userModel getUserHeadUrl];
    [_iconView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:ImageName(@"icon_ellipse")];
    _nameLabel.text = userModel.userName;
}

@end
