//
//  HomeHeaderView.m
//  redstar
//
//  Created by admin on 15/10/26.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "HomeHeaderView.h"
#define ImageCount 5

@interface HomeHeaderView () <UIScrollViewDelegate>
@property (nonatomic, strong) UIView *bgView; // 背景View
@end

@implementation HomeHeaderView

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kMineBackGroundColor;
        [self setup];
    }
    return self;
}


#pragma mark - Private Methods
- (void)setup
{
    [self.customPageControl setNumberOfPages:2];
    self.bgView.backgroundColor = [UIColor whiteColor];

}


#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = _scrollView.bounds.size.width;
    float fractionalPage = _scrollView.contentOffset.x / pageWidth;
    NSInteger nearestNumber = lround(fractionalPage);
    
    if (_customPageControl.currentPage != nearestNumber) {
        _customPageControl.currentPage = nearestNumber;
        // 当scrollView在滑动
        if (_scrollView.dragging) {
            // 更新选中的小点点的位置
            [_customPageControl updateCurrentPageDisplay];
        }
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [_customPageControl updateCurrentPageDisplay] ;
}



//#pragma mark - lazy loading
//- (UIImageView *)bgImageView
//{
//    if (!_bgImageView) {
//        _bgImageView = [[UIImageView alloc] init];
//        _bgImageView.translatesAutoresizingMaskIntoConstraints = NO;
//        _bgImageView.userInteractionEnabled = YES;
//        [self addSubview:_bgImageView];
//        
//        NSLayoutConstraint *bgImageTop = [NSLayoutConstraint constraintWithItem:_bgImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
//        [self addConstraint:bgImageTop];
//        
//        NSLayoutConstraint *bgImageLeft = [NSLayoutConstraint constraintWithItem:_bgImageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
//        [self addConstraint:bgImageLeft];
//        
//        NSLayoutConstraint *bgImageHeight = [NSLayoutConstraint constraintWithItem:_bgImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:95];
//        [self addConstraint:bgImageHeight];
//        
//        NSLayoutConstraint *bgImageRight = [NSLayoutConstraint constraintWithItem:_bgImageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
//        [self addConstraint:bgImageRight];
//    }
//    return _bgImageView;
//}
//
//- (UITextField *)searchTextField
//{
//    if (!_searchTextField) {
//        _searchTextField = [[UITextField alloc] init];
//        _searchTextField.translatesAutoresizingMaskIntoConstraints = NO;
//        _searchTextField.font = [UIFont systemFontOfSize:13.0];
//        _searchTextField.textAlignment = NSTextAlignmentCenter;
//        _searchTextField.layer.cornerRadius = 4.0;
//        _searchTextField.textColor = [UIColor whiteColor];
//        _searchTextField.layer.borderColor = [[UIColor whiteColor] CGColor];
//        _searchTextField.layer.borderWidth = 0.5;
//        _searchTextField.placeholder = @"查询";
//        [_searchTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
//        UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search"]];
//        imageV.contentMode = UIViewContentModeCenter;
//        imageV.width += 10;
//        _searchTextField.rightView = imageV;
//        _searchTextField.rightViewMode = UITextFieldViewModeAlways;
//        [self.bgImageView addSubview:_searchTextField];
//        
//        NSLayoutConstraint *bgImageTop = [NSLayoutConstraint constraintWithItem:_searchTextField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.bgImageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:35];
//        [self.bgImageView addConstraint:bgImageTop];
//        
//        NSLayoutConstraint *bgImageRight = [NSLayoutConstraint constraintWithItem:_searchTextField attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.bgImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
//        [self.bgImageView addConstraint:bgImageRight];
//        
//        NSLayoutConstraint *bgImageHeight = [NSLayoutConstraint constraintWithItem:_searchTextField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:28];
//        [self.bgImageView addConstraint:bgImageHeight];
//        
//        NSLayoutConstraint *bgImageWidth = [NSLayoutConstraint constraintWithItem:_searchTextField attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100];
//        [self.bgImageView addConstraint:bgImageWidth];
//    }
//    return _searchTextField;
//}
//
//- (UILabel *)nameLabel
//{
//    if (!_nameLabel) {
//        _nameLabel = [[UILabel alloc] init];
//        _nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
//        _nameLabel.font = [UIFont systemFontOfSize:13.0];
//        _nameLabel.textAlignment = NSTextAlignmentRight;
//        _nameLabel.textColor = [UIColor whiteColor];
//        [self.bgImageView addSubview:_nameLabel];
//        
//        NSLayoutConstraint *bgImageTop = [NSLayoutConstraint constraintWithItem:_nameLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.searchTextField attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3];
//        [self.bgImageView addConstraint:bgImageTop];
//        
//        NSLayoutConstraint *bgImageRight = [NSLayoutConstraint constraintWithItem:_nameLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.bgImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
//        [self.bgImageView addConstraint:bgImageRight];
//        
//        NSLayoutConstraint *bgImageBottom = [NSLayoutConstraint constraintWithItem:_nameLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bgImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
//        [self.bgImageView addConstraint:bgImageBottom];
//        
//        NSLayoutConstraint *bgImageWidth = [NSLayoutConstraint constraintWithItem:_nameLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:95];
//        [self.bgImageView addConstraint:bgImageWidth];
//    }
//    return _nameLabel;
//}
//
//- (UILabel *)shopnameLabel
//{
//    if (!_shopnameLabel) {
//        _shopnameLabel = [[UILabel alloc] init];
//        _shopnameLabel.translatesAutoresizingMaskIntoConstraints = NO;
//        _shopnameLabel.font = [UIFont systemFontOfSize:13.0];
//        _shopnameLabel.textAlignment = NSTextAlignmentRight;
//        _shopnameLabel.textColor = [UIColor whiteColor];
//        [self.bgImageView addSubview:_shopnameLabel];
//        
//        NSLayoutConstraint *bgImageTop = [NSLayoutConstraint constraintWithItem:_shopnameLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.searchTextField attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3];
//        [self.bgImageView addConstraint:bgImageTop];
//        
//        NSLayoutConstraint *bgImageRight = [NSLayoutConstraint constraintWithItem:_shopnameLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.nameLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-10];
//        [self.bgImageView addConstraint:bgImageRight];
//        
//        NSLayoutConstraint *bgImageBottom = [NSLayoutConstraint constraintWithItem:_shopnameLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bgImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
//        [self.bgImageView addConstraint:bgImageBottom];
//        
//        NSLayoutConstraint *bgImageLeft = [NSLayoutConstraint constraintWithItem:_shopnameLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.bgImageView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
//        [self.bgImageView addConstraint:bgImageLeft];
//    }
//    return _shopnameLabel;
//}

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.translatesAutoresizingMaskIntoConstraints = NO;
        _bgView.layer.borderWidth = 0.5;
        _bgView.layer.borderColor = kSeparateLineCGColor;
        [self addSubview:_bgView] ;
        
        NSLayoutConstraint *bgImageTop = [NSLayoutConstraint constraintWithItem:_bgView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        [self addConstraint:bgImageTop];
        
        NSLayoutConstraint *bgImageRight = [NSLayoutConstraint constraintWithItem:_bgView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [self addConstraint:bgImageRight];
        
        NSLayoutConstraint *bgImageBottom = [NSLayoutConstraint constraintWithItem:_bgView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10];
        [self addConstraint:bgImageBottom];
        
        NSLayoutConstraint *bgImageLeft = [NSLayoutConstraint constraintWithItem:_bgView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        [self addConstraint:bgImageLeft];
    }
    
    return _bgView;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 86)];
        _scrollView.bounces = NO;
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        [self addSubview:_scrollView];
    }
    return _scrollView;
}

