//
//  GBTagListView.m
//  升级版流式标签支持点击
//
//  Created by 张国兵 on 15/8/16.
//  Copyright (c) 2015年 zhangguobing. All rights reserved.
//

#import "GBTagListView.h"
#import "CLMarkButton.h"
#define HORIZONTAL_PADDING 7.0f
#define VERTICAL_PADDING   3.0f
#define LABEL_MARGIN       18.0f
#define BOTTOM_MARGIN      20.0f
#define KBtnTag            1000
#define R_G_B_16(rgbValue)\
\
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 \
alpha:1.0]

@implementation GBTagListView
-(NSMutableArray *)radioArray
{
    if (!_radioArray) {
        _radioArray =[NSMutableArray new];
    }
    return _radioArray;

}


-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        totalHeight=0;
        self.frame=frame;
//        _tagArr=[[NSMutableArray alloc]init];
        _TagArray =[[NSMutableArray alloc]init];
    }
    return self;
    
    
}
-(void)setTagWithTagArray:(NSArray*)arr{
    
    previousFrame = CGRectZero;
//    [_tagArr addObjectsFromArray:arr];
     [_TagArray addObjectsFromArray:arr];
__weak __typeof(self)weakSelf = self;
    [arr enumerateObjectsUsingBlock:^(NSString*str, NSUInteger idx, BOOL *stop) {
        

        
        CLMarkButton*tagBtn=[[CLMarkButton alloc]init];
        tagBtn.frame=CGRectZero;
        
        if(_signalTagColor){
            //可以单一设置tag的颜色
            tagBtn.backgroundColor=_signalTagColor;
        }else{
            //tag颜色多样
            tagBtn.backgroundColor=[UIColor colorWithRed:random()%255/255.0 green:random()%255/255.0 blue:random()%255/255.0 alpha:1];
        }
        if(_canTouch){
            tagBtn.userInteractionEnabled=YES;
            
        }else{
            
            tagBtn.userInteractionEnabled=NO;
        }
        [tagBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [tagBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        tagBtn.titleLabel.font=[UIFont boldSystemFontOfSize:15];
        [tagBtn setBackgroundImage:[UIImage imageNamed:@"navibg"] forState:UIControlStateSelected];
//        [tagBtn addTarget:self action:@selector(tagBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [tagBtn setTitle:str forState:UIControlStateNormal];
        tagBtn.tag=KBtnTag+idx;
        tagBtn.layer.cornerRadius=13;
        tagBtn.layer.borderColor=[UIColor colorWithRed:194/255.0 green:194/255.0 blue:194/255.0 alpha:1.0].CGColor;
        tagBtn.layer.borderWidth=1;
        tagBtn.clipsToBounds=YES;
        NSDictionary *attrs = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:15]};
        CGSize Size_str=[str sizeWithAttributes:attrs];
        Size_str.width += HORIZONTAL_PADDING*3;
        Size_str.height += VERTICAL_PADDING*3;
        
        CGRect newRect = CGRectZero;
        
        if (previousFrame.origin.x + previousFrame.size.width + Size_str.width + LABEL_MARGIN > self.bounds.size.width) {
            
            newRect.origin = CGPointMake(10, previousFrame.origin.y + Size_str.height + BOTTOM_MARGIN);
            totalHeight +=Size_str.height + BOTTOM_MARGIN;
        }
        else {
            newRect.origin = CGPointMake(previousFrame.origin.x + previousFrame.size.width + LABEL_MARGIN, previousFrame.origin.y);
            
        }
        newRect.size = Size_str;
        [tagBtn setFrame:newRect];
        previousFrame=tagBtn.frame;
        [self setHight:self andHight:totalHeight+Size_str.height + BOTTOM_MARGIN];
        [self addSubview:tagBtn];
        
            [weakSelf.radioArray addObject:tagBtn];
        NSLog(@"%@",weakSelf.radioArray);
    }
     ];
 NSLog(@"count:%lu",(unsigned long)self.radioArray.count);
    for (int i=0; i<self.radioArray.count; ++i)
    {
        CLMarkButton *button = self.radioArray[i];
        button.type = CLMarkTypeSingleCheck;
        button.kCheckSelectedImage =@"";
        button.groupId = 10001;
        button.tag = i;
        [button addTarget:self action:@selector(tagBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    
    
    
    
    
    
    
    
    
    if(_GBbackgroundColor){
        
        self.backgroundColor=_GBbackgroundColor;
        
    }else{
        
        self.backgroundColor=[UIColor whiteColor];
        
    }
    
    
}
#pragma mark-改变控件高度
- (void)setHight:(UIView *)view andHight:(CGFloat)hight
{
    CGRect tempFrame = view.frame;
    tempFrame.size.height = hight;
    view.frame = tempFrame;
}
-(void)tagBtnClick:(CLMarkButton*)button{
//    button.selected=!button.selected;
     [button buttonDidClick];
//    if(button.selected==YES){
//        
//        button.backgroundColor=[UIColor colorWithRed:34/255.0 green:167/255.0 blue:222/255.0 alpha:1.0];
//    }
//    else if (button.selected==NO){
//        button.backgroundColor=[UIColor colorWithRed:179/255.0 green:179/255.0 blue:179/255.0 alpha:1.0];
//    }
    
    [self didSelectItems];
    
    
}
-(void)didSelectItems{
    
    NSMutableArray*arr=[[NSMutableArray alloc]init];
    for(CLMarkButton*buttonCL  in self.radioArray){
    
        if (buttonCL.selected == YES) {
            [arr addObject:_TagArray[buttonCL.tag]];
        }
    }


    self.didselectItemBlock(arr);
    
}
@end
