//
//  BaseViewController.m
//  keyBoard
//
//  Created by Mac on 2025/11/7.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@interface BaseViewController ()
@property (nonatomic, strong) UIView *kb_navViewInternal;      // 自定义导航栏容器
@property (nonatomic, strong) UIView *kb_navContentView;       // 44 高度内容区（便于居中）
@property (nonatomic, strong) UILabel *kb_titleLabelInternal;  // 中间标题
@property (nonatomic, strong) UIButton *kb_backButtonInternal; // 返回
@property (nonatomic, strong) MASConstraint *kb_navHeightConstraint; // 高度约束，适配刘海
@end

@implementation BaseViewController {
    BOOL _kb_enableCustomNavBar;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.kb_enableCustomNavBar = true;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self kb_updateCustomNavBarVisibility];
    [self kb_updateBackButtonVisibility];
}


#pragma mark - Custom NavBar

- (void)setKb_enableCustomNavBar:(BOOL)kb_enableCustomNavBar {
    _kb_enableCustomNavBar = kb_enableCustomNavBar;
    if (kb_enableCustomNavBar) { [self kb_buildNavIfNeeded]; }
    [self kb_updateCustomNavBarVisibility];
}

- (BOOL)kb_enableCustomNavBar { return _kb_enableCustomNavBar; }

/// 懒创建自定义导航栏及子视图
- (void)kb_buildNavIfNeeded {
    if (self.kb_navViewInternal.superview) { return; }

    // 容器
    [self.view addSubview:self.kb_navViewInternal];
    CGFloat status = KB_StatusBarHeight();
    [self.kb_navViewInternal mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view);
        self.kb_navHeightConstraint = make.height.mas_equalTo(status + KB_NAVBAR_BASE_HEIGHT);
    }];

    // 内容区（44 高度）
    [self.kb_navViewInternal addSubview:self.kb_navContentView];
    [self.kb_navContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.kb_navViewInternal);
        make.height.mas_equalTo(KB_NAVBAR_BASE_HEIGHT);
    }];

    // 返回按钮
    [self.kb_navContentView addSubview:self.kb_backButtonInternal];
    [self.kb_backButtonInternal mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.kb_navContentView).offset(12);
        make.centerY.equalTo(self.kb_navContentView);
        make.width.height.mas_equalTo(32);
    }];

    // 标题
    [self.kb_navContentView addSubview:self.kb_titleLabelInternal];
    [self.kb_titleLabelInternal mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.kb_navContentView);
        make.centerY.equalTo(self.kb_navContentView);
        make.left.greaterThanOrEqualTo(self.kb_backButtonInternal.mas_right).offset(8);
    }];

    // 初始标题显示为 VC 的 title
    self.kb_titleLabelInternal.text = self.title ?: self.navigationItem.title;
    // 背景色同步
    self.kb_navViewInternal.backgroundColor = [UIColor whiteColor];
}

- (void)kb_updateCustomNavBarVisibility {
    // 根据开关决定是否显示自定义 navView
    self.kb_navViewInternal.hidden = !self.kb_enableCustomNavBar;

    // 适配状态栏高度变化（横竖屏切换）
    if (self.kb_navHeightConstraint) {
        CGFloat status = KB_StatusBarHeight();
        [self.kb_navHeightConstraint uninstall];
        [self.kb_navViewInternal mas_updateConstraints:^(MASConstraintMaker *make) {
            self.kb_navHeightConstraint = make.height.mas_equalTo(status + KB_NAVBAR_BASE_HEIGHT);
        }];
    }
}

- (void)kb_updateBackButtonVisibility {
    BOOL canGoBack = (self.navigationController.viewControllers.count > 1) || self.presentingViewController != nil;
    self.kb_backButtonInternal.hidden = !canGoBack;
}

- (void)setTitle:(NSString *)title {
    [super setTitle:title];
    self.kb_titleLabelInternal.text = title;
}

- (UIView *)kb_navView { return self.kb_navViewInternal; }
- (UILabel *)kb_titleLabel { return self.kb_titleLabelInternal; }
- (UIButton *)kb_backButton { return self.kb_backButtonInternal; }

#pragma mark - Lazy

- (UIView *)kb_navViewInternal {
    if (!_kb_navViewInternal) {
        _kb_navViewInternal = [UIView new];
        _kb_navViewInternal.backgroundColor = [UIColor whiteColor];
    }
    return _kb_navViewInternal;
}

- (UIView *)kb_navContentView {
    if (!_kb_navContentView) {
        _kb_navContentView = [UIView new];
    }
    return _kb_navContentView;
}

- (UILabel *)kb_titleLabelInternal {
    if (!_kb_titleLabelInternal) {
        _kb_titleLabelInternal = [UILabel new];
        _kb_titleLabelInternal.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
        _kb_titleLabelInternal.textColor = [UIColor colorWithHex:0x1B1F1A]; // 深色标题
        _kb_titleLabelInternal.textAlignment = NSTextAlignmentCenter;
    }
    return _kb_titleLabelInternal;
}

- (UIButton *)kb_backButtonInternal {
    if (!_kb_backButtonInternal) {
        _kb_backButtonInternal = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *img = [UIImage imageNamed:@"back"]; // 资源名 back_black_icon 内含 back@2x/back@3x
        if (!img) { img = [UIImage imageNamed:@"back_black_icon"]; }
        if (img) { [_kb_backButtonInternal setImage:img forState:UIControlStateNormal]; }
        [_kb_backButtonInternal addTarget:self action:@selector(kb_onBack) forControlEvents:UIControlEventTouchUpInside];
        _kb_backButtonInternal.adjustsImageWhenHighlighted = YES;
        _kb_backButtonInternal.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _kb_backButtonInternal.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return _kb_backButtonInternal;
}

- (void)kb_onBack {
    if (self.navigationController && self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