- (CustomPageControl *)customPageControl
{
    if (!_customPageControl) {
        _customPageControl = [[CustomPageControl alloc] init];
        _customPageControl.translatesAutoresizingMaskIntoConstraints = NO;
        _customPageControl.backgroundColor = [UIColor clearColor];
        [_customPageControl setCenter: CGPointMake(self.bgView.center.x, self.bgView.bounds.size.height-7.5f)] ;
        [_customPageControl setCurrentPage:0] ;
        [_customPageControl setDefersCurrentPageDisplay: YES] ;
        [_customPageControl setOnColor:kLineColor] ;
        [_customPageControl setOffColor:kLineColor] ;
        [_customPageControl setIndicatorDiameter:7.5f] ;
        [_customPageControl setIndicatorSpace:8.0f] ;
        [self.bgView addSubview:_customPageControl];
        
        NSLayoutConstraint *bgImageHeight = [NSLayoutConstraint constraintWithItem:_customPageControl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:10];
        [self.bgView addConstraint:bgImageHeight];
        
        NSLayoutConstraint *bgImageRight = [NSLayoutConstraint constraintWithItem:_customPageControl attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.bgView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [self.bgView addConstraint:bgImageRight];
        
        NSLayoutConstraint *bgImageBottom = [NSLayoutConstraint constraintWithItem:_customPageControl attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bgView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-5];
        [self.bgView addConstraint:bgImageBottom];
        
        NSLayoutConstraint *bgImageLeft = [NSLayoutConstraint constraintWithItem:_customPageControl attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.bgView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        [self.bgView addConstraint:bgImageLeft];
        
    }
    
    return _customPageControl;
}

@end