//
//  KWMBottomView.m
//  iCemarose
//
//  Created by 陈荣科 on 16/9/6.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMBottomView.h"
#import "KWMTBVSectionHeardView.h"

@interface KWMBottomView ()

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

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,weak) IBOutlet UIPageControl *pageControl;

@end

@implementation KWMBottomView

#define sclHeight (UI_SCREEN_HEITHT-20-20-40-90-64)
static NSString *idStr = @"KWMNewGoodsCell";
- (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)awakeFromNib{
    [super awakeFromNib];
}

-(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];
    

}


- (void)initView{
    self.isLoadingSameBrand = NO;
    self.isLoadingSameType = NO;
    [self.tableView registerNib:[UINib nibWithNibName:idStr bundle:nil] forCellReuseIdentifier:idStr];
}

- (void)setCount:(NSInteger)count{
    _count = count+1;
    if(count>0){
        self.pageControl.numberOfPages = count;
        //需要放在设置图片之前,不然会发生crash
        self.pageControl.currentPage = self.count-1;

        [self.pageControl setValue:[UIImage imageNamed:@"ic_page_3"] forKey:@"_currentPageImage"];
        [self.pageControl setValue:[UIImage imageNamed:@"ic_page_1"] forKey:@"_pageImage"];
    }
}


#pragma mark -- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KWMNewGoodsCell *newGoodsCell = [tableView dequeueReusableCellWithIdentifier:idStr forIndexPath:indexPath];
    if(!newGoodsCell){
        [tableView registerNib:[UINib nibWithNibName:idStr bundle:nil] forCellReuseIdentifier:idStr];
        newGoodsCell = [tableView dequeueReusableCellWithIdentifier:idStr forIndexPath:indexPath];
    }
    newGoodsCell.isDetail = YES;
    newGoodsCell.delegate = self;
    newGoodsCell.isSameBrand = indexPath.section == 0;
    if(newGoodsCell.isSameBrand){//同品牌推荐
        if(self.pageSameBrand <= 1){
            [newGoodsCell setData:self.sameBrandProducts];
        }else{
            [newGoodsCell addData:self.sameBrandProducts];
        }
    }else{//同类型商品推荐
        if(self.pageSameType <= 1){
            [newGoodsCell setData:self.sameTypeProducts];
        }else{
            [newGoodsCell addData:self.sameTypeProducts];
        }
    }
    return newGoodsCell;
}

#pragma mark -- KWMNewGoodsCellDelegate
- (void)kwm_selectCollectionCell:(BUYProduct *)product{
    if ([self.delegate respondsToSelector:@selector(kwm_clickCollectionCell:)]) {
        [self.delegate kwm_clickCollectionCell:(BUYProduct *)product];
    }
}

-(void)kwm_onNexPage:(BOOL)isNewBrand{
    if ([self.delegate respondsToSelector:@selector(kwm_loadNewPage:)]) {
        [self.delegate kwm_loadNewPage:isNewBrand];
    }
}

#pragma mark - UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  173.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    KWMTBVSectionHeardView *vTBVSectionHeard = [[KWMTBVSectionHeardView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 60)];
    if (section == 0) {
        NSString *title;
        if(self.brand == nil){
            vTBVSectionHeard = [[KWMTBVSectionHeardView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 40)];
            title = @"其他商品";
        }else{
            title = [NSString stringWithFormat: @"%@\n其他商品", self.brand];
        }
        vTBVSectionHeard.lbSectionTitel.text = title;
    }else{
        vTBVSectionHeard = [[KWMTBVSectionHeardView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 40)];
        vTBVSectionHeard.lbSectionTitel.text = @"同类商品";
    }
    vTBVSectionHeard.ivMore.hidden = YES;
    return vTBVSectionHeard;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0 && self.brand != nil){
        return 60;
    }else{
        return 40;
    }
}


-(void)setData:(NSArray *)products isSameBrand:(BOOL)isSameBrand{
    if(products == nil || products.count == 0){
        return;
    }
    if(isSameBrand){
        self.sameBrandProducts = products;
    }else{
        self.sameTypeProducts = products;
    }
    [self.tableView reloadData];
}

-(void)setBrandString:(NSString *)brand{
    if(brand == nil){
        return;
    }
    self.brand = brand;
}

-(void)clear{
    self.brand = @"";
    self.pageSameBrand = 0;
    self.pageSameType = 0;
    self.sameTypeProducts = nil;
    self.sameBrandProducts = nil;
    [self.tableView reloadData];
}



@end