//
//  CSFileExtensionViewForCell.m
//  zhuxun
//
//  Created by winsoft on 17/6/16.
//
//

#import "CSFileExtensionViewForCell.h"

#import "CustomButton.h"

#define ItemSizeW 32
#define OnePageMaxItemCount 4

@interface CSFileExtensionViewForCell()
@property (nonatomic , strong) NSMutableArray *itemOpArrays;
@property (nonatomic , strong) NSArray *itemArray;
@end
@implementation CSFileExtensionViewForCell

- (NSMutableArray *)itemOpArrays
{
    if (!_itemOpArrays) {
        _itemOpArrays = [NSMutableArray array];
    }
    
    return _itemOpArrays;
    
}

- (instancetype)initWithItmes:(NSArray *)items
{
    if (self = [super init]) {
        
        self.backgroundColor = LineForSubLineViewColor;
 
        [self.itemOpArrays addObjectsFromArray:items];
        
//        UIScrollView *mainScrollView = [[UIScrollView alloc]init];
//        [self addSubview:mainScrollView];
//        self.mainScrollView = mainScrollView;
        
        
        _itemArray = items;
        
        for (int i = 0; i < self.itemOpArrays.count; i ++) {
            NSDictionary *itemDict = self.itemOpArrays[i];
            CustomButton *itemBtn = [[CustomButton alloc]initCustomBtnWithImageDirection:ImagePostionTop imageViewSize:CGSizeMake(ItemSizeW, ItemSizeW) imageLabelMargin:0 titleLabelFont:[UIFont systemFontOfSize:10] imageIcon:itemDict[@"icon"] selectIcon:nil title:itemDict[@"title"] btnWidth:ItemSizeW];
            
            [itemBtn addTarget:self action:@selector(extensionOp:) forControlEvents:UIControlEventTouchUpInside];
            itemBtn.tag = i;
            [self addSubview:itemBtn];
        }
        
    }
    
    return self;
}

- (void)extensionOp:(CustomButton *)btn
{
    NSDictionary *item = self.itemArray[btn.tag];
    
    if (self.extensionOpBlock) {
        self.extensionOpBlock((ExtensionOP)[item[@"opType"] integerValue],nil);
    }
}

- (void)setDisable:(BOOL)disable
{
    _disable = disable;
    
    if (disable) {
        

        
        for (CustomButton *customBtn in self.subviews) {
            if ([customBtn isKindOfClass:[CustomButton class]]) {
                
                customBtn.enabled = NO;
                
                [customBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            }
            
        }

    }else{

        
        for (CustomButton *customBtn in self.subviews) {
            if ([customBtn isKindOfClass:[CustomButton class]]) {
                
                customBtn.enabled = YES;
                
                [customBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
            
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
//    self.mainScrollView.frame = self.bounds;
    
//    CGFloat leftRightMargin = (self.frame.size.width - OnePageMaxItemCount * ItemSizeW) / (OnePageMaxItemCount * 2);
    
    CGFloat leftRightMargin = (self.frame.size.width - self.itemOpArrays.count * ItemSizeW) / (self.itemOpArrays.count * 2);

    
    
    self.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
    
    for (CustomButton *customBtn in self.subviews) {
        if ([customBtn isKindOfClass:[CustomButton class]]) {
//            CGFloat frameX = customBtn.tag * (leftRightMargin * 2 + ItemSizeW) + leftRightMargin;
            CGFloat frameX = customBtn.tag * (leftRightMargin * 2 + ItemSizeW) + leftRightMargin;
            CGFloat frameY = (self.frame.size.height - customBtn.customBtnHeight)/2.0f;
            CGFloat frameW = ItemSizeW;
            CGFloat frameH = customBtn.customBtnHeight;
            
            customBtn.frame = CGRectMake(frameX, frameY, frameW, frameH);
        }
        
        
    }
    
    
}
@end
