//
//  KWMHomeVC.m
//  iCemarose
//
//  Created by HouWeiBin on 16/8/22.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMHomeVC.h"
#import "KWMShopCartVC.h"
#import "KWMGuideVC.h"
#import "KWMBlogCell.h"
#import "KWMSearchBar.h"
#import "KWMSearchFeedbackVC.h"
#import "KWMShopCartData.h"
#import "KWMBlogDetailVC.h"
#import "KWMUserModel.h"
#import "KWMLoginVC.h"
#import "KWMShoppingCart.h"

@interface KWMHomeVC ()
@property (nonatomic)KWMSearchBar *searchBar;
@property (nonatomic)NSArray *dataArr;

@end

@implementation KWMHomeVC

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

- (void)viewDidLoad {
//    KWMUserModel *user = [KWMUserModel shareUser];
//    if (user.status != 1) {
//        //登录
//        KWMLoginVC *loginVc = (KWMLoginVC *)[KWMBaseVC findControllerBy:[KWMLoginVC kwmTag] fromStoryboard:@"Login"];
//        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc];
//        [self presentViewController:nav animated:YES completion:nil];
//    }
    
    
    [super viewDidLoad];
    _dataArr = [NSArray array];
    [self requestBlogAPI];
}

- (UITableView *)targetTableView{
    return self.tbvBlog;
}
- (void)loadData{
    [self requestBlogAPI];
}

- (void)reLoadData{
    [self requestBlogAPI];
}

//刷新用户
//- (void)refreshCustomer:(BUYCustomer *)customer{
//    [self.client updateCustomer:customer callback:^(BUYCustomer * _Nullable customer, NSError * _Nullable error) {
//         if (customer != nil &&error == nil) {
//             [[NSUserDefaults standardUserDefaults] setObject:customer forKey:@"customer"];
//         }else if (error != nil) {
//             [self showError:error];
//         }
//    }];
//}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
//    [self initHeaderView];
//    if (_searchBar) {
//        NSInteger count = [[KWMShoppingCart sharedInstance] count].integerValue;
//        _searchBar.count = count;
//    }
//    [_searchBar resumeView];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:YES];
//    [_searchBar removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)awakeFromNib{
    [super awakeFromNib];
    self.title = @"发现新鲜";
}

//请求api
- (void)requestBlogAPI{
    __weak KWMHomeVC *weakSelf = self;
    [self.api getAllBlog:nil cacheCallback:^(KWMBlogResult *result) {
        if (result != nil) {
            _dataArr =  [[result.blogs reverseObjectEnumerator] allObjects];
            [self.tbvBlog reloadData];
        }
    }  success:^(NSURLSessionDataTask *task, KWMBlogResult *result) {
        [weakSelf hideLoading];
        if (result != nil) {
            _dataArr =  [[result.blogs reverseObjectEnumerator] allObjects];
            [self.tbvBlog reloadData];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if(self.dataArr==nil || self.dataArr.count == 0){
            [self showError:error];
        }
        NSLog(@"error:%@",error);
    }];
}

#pragma  mark UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat imageHeight = 211.0/375.0 * UI_SCREEN_WIDTH;
    CGFloat defaultHeight = 339;
    
    KWMArticlesResult *article = [_dataArr objectAtIndex:indexPath.row];
    if (article.imageScr == nil) {
        imageHeight = 0;
    }
    
    if(indexPath.row == 0) {
        defaultHeight = 354;
    }
    return ( defaultHeight - 221 + imageHeight);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentifier = @"KWMBlogCell";
    
    KWMBlogCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:indentifier bundle:nil] forCellReuseIdentifier:indentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    }
    
    KWMArticlesResult *article = [_dataArr objectAtIndex:indexPath.row];
    
    cell.article = article;
    [cell setData:indexPath.row];
    return cell;
}

//点击了某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    KWMBlogDetailVC *blogDetailVC = (KWMBlogDetailVC *)[KWMBlogDetailVC findControllerBy:@"KWMBlogDetailVC" fromStoryboard:@"Home"];
    KWMArticlesResult *article = [_dataArr objectAtIndex:indexPath.row];
    blogDetailVC.article = article;
    [self.navigationController pushViewController:blogDetailVC animated:YES];
}

- (void)initHeaderView{
    //添加頭部header
    _searchBar = [[KWMSearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    _searchBar.delegate = self;
    _searchBar.btnSearch.hidden = YES;
    [self.view addSubview:_searchBar];
}


#pragma mark -- KWMSearchBarDelegate

- (void)kwm_onClickShopCar{
    //购物车
    KWMShopCartVC *contactVC = (KWMShopCartVC *)[KWMBaseVC findControllerBy:[KWMShopCartVC kwmTag] fromStoryboard:@"ShopCart"];
    [self.navigationController pushViewController:contactVC animated:YES];
}

@end