//
//  XXGSelectMobileAccountViewController.m
//  XXGPlayKit
//
//  Created by apple on 2025/2/6.
//

#import "XXGSelectMobileAccountViewController.h"
#import "NSString+XXGString.h"

@implementation XXGSelectMobileAccountModel

@end

@interface XXGSelectMobileAccountViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) XXGSelectMobileAccountModel *xxpk_model;
@property (nonatomic, strong) UITableView *xxpk_tableView;
@property (nonatomic, assign) NSInteger xxpk_selectIdx;
@property (nonatomic, strong) UIButton *xxpk_cancelButton;
@property (nonatomic, strong) UIButton *xxpk_confirmButton;

@end

@implementation XXGSelectMobileAccountViewController

- (XXGSelectMobileAccountModel *)xxpk_model {
    return self.xxpk_object;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.xxpk_closeButton.hidden = YES;
    self.xxpk_backButton.hidden = YES;

    // 顶部标题
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = XXGUIDriver.xxpk_string_ui.xxpk_select_account;
    titleLabel.textColor = [XXGUIDriver xxpk_mainColor];
    titleLabel.font = [UIFont systemFontOfSize:XXGUIDriver.xxpk_data_ui.xxpk_float17];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(XXGUIDriver.xxpk_data_ui.xxpk_float8);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(XXGUIDriver.xxpk_data_ui.xxpk_float35);
    }];

    self.view.clipsToBounds = YES;
    self.view.layer.cornerRadius = XXGUIDriver.xxpk_data_ui.xxpk_float4;

    // 底部按钮容器
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(XXGUIDriver.xxpk_data_ui.xxpk_float48);
    }];

    // 取消按钮
    _xxpk_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_xxpk_cancelButton setTitle:XXGUIDriver.xxpk_string_ui.xxpk_cancel forState:UIControlStateNormal];
    [_xxpk_cancelButton setTitleColor:UIColor.darkGrayColor forState:UIControlStateNormal];
    [_xxpk_cancelButton setBackgroundColor:UIColor.systemGray5Color];
    _xxpk_cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [_xxpk_cancelButton addTarget:self action:@selector(xxpk_cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:_xxpk_cancelButton];
    [_xxpk_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(bottomView);
        make.width.equalTo(bottomView).multipliedBy(0.5);
    }];

    // 确定按钮
    _xxpk_confirmButton = [XXGUIDriver xxpk_buttonMainColor:XXGUIDriver.xxpk_string_ui.xxpk_ok];
    [_xxpk_confirmButton addTarget:self action:@selector(xxpk_confirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:_xxpk_confirmButton];
    [_xxpk_confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(bottomView);
        make.width.equalTo(bottomView).multipliedBy(0.5);
    }];

    // TableView
    _xxpk_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _xxpk_tableView.backgroundColor = UIColor.systemGray6Color;
    _xxpk_tableView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
    _xxpk_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _xxpk_tableView.rowHeight = XXGUIDriver.xxpk_data_ui.xxpk_float52;
    _xxpk_tableView.delegate = self;
    _xxpk_tableView.dataSource = self;
    [_xxpk_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"XXGSelectMobileAccountCell"];

    [self.view addSubview:_xxpk_tableView];
    [_xxpk_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(XXGUIDriver.xxpk_data_ui.xxpk_float5);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(bottomView.mas_top);
    }];

    // 默认选中第一行
    if (self.xxpk_model.xxpk_accounts.count > 0) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [_xxpk_tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        _xxpk_selectIdx = 0;
    }
}

#pragma mark - Actions

- (void)xxpk_cancelButtonAction:(UIButton *)sender {
    [[XXGWindowManager shared] xxpk_dismissWindow];
    if (self.xxpk_delegate && [self.xxpk_delegate respondsToSelector:@selector(xxpk_selectMobileAccountCancelButtonDidClick)]) {
        [self.xxpk_delegate xxpk_selectMobileAccountCancelButtonDidClick];
    }
}

- (void)xxpk_confirmButtonAction:(UIButton *)sender {
    [[XXGWindowManager shared] xxpk_dismissWindow];
    if (self.xxpk_delegate && [self.xxpk_delegate respondsToSelector:@selector(xxpk_selectMobileAccountConfirmButtonDidClickWithModel:selectedAccount:)]) {
        NSString *selectedAccount = self.xxpk_model.xxpk_accounts[self.xxpk_selectIdx];
        [self.xxpk_delegate xxpk_selectMobileAccountConfirmButtonDidClickWithModel:self.xxpk_model selectedAccount:selectedAccount];
    }
}

#pragma mark - UITableViewDataSource

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XXGSelectMobileAccountCell" forIndexPath:indexPath];
    cell.backgroundColor = UIColor.whiteColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.xxpk_model.xxpk_accounts[indexPath.row];
    cell.textLabel.textColor = UIColor.darkGrayColor;
    cell.textLabel.font = [UIFont systemFontOfSize:16];

    // 选中状态图标
    if (indexPath.row == self.xxpk_selectIdx) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.tintColor = [XXGUIDriver xxpk_mainColor];
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger previousIdx = self.xxpk_selectIdx;
    self.xxpk_selectIdx = indexPath.row;

    // 刷新之前选中和当前选中的cell
    NSMutableArray *reloadPaths = [NSMutableArray array];
    [reloadPaths addObject:[NSIndexPath indexPathForRow:previousIdx inSection:0]];
    if (previousIdx != indexPath.row) {
        [reloadPaths addObject:indexPath];
    }
    [tableView reloadRowsAtIndexPaths:reloadPaths withRowAnimation:UITableViewRowAnimationNone];
}

@end
