//
//  XXGSendCodeButton.m
//  XXGPlayKit
//
//  Created by apple on 2025/3/16.
//

#import "XXGSendCodeButton.h"
#import "XXGUIDriver.h"
#import "UIImage+XXGImage.h"

@interface XXGSendCodeButton ()

// 内部定时器，用于倒计时
@property (nonatomic, strong) NSTimer *xxpk_countdownTimer;
// 当前剩余的倒计时秒数
@property (nonatomic, assign) NSInteger xxpk_remainingSeconds;
// 记录按钮初始标题
@property (nonatomic, copy) NSString *xxpk_originalTitle;

@end

@implementation XXGSendCodeButton

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self xxpk_commonInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self xxpk_commonInit];
    }
    return self;
}

/// 公共初始化方法
- (void)xxpk_commonInit {
    // 默认倒计时为60秒
    self.xxpk_countdownDuration = 60;
    self.xxpk_originalTitle = XXGUIDriver.xxpk_string_ui.xxpk_sendVerificationCode;
    [self setTitle:self.xxpk_originalTitle forState:UIControlStateNormal];
    [self setBackgroundImage:[UIImage xxpk_imageWithColor:XXGUIDriver.xxpk_mainColor] forState:UIControlStateNormal];
    [self setBackgroundImage:[UIImage xxpk_imageWithColor:[[UIColor lightGrayColor] colorWithAlphaComponent:0.5f]] forState:UIControlStateHighlighted];
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    self.layer.cornerRadius = 2.f;
    self.layer.masksToBounds = YES;
    // 如果需要增加内边距，可以设置 contentEdgeInsets
    self.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
    // 调整按钮尺寸以适应标题和内边距
    [self sizeToFit];
    
    // 添加点击事件
    [self addTarget:self action:@selector(xxpk_buttonClicked) forControlEvents:UIControlEventTouchUpInside];
}

/// 点击按钮时调用，触发发送验证码回调并启动倒计时
- (void)xxpk_buttonClicked {
    [self xxpk_startCountdown];
    if (self.xxpk_sendCodeAction) {
        self.xxpk_sendCodeAction();
    }
}

/// 开始倒计时，按钮不可点击
- (void)xxpk_startCountdown {
    self.enabled = NO;
    self.xxpk_remainingSeconds = self.xxpk_countdownDuration;
    [self xxpk_updateButtonTitle];
    
    // 每秒触发一次
    self.xxpk_countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                                 target:self
                                                               selector:@selector(xxpk_timerFired:)
                                                               userInfo:nil
                                                                repeats:YES];
}

/// 定时器触发时调用，更新剩余时间
- (void)xxpk_timerFired:(NSTimer *)timer {
    self.xxpk_remainingSeconds--;
    if (self.xxpk_remainingSeconds <= 0) {
        [self xxpk_stopCountdown];
    } else {
        [self xxpk_updateButtonTitle];
    }
}

/// 更新按钮标题，显示倒计时状态
- (void)xxpk_updateButtonTitle {
    NSString *title = [NSString stringWithFormat:@"%@(%ld)",XXGUIDriver.xxpk_string_ui.xxpk_reSendVerificationCode, (long)self.xxpk_remainingSeconds];
    [self setTitle:title forState:UIControlStateDisabled];
}

/// 停止倒计时，恢复按钮可点击状态及原始标题
- (void)xxpk_stopCountdown {
    [self.xxpk_countdownTimer invalidate];
    self.xxpk_countdownTimer = nil;
    self.enabled = YES;
    [self setTitle:self.xxpk_originalTitle forState:UIControlStateNormal];
}

- (void)dealloc {
    NSLog(@"%s", __func__);
    [self.xxpk_countdownTimer invalidate];
    self.xxpk_countdownTimer = nil;
}

@end
