//
//  KWMHotSalesHeader.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/7/10.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMHotSalesHeader.h"
#import "KWMProductBannerItemView.h"
#import "UIImageView+WebCache.h"
#import "KWMNewHomeCell.h"
#import <SDCycleScrollView/SDCycleScrollView.h>

@interface KWMHotSalesHeader()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,SDCycleScrollViewDelegate>

@property (nonatomic,weak) IBOutlet UIView *vView;

@property (nonatomic,weak) IBOutlet UIView *bannerView;

@property (nonatomic, strong) SDCycleScrollView *focusView;

@property (nonatomic,weak) IBOutlet UICollectionView *singleShowView;

@property (nonatomic,weak) IBOutlet NSLayoutConstraint *heightBannerView;

@property (nonatomic,weak) IBOutlet NSLayoutConstraint *heightSingleShowView;

@end

@implementation KWMHotSalesHeader

+(NSString *)kwmTag{
    return @"KWMHotSalesHeader";
}

- (id)init{
    if (self=[super init]){
        [self addView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addView];
    }
    return self;
}

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

-(void)addView{
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
                                  owner:self
                                options:nil];
    
    self.vView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self addSubview:self.vView];
    [self initView];
}

-(NSInteger)actrualHeight{
    NSInteger height = 60 + self.heightSingleShowView.constant + self.heightBannerView.constant;
    return height;
}

-(void)initView{
    UINib *nib = [UINib nibWithNibName:@"KWMNewHomeCell" bundle:nil];
    [self.singleShowView registerNib:nib forCellWithReuseIdentifier:@"KWMNewHomeCell"];
    self.singleShowView.delegate = self;
    self.singleShowView.dataSource = self;
    self.heightBannerView.constant = 0;
    self.heightSingleShowView.constant = 0;
}

-(void)initFocusView {
    if (!_focusView) {
        _focusView = [[SDCycleScrollView alloc] init];
        _focusView.delegate = self;
        _focusView.translatesAutoresizingMaskIntoConstraints = NO;
        _focusView.frame = CGRectMake(0, 0, 375, 190);
        _focusView.autoScrollTimeInterval = 3;
        _focusView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        _focusView.pageDotImage = [UIImage imageNamed:@"ic_page_1"];
        _focusView.currentPageDotImage = [UIImage imageNamed:@"ic_page_2"];
        [self.bannerView addSubview:_focusView];
        NSDictionary *views = @{ @"v": _focusView };
        [self.bannerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v]|" options:0 metrics:nil views:views]];
        [self.bannerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views:views]];
    }
}

-(void)setBannerArray:(NSArray *)bannerArray{
    _bannerArray = bannerArray;
    if(bannerArray && bannerArray.count > 0){
        [self initFocusView];
        self.focusView.imageURLStringsGroup = [bannerArray  rx_mapWithBlock:^id(KWMAdvertisement *each) {
            return each.image;
        }];
        self.heightBannerView.constant = 190;
    }else{
        self.heightBannerView.constant = 0;
    }
}

-(void)setSingleShowArray:(NSArray *)singleShowArray{
    _singleShowArray = singleShowArray ? singleShowArray : [NSArray array];
    
//    NSInteger itemWidth = UI_SCREEN_WIDTH - 16;
    NSInteger itemHeight = UI_SCREEN_WIDTH + 58;
    
//    for(int i=0;i<singleShowArray.count;i++){
//        BUYProduct *product = [singleShowArray objectAtIndex:i];
//        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"KWMNewHomeCell" owner:self options:nil];
//        KWMNewHomeCell *homeCell = [nib objectAtIndex:0];
//        homeCell.frame = CGRectMake(8, itemHeight*i, itemWidth, itemHeight);
//        homeCell.singleShow = YES;
//        homeCell.tag = i;
//        homeCell.product = product;
//        
//        [self.singleShowView addSubview:homeCell];
//    }
    self.heightSingleShowView.constant = itemHeight*singleShowArray.count;
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.singleShowArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    KWMNewHomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KWMNewHomeCell" forIndexPath:indexPath];
    cell.singleShow = YES;
    cell.product = self.singleShowArray[indexPath.item];
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(UI_SCREEN_WIDTH - 16, UI_SCREEN_WIDTH + 58);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate kwm_onClickProduct:self.singleShowArray[indexPath.item]];
}

#pragma mark - KWMBannerViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    [self.delegate kwm_onClickHotSalesAd:self.bannerArray[index]];
}

//-(void)bannerView:(KWMBannerView *)bannerView onClickPage:(NSInteger)index{
//    KWMAdvertisement *adv = [self.bannerArray objectAtIndex:index];
//    [self.delegate kwm_onClickFocusImage:adv];
//}




@end