//
//  XVSettingViewController.m
//  fastservice
//
//  Created by xuning on 4/27/25.
//  Copyright © 2025 FastService. All rights reserved.
//

#import "XVSettingViewController.h"
#import "XVSettingTableViewCell.h"
#import "XVAboutViewController.h"

@interface XVSettingViewController ()

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray<NSArray *> *dataArray;

@property (nonatomic, strong) UIButton *logoutButton;

@property (nonatomic, strong) UIButton *versionButton;


@end

@implementation XVSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self setupUI];
}

- (void)setupUI {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 7, 0, 7));
    }];
}

#pragma mark - TableViewDelegate & Datasource

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat cornerRadius =10.0;
    CGRect bounds = cell.bounds;

    // 每区行数
    NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
    
    //绘制曲线
    UIBezierPath *bezierPath = nil;
    
    if (indexPath.row == 0 && numberOfRows == 1) {
             //四个角都为圆角
             bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
         
     } else  if (indexPath.row == 0) {
//      cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);

    //左上、右上角为圆角
    bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
        
    } else if (indexPath.row == numberOfRows - 1) {
//        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);

        //某个区的最后一行：左下、右下角为圆角
        bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    } else {
        //某个区的中间行：为矩形
        bezierPath = [UIBezierPath bezierPathWithRect:bounds];
    }

    cell.backgroundColor = [UIColor clearColor];
    
    //新建一个layer层,设置填充色和边框颜色
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = bezierPath.CGPath;
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.strokeColor = [UIColor whiteColor].CGColor;
    
    //将layer层添加到cell.layer中,并插到最底层
    [cell.layer insertSublayer:layer atIndex:0];
    

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray[section].count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dict = self.dataArray[indexPath.section][indexPath.row];
    XVSettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XVSettingViewController"];

    cell.dict = dict;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XVAboutViewController *aboutVc = [[XVAboutViewController alloc]init];
    [self.navigationController pushViewController:aboutVc animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 80;
    }
    return 44;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 3) {
        return 114;
    }
    return CGFLOAT_MIN;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    if (section == 3) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.width, 114)];
        
        [view addSubview:self.logoutButton];
        self.logoutButton.frame = CGRectMake(0, 20, self.tableView.width, 44);
        
        [view addSubview:self.versionButton];
        self.versionButton.frame = CGRectMake(0, 97, self.tableView.width, 17);
        
        return view;
    }
    return [UIView new];

}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = DEFAULT_BACKGROUND_COLOR;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; //设置分割线的分割
        [_tableView registerClass:[XVSettingTableViewCell class] forCellReuseIdentifier:@"XVSettingViewController"];
    }
    return _tableView;
}
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:4];
        
        [_dataArray addObject:@[
         @{@"title":@"头像",@"type":@(1)},
         @{@"title":@"姓名",@"type":@(2)},
         @{@"title":@"昵称",@"type":@(3)},
         @{@"title":@"性别",@"type":@(4)}]];
        
        [_dataArray addObject:@[
         @{@"title":@"更换手机号",@"type":@(4)},
         @{@"title":@"修改密码",@"type":@(4)}]];

        
        [_dataArray addObject:@[
         @{@"title":@"清除缓存",@"type":@(4)},
         @{@"title":@"关于快服务骑士",@"type":@(4)}]];

        [_dataArray addObject:@[
         @{@"title":@"账号注销",@"type":@(4)}]];

        
    }
    return _dataArray;
}

- (UIButton *)logoutButton {
    if (_logoutButton == nil) {
        _logoutButton = [[UIButton alloc]initWithTitle:@"退出登录" fontSize:14 titleColor:[UIColor whiteColor] bgColor:COLOR_THEMEGRENN];
    }
    return _logoutButton;
}
- (UIButton *)versionButton {
    if(_versionButton == nil) {
        _versionButton = [[UIButton alloc]initWithTitle:S(@"当前版本号：%@(Build %@)",APP_VERSION,APP_BUILD) fontSize:12 titleColor:F_606060_COLOR bgColor:nil];
    }
    return _versionButton;
}
@end
