//
//  CloudRouterShowView.m
//  zhuxun
//
//  Created by winsoft on 17/6/28.
//
//

#import "CloudRouterShowView.h"
#define MarginLR 10
#define MarginBetweenBtn 5
#define MarginTB 4
@interface CloudRouterShowView()

@end
@implementation CloudRouterShowView
+ (CloudRouterShowView *)cloudRouterShowViewWithItems:(NSArray *)items bkColors:(NSArray *)bkColors
{
    CloudRouterShowView *cloudRouterShowView = [[CloudRouterShowView alloc]initCloudRouterShowViewWithItems:items bkColors:bkColors];
    
    return cloudRouterShowView;
}

- (instancetype)initCloudRouterShowViewWithItems:(NSArray *)items bkColors:(NSArray *)bkColors
{
    if (self = [super init]) {
        
        for (int i = 0; i < items.count; i++) {
            
            UIButton *actionBtn = [[UIButton alloc]init];
            actionBtn.tag = i;
            [actionBtn setTitle:items[i] forState:UIControlStateNormal];
            [actionBtn setBackgroundColor:bkColors[i]];
            [actionBtn addTarget:self action:@selector(actionForBtn:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:actionBtn];
        
            
        }
        
    }
    
    return self;
}

- (void)actionForBtn:(UIButton *)btn
{
    if (self.actionBlock) {
        self.actionBlock(btn.tag);
    }
}

- (void)setIndexActionBtnEnableWithIndex:(NSInteger)index enable:(BOOL)enable
{
    UIButton *targetBtn = self.subviews[index];
    
    targetBtn.enabled = enable;
    
    targetBtn.backgroundColor = [UIColor lightGrayColor];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
    CGFloat btnW = (self.frame.size.width - 2 * MarginLR - (self.subviews.count - 1) * MarginBetweenBtn)/self.subviews.count;
    
    CGFloat btnH = self.frame.size.height - 2 * MarginTB;
    
    for (UIButton *subBtn in self.subviews) {
        
        CGFloat btnX = subBtn.tag * (btnW + MarginBetweenBtn) + MarginLR;
        CGFloat btnY = (self.frame.size.height - btnH)/2.0f;
        subBtn.layer.masksToBounds = YES;
        subBtn.layer.cornerRadius = 2;
        subBtn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
}
@end
