//
//  HomeTableViewCellTopic.m
//  RedAnt ERP Mobile
//
//  Created by Ray on 7/21/15.
//  Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
//

#import "HomeTableViewCellTopic.h"
#import "CategoryCellSmall.h"
#import "iSalesDB.h"
#import "DetailViewController.h"

@implementation HomeTableViewCellTopic
@synthesize itemdelegate;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark -- UICollectionViewDataSource

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{
    
    // 每个Section的item个数
    int count =[[self.related_data  valueForKey:@"count"] intValue];
    return count;
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString * CellIdentifier = @"RelatedModelCell";
    CategoryCellSmall * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    NSDictionary * item_json =[self.related_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
    NSString* img_url =[item_json valueForKey:@"picture_path"];
    NSString* description =[item_json valueForKey:@"fash_name"];
    //        NSString* old_price =[item_json valueForKey:@"old_price"];
    //        NSString* price =[item_json valueForKey:@"price"];
    cell.cellDescription.text = description;
    //        cell.oldPrice.text = old_price;
    //        cell.Price.text = price;
    cell.cellImageView.image = [UIImage imageNamed:@"loading_s"];
    NSString* file_name=[img_url lastPathComponent];
    NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
    if(img_data!=nil)
    {
        
        UIImage * img =[UIImage imageWithData:img_data];
        cell.cellImageView.image = img;
    }
    else
    {
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            NSData*  downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                
                if(downloadimg_data!=nil)
                {
                    
                    [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
                    
                    UIImage * img =[UIImage imageWithData:downloadimg_data];
                    cell.cellImageView.image = img;
                }
                else
                    cell.cellImageView.image = [UIImage imageNamed:@"notfound_s"];
                
            });
        });
        
        
    }
    return cell;
    
}

#pragma mark --UICollectionViewDelegateFlowLayout



////定义每个UICollectionView 的大小
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return CGSizeMake(96, 100);
//}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
#pragma mark --UICollectionViewDelegate



//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary* json=[self.related_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
    NSString* product_id = [json valueForKey:@"product_id"];
    NSString* category = [json valueForKey:@"category"];
    [itemdelegate TopicItemClicked : product_id category:category];
}

@end
