//
//  KBLoginPopView.m
//  自定义登录弹窗内容视图（配合 LSTPopView 使用）
//
//  要点：
//  - 使用 Masonry 进行布局，支持 iOS 13+ 的 ASAuthorizationAppleIDButton
//  - 中文注释，懒加载子视图
//

#import "KBLoginPopView.h"
#import <Masonry/Masonry.h>
#import <AuthenticationServices/AuthenticationServices.h>

@interface KBLoginPopView ()
@property (nonatomic, strong) UILabel *titleLabel;   // 标题
@property (nonatomic, strong) UILabel *descLabel;    // 副标题/说明
@property (nonatomic, strong) UIButton *closeButton; // 右上角关闭
@property (nonatomic, strong) UIView *appleContainer; // Apple 按钮容器（iOS13- 时显示占位）
@end

@implementation KBLoginPopView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 16.0;
        self.layer.masksToBounds = YES;
        [self setupUI];
    }
    return self;
}

#pragma mark - UI

// 构建视图层级与约束
- (void)setupUI {
    [self addSubview:self.titleLabel];
    [self addSubview:self.descLabel];
    [self addSubview:self.appleContainer];
    [self addSubview:self.closeButton];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(18);
        make.left.equalTo(self).offset(16);
        make.right.equalTo(self).offset(-16);
    }];
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(6);
        make.left.right.equalTo(self.titleLabel);
    }];
    [self.appleContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(16);
        make.right.equalTo(self).offset(-16);
        make.top.equalTo(self.descLabel.mas_bottom).offset(20);
        make.height.mas_equalTo(52);
        make.bottom.equalTo(self).offset(-24); // 决定整体高度
    }];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(6);
        make.right.equalTo(self).offset(-6);
        make.width.height.mas_equalTo(36);
    }];

    // 放置 Apple 登录按钮或占位
    if (@available(iOS 13.0, *)) {
        ASAuthorizationAppleIDButton *btn = [ASAuthorizationAppleIDButton new];
        [btn addTarget:self action:@selector(onTapApple) forControlEvents:UIControlEventTouchUpInside];
        [self.appleContainer addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.appleContainer);
        }];
    } else {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"需要 iOS13+ 才能使用 Apple 登录" forState:UIControlStateNormal];
        btn.enabled = NO;
        btn.layer.cornerRadius = 8.0;
        btn.layer.borderWidth = 1.0;
        btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [self.appleContainer addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.appleContainer);
        }];
    }
}

#pragma mark - Actions

// 点击“用 Apple 登录”
- (void)onTapApple {
    if (self.appleLoginHandler) self.appleLoginHandler();
}

// 点击关闭按钮
- (void)onTapClose {
    if (self.closeHandler) self.closeHandler();
}

#pragma mark - Lazy

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.text = @"登录后可使用全部功能";
        _titleLabel.font = [UIFont boldSystemFontOfSize:18];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.numberOfLines = 1;
    }
    return _titleLabel;
}

- (UILabel *)descLabel {
    if (!_descLabel) {
        _descLabel = [UILabel new];
        _descLabel.text = @"我们将使用 Apple 进行快速安全登录";
        _descLabel.font = [UIFont systemFontOfSize:14];
        _descLabel.textColor = [UIColor colorWithWhite:0.2 alpha:0.8];
        _descLabel.textAlignment = NSTextAlignmentCenter;
        _descLabel.numberOfLines = 0;
    }
    return _descLabel;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_closeButton setTitle:@"✕" forState:UIControlStateNormal];
        [_closeButton setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
        _closeButton.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
        [_closeButton addTarget:self action:@selector(onTapClose) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

- (UIView *)appleContainer {
    if (!_appleContainer) {
        _appleContainer = [UIView new];
    }
    return _appleContainer;
}

@end

