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

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

@interface XVAboutViewController ()

@property (nonatomic, strong) UITableView *tableView;

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

@property (nonatomic, strong) UIImageView *logoImageView;

@property (nonatomic, strong) UIButton *versionButton;

@end

@implementation XVAboutViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.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)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

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

    cell.dict = dict;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

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

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

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return CGFLOAT_MIN;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.width, 114)];
    
    [view addSubview:self.logoImageView];
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(86, 86));
        make.centerX.mas_equalTo(view);
        make.top.mas_equalTo(view).mas_offset(31);
    }];
    
    [view addSubview:self.versionButton];
    [self.versionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.centerX.mas_equalTo(view);
        
        make.top.mas_equalTo(self.logoImageView.mas_bottom).mas_offset(16);
    }];
    
    return view;
    
    return [UIView new];
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    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:@"XVAboutViewController"];
    }
    return _tableView;
}
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:4];
        
        [_dataArray addObjectsFromArray:@[
         @{@"title":@"骑士协议",@"type":@(4)},
         @{@"title":@"隐私协议",@"type":@(4)},
         @{@"title":@"官网",@"type":@(4)},
         @{@"title":@"检查新版",@"type":@(4)}]];

    }
    return _dataArray;
}

- (UIImageView *)logoImageView {
    if (_logoImageView == nil) {
        _logoImageView = [[UIImageView alloc]initWithImageName:@"tubiao"];
    }
    return _logoImageView;
}

- (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
