//
//  filePathCreatPopView.m
//  隐私保护
//
//  Created by xd h on 2023/12/29.
//

#import "filePathCreatPopView.h"
#import "filePathCreatTableViewCell.h"
#import "cloudPhoneExtraFileListModel.h"

@interface filePathCreatPopView ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    UIView *whiteBgView;
    UILabel *titleLabel;
    UITextField *filePathNameTF;
    
    UIButton *canclButton;
    UIButton *okButton;
}

@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic,copy) NSMutableArray *curDataArr;//磁盘数据 +默认
@end

@implementation filePathCreatPopView

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



- (void)drawAnyView{
    [self setBackgroundColor:[UIColor hwColor:@"#000000" alpha:0.6]];
    
    whiteBgView = [[UIView alloc] init];
    whiteBgView.backgroundColor = [UIColor whiteColor];
    whiteBgView.layer.cornerRadius = 12;
    [self addSubview:whiteBgView];
    
    [whiteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(418+ 10 + safeArea);
        //make.height.mas_equalTo(200+ 10 + safeArea);
        make.bottom.mas_equalTo(10);
    }];
    
    UIView *tapView = [[UIView alloc] init];
    [self addSubview:tapView];
    
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapUpFun:)];
//    [tapView addGestureRecognizer:tap];
    
    [tapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(whiteBgView.mas_top);
    }];
    
    titleLabel = [[UILabel alloc] init];
    titleLabel.text = NSLocalizedString(@"File_upload_path_new",nil);
    titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
    titleLabel.textColor = [UIColor hwColor:@"#0A132B" alpha:1.0];
    [whiteBgView addSubview:titleLabel];

    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(20);
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(15);
    }];
    
    [whiteBgView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(70*2+50);
        //make.height.mas_equalTo(0.0);
        make.top.equalTo(titleLabel.mas_bottom).offset(15.f);
    }];
    //self.tableView.hidden = YES;
    
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.text = NSLocalizedString(@"File_upload_path_new_name_tip",nil);
    tipLabel.font = [UIFont systemFontOfSize:14.0];
    tipLabel.textColor = [UIColor hwColor:@"#0A132B" alpha:1.0];
    [whiteBgView addSubview:tipLabel];

    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(20);
        make.left.mas_equalTo(15);
        make.top.equalTo(self.tableView.mas_bottom).offset(15);
    }];
    
    filePathNameTF = [[UITextField alloc] init];
    filePathNameTF.backgroundColor = [UIColor hwColor:@"#F9F9F9" alpha:1.0];
    filePathNameTF.layer.cornerRadius = 8;
    filePathNameTF.font = [UIFont systemFontOfSize:14.0];
    filePathNameTF.placeholder = NSLocalizedString(@"File_upload_path_new_input_tip",nil);
    filePathNameTF.delegate = self;
    filePathNameTF.returnKeyType = UIReturnKeyDone;
    [whiteBgView addSubview:filePathNameTF];
    [filePathNameTF setTintColor:[UIColor hwColor:@"#01B7EA" alpha:1.0]];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 1)];
    filePathNameTF.leftView = leftView;
    filePathNameTF.leftViewMode = UITextFieldViewModeAlways;

    [filePathNameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(48);
        make.left.mas_equalTo(15);
        make.top.equalTo(tipLabel.mas_bottom).offset(15);
    }];
    
    
    canclButton = [[UIButton alloc] init];
    [canclButton setTitle:NSLocalizedString(@"other_cancel",nil) forState:UIControlStateNormal];
    [canclButton setTitleColor:[UIColor hwColor:@"#0A132B" alpha:1.0] forState:UIControlStateNormal];
    canclButton.backgroundColor = [UIColor hwColor:@"#E3E8F1" alpha:1.0];
    canclButton.layer.cornerRadius = 8;
    canclButton.tag = 1;
    [canclButton addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside];
    [whiteBgView addSubview:canclButton];

    [canclButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15.0);
        make.right.equalTo(whiteBgView.mas_centerX).offset(-10);
        make.height.mas_equalTo(40);
        make.bottom.mas_equalTo(-safeArea -10);
    }];


    okButton = [[UIButton alloc] init];
    okButton.tag = 2;
    [okButton setTitle:NSLocalizedString(@"other_comment_ok",nil) forState:UIControlStateNormal];
    okButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
    okButton.layer.cornerRadius = 8;
    okButton.layer.masksToBounds = YES;
    [okButton addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside];
    [whiteBgView addSubview:okButton];

    // gradient
    CAGradientLayer* _glayer = [CAGradientLayer layer];
    _glayer.startPoint = CGPointMake(0, 0.5);
    _glayer.endPoint = CGPointMake(1, 0.5);
    _glayer.colors = @[(__bridge id)[UIColor hwColor:@"#0BDDFD" alpha:1.0].CGColor, (__bridge id)[UIColor hwColor:@"#048CFB" alpha:1.0].CGColor];
    _glayer.locations = @[@(0), @(1.0f)];
    [okButton.layer  insertSublayer:_glayer atIndex:0];
    
    CGRect frame = CGRectMake(0, 0, SCREEN_W/2.0, 40);
    _glayer.frame = frame;
    
    [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15.0);
        make.left.equalTo(whiteBgView.mas_centerX).offset(10);
        make.height.mas_equalTo(40);
        make.bottom.mas_equalTo(-safeArea -10);
    }];
}

