//
//  TopicViewController.m
//  RedAnt ERP Mobile
//
//  Created by Ray on 14-5-26.
//  Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
//

#import "MainViewController.h"
#import "TopicViewController.h"

@interface TopicViewController ()

@end

@implementation TopicViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
       CGRect r= self.TopicCollection.frame;
    r.size.height = self.count/4 * 210;
    self.TopicCollection.frame = r;
    
//    UICollectionView* TopicCollectiondelegate =self.TopicCollection;
    self.TopicCollection.delegate = self;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void) itemCount : (int)count
{
    self.count = count;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -- UICollectionViewDataSource

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

{
    
    // 每个Section的item个数
    
    return self.count;
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"TopicItem";
    TopCellItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    NSDictionary * item_json =[self.content objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row] ];
    NSString* img_url =[item_json valueForKey:@"img"];
    NSString* description =[item_json valueForKey:@"description"];
    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.cellImage.image=[UIImage imageNamed:@"loading_s"];
    NSString* file_name=[img_url lastPathComponent];
    NSData* img_data=[iSalesDB load_cached_img:file_name];
    if(img_data!=nil)
    {
        
        UIImage * img =[UIImage imageWithData:img_data];
        cell.cellImage.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 :file_name ];
                    
                    UIImage * img =[UIImage imageWithData:downloadimg_data];
                    cell.cellImage.image = img;
                }
                else
                    cell.cellImage.image=[UIImage imageNamed:@"notfound_s"];
                
            });
        });
        
        
    }
    
//    cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row) / 255.0) green:((20 * indexPath.row)/255.0) blue:((30 * indexPath.row)/255.0) alpha:1.0f];
    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(5, 5, 5, 5);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

        int type = 0;
        //    if(collectionView == self.topicview.TopicCollection)
        //        type = 0;
        //    else
        //        type = 1;
        MainViewController* pvc = (MainViewController*)self.rootViewController;
        [pvc topicViewSelected:type _id:(int)indexPath.row];
        //    [(MainViewController*)self.parentViewController topicViewSelected type:indexPath.row];
        

}
//- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
//{
//    //     CGRect frame = self.view.frame;
//    //    [self.carouselController.view removeFromSuperview];
//    //    wait(2000);
//    //    self.carouselController = [[FPCarouselNonXIBViewController alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 400)];
//    //    [self.view addSubview:self.carouselController.view];
//    //    int i = self.view.subviews.count;
//    //    int b = 0;
//    // [self.carouselController updateFrame:CGRectMake(0, 0, self.view.frame.size.width, 400)];
//}
////返回这个UICollectionView是否可以被选择
//-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}

@end
