//
//  uploadFileRecordheadView.m
//  隐私保护
//
//  Created by xd h on 2023/11/15.
//

#import "uploadFileRecordheadView.h"
@interface uploadFileRecordheadView()
@property(nonatomic,strong) UIView *selectBgView;//
@property(nonatomic,assign) CGFloat butWidth;//
@end

@implementation uploadFileRecordheadView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    [self drawAnyView];
    
    return self;
}

- (void)drawAnyView{
    [self setBackgroundColor:[UIColor hwColor:@"#F9F9F9" alpha:1.0]];
    self.layer.cornerRadius = 8;
    
    NSString *leftStr =  NSLocalizedString(@"File_upload_Record_uploading",nil);
    NSString *midStr =  NSLocalizedString(@"File_upload_Record_upload_suc",nil);
    NSString *rightStr =  NSLocalizedString(@"File_upload_Record_upload_fail",nil);
    NSArray *titleArr =  @[leftStr,midStr,rightStr];
    
    _butWidth = (SCREEN_W - 30)/3.0;
    
 
    for (NSInteger i=0; i<titleArr.count; i++) {
   
        NSString* title = titleArr[i];
        
        UIButton *but = [[UIButton alloc] init];
        but.tag = i+1;
        [but setTitle:title forState:UIControlStateNormal];
        [but setTitleColor:[UIColor hwColor:@"#666666" alpha:1.0] forState:UIControlStateNormal];
        [but setTitleColor:[UIColor hwColor:@"#0A132B" alpha:1.0] forState:UIControlStateSelected];
        but.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [but addTarget:self action:@selector(didClickButFun:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:but];
        
        [but mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(i*_butWidth);
            make.width.mas_equalTo(_butWidth);
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
        }];
        
        if(i==0){
            but.selected = YES;
            
            _selectBgView = [[UIView alloc] init];
            _selectBgView.backgroundColor = [UIColor whiteColor];
            _selectBgView.layer.cornerRadius = 8;
            [self insertSubview:_selectBgView atIndex:0];
            
            [_selectBgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(10);
                make.width.mas_equalTo(_butWidth -20);
                make.top.mas_equalTo(6);
                make.bottom.mas_equalTo(-6);
            }];
        }
    }
    
}

#pragma mark 按钮点击
- (void)didClickButFun:(UIButton*)but
{
    NSInteger tag = but.tag;
    
    self.selectIndex = tag;
    
    if(_didClickButFun){
        _didClickButFun(tag);
    }
}

- (void)setSelectIndex:(NSInteger)selectIndex
{
    _selectIndex = selectIndex;
    
    NSArray *subViews = [self subviews];
    
    for (UIButton *but in subViews) {
        if([but isKindOfClass:[UIButton class]]){
            if(but.tag == selectIndex){
                but.selected = YES;
            }
            else{
                but.selected = NO;
            }
        }
    }
    
    CGRect frame = _selectBgView.frame;
    frame.origin.x = (_selectIndex -1)* _butWidth + 10;
    
    [UIView animateWithDuration:0.2 animations:^{
        self->_selectBgView.frame = frame;
    }];
}

@end