#pragma mark - 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
//        _tableView.contentInset = UIEdgeInsetsMake(-H_STATE_BAR, 0, 0, 0);
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
        [_tableView setSeparatorColor:[UIColor clearColor]];
        [_tableView setBackgroundColor:[UIColor clearColor]];
        [_tableView setTableFooterView:[UIView new]];
        [_tableView setBounces:YES];
        _tableView.backgroundColor = [UIColor hwColor:@"#F9F9F9" alpha:1.0];
        _tableView.layer.cornerRadius = 8;
        _tableView.scrollEnabled = NO;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        
    }
    
    return _tableView;
}


#pragma mark - 列表委托
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _curDataArr.count;
}

- (filePathCreatTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    __block NSInteger row = indexPath.row;
    static NSString *identifier = @"filePathCreatTableViewCell";
    
    filePathCreatTableViewCell * cell =  [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell){
        cell = [[filePathCreatTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setAccessoryType:(UITableViewCellAccessoryNone)];
        
        [cell.bgViewLayer removeFromSuperlayer];
        [cell.titleLabel2 setHidden:NO];
        [cell.rightImage setHidden:YES];
        [cell.lineView setHidden:YES];
        [cell.checkButton setHidden:NO];
        
    }
    
    if(row < _curDataArr.count){
        cloudPhoneExtraFileModel *model = _curDataArr[row];
        cell.curModel = model;
    }
    
//    if (row == 0){
//        //[cell.titleLabel setText:NSLocalizedString(@"File_Transfer_set_Cellular",nil)];
//        cell.titleLabel.text = @"云机系统名称";
//
//        cell.checkButton.selected = YES;
//
//    }else if (row == 1){
//        //[cell.titleLabel setText:NSLocalizedString(@"File_Transfer_set_battery_level",nil)];
//
//        cell.titleLabel.text = @"云机系统名称";
//
//    }

    KWeakSelf
    cell.didClickSwitch = ^(BOOL SwitchOn) {
        [weakSelf setModelSelectInRow:row];
    };
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.0;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 40)];
    //headView.backgroundColor = [UIColor greenColor];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = NSLocalizedString(@"File_upload_path_new_check_tip",nil);
    titleLabel.font = [UIFont systemFontOfSize:14.0];
    titleLabel.textColor = [UIColor hwColor:@"#0A132B" alpha:1.0];
    [headView addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(20);
        make.top.mas_equalTo(10);
    }];
    
    return headView;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
      NSString *beString = [textField.text stringByReplacingCharactersInRange:range withString:string];
      if ([beString length] > 16) {
        textField.text = [beString substringToIndex:16];
        return NO;
      }
      return YES;
}

- (void)setModelSelectInRow:(NSInteger)row
{
    for (int i=0; i<_curDataArr.count; i++) {
        cloudPhoneExtraFileModel *model = _curDataArr[i];
        model.isCheckType = NO;
        
        if(i == row){
            model.isCheckType = YES;
            _defaultSaveDiskStr = model.extraPath;
        }
    }
    
    [self.tableView reloadData];
}

//- (void)setFileTransferType:(NSInteger)fileTransferType
//{
//    _fileTransferType = fileTransferType;
//
//    NSString *uploadTitle = NSLocalizedString(@"File_upload_type_image",nil);
//    NSString *uploadTip = NSLocalizedString(@"File_upload_type_tip",nil);
//
//    NSString *downloadTitle = NSLocalizedString(@"File_down_type_image",nil);
//    NSString *downloadTip = NSLocalizedString(@"File_down_type_tip",nil);
//
//    if(fileTransferType == 2)
//    {
//        uploadTitle = NSLocalizedString(@"File_upload_type_video",nil);
//        downloadTitle = NSLocalizedString(@"File_down_type_video",nil);
//    }
//
//    UILabel *uploadLabel = [[UILabel alloc] init];
//    uploadLabel.textColor = [UIColor whiteColor];
//    uploadLabel.font = [UIFont boldSystemFontOfSize:18.0];
//    uploadLabel.numberOfLines = 0;
//    [uploadButton addSubview:uploadLabel];
//
//    NSString *totalStr = [[NSString alloc] initWithFormat:@"%@\n%@",uploadTitle,uploadTip];
//
//    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
//
//    NSRange redRange = NSMakeRange([totalStr rangeOfString:uploadTip].location, [totalStr rangeOfString:uploadTip].length);
//    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor hwColor:@"#FFFFFF" alpha:0.8] range:redRange];
//    [noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:redRange];
//    // 设置行间距
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//    [paragraphStyle setLineSpacing:5];        //设置行间距
//
//    [noteStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalStr  length])];
//
//    uploadLabel.attributedText = noteStr;
//
//    [uploadLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(15);
//        make.right.mas_equalTo(-15);
//        make.bottom.mas_equalTo(0);
//        make.top.mas_equalTo(0);
//    }];
//
//    UILabel *downloadLabel = [[UILabel alloc] init];
//    downloadLabel.textColor = [UIColor whiteColor];
//    downloadLabel.font = [UIFont boldSystemFontOfSize:18.0];
//    downloadLabel.numberOfLines = 0;
//    [downButton addSubview:downloadLabel];
//
//    NSString *downTotalStr = [[NSString alloc] initWithFormat:@"%@\n%@",downloadTitle,downloadTip];
//
//    NSMutableAttributedString *downNoteStr = [[NSMutableAttributedString alloc] initWithString:downTotalStr];
//
//    NSRange downredRange = NSMakeRange([downTotalStr rangeOfString:downloadTip].location, [downTotalStr rangeOfString:downloadTip].length);
//    [downNoteStr addAttribute:NSForegroundColorAttributeName value:[UIColor hwColor:@"#FFFFFF" alpha:0.8] range:downredRange];
//    [downNoteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:downredRange];
//    // 设置行间距
////    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
////    [paragraphStyle1 setLineSpacing:5];        //设置行间距
//
//    [downNoteStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [downTotalStr  length])];
//
//    downloadLabel.attributedText = downNoteStr;
//
//    [downloadLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(15);
//        make.right.mas_equalTo(-15);
//        make.bottom.mas_equalTo(0);
//        make.top.mas_equalTo(0);
//    }];
//}


- (void)setOutSizeArr:(NSArray *)outSizeArr
{
    NSMutableArray *arr = [NSMutableArray new];
    if(outSizeArr && outSizeArr.count >0){
        
        for (cloudPhoneExtraFileModel*model in outSizeArr) {
            model.isCheckType = NO;
            [arr addObject:model];
        }
        //[arr addObjectsFromArray:outSizeArr];
    }
    
//    cloudPhoneExtraFileModel *defModel = [cloudPhoneExtraFileModel new];
//    //defModel
//    defModel.extraPath = @"sdcard";
//    //defModel.extraTotalSize = ksharedAppDelegate.couldPhoneBaseInfoMod;
//    
//    [arr insertObject:defModel atIndex:0];
    
    //设置默认的选中
    cloudPhoneExtraFileModel *defModel = arr.firstObject;
    defModel.isCheckType = YES;
    _defaultSaveDiskStr = defModel.extraPath;
    
    NSInteger tableRowHeight = 70;
    NSInteger allRow = arr.count;
    
    [whiteBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(278 +tableRowHeight*allRow  + 10 + safeArea);
        //make.height.mas_equalTo(418+ 10 + safeArea);
        make.bottom.mas_equalTo(10);
    }];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(tableRowHeight*allRow +50);
        //make.height.mas_equalTo(0.0);
        make.top.equalTo(titleLabel.mas_bottom).offset(15.f);
    }];
    
    _curDataArr = arr;
    [self.tableView reloadData];
}

- (void)tapUpFun:(UITapGestureRecognizer*)tap
{
    //[self deleteFromSuperview];
}

- (void)deleteFromSuperview
{
    
    [self removeFromSuperview];
}

- (void)didClickButtonFun:(UIButton*)button
{
    NSInteger tag = button.tag;
    
    if(tag == 1){
        [self deleteFromSuperview];
    }
    else if(tag == 2){
        
        if(!_defaultSaveDiskStr ||_defaultSaveDiskStr.length ==0){
            //[[iToast makeText:NSLocalizedString(@"File_upload_path_new_input_tip",nil)] show];
            return;
        }
        
        NSString* folderName = [filePathNameTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        //去掉中间空格
        folderName = [folderName stringByReplacingOccurrencesOfString:@" " withString:@""];
        folderName = [folderName stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        if(!folderName || folderName.length ==0){
            [[iToast makeText:NSLocalizedString(@"File_upload_path_new_input_tip",nil)] show];
            return;
        }
        
        [self deleteFromSuperview];
        
        if(_didClickOkBut)
        {
            _didClickOkBut(folderName,_defaultSaveDiskStr);
        }
    }
    
}
@end
