//
//  BaseTabBarController.m
//  keyBoard
//
//  Created by Mac on 2025/10/29.
//

#import "BaseTabBarController.h"
#import "HomeVC.h"
#import "HomeMainVC.h"
#import "MyVC.h"
#import "KBShopVC.h"
#import "KBCommunityVC.h"

#import "BaseNavigationController.h"
#import "KBAuthManager.h"
@interface BaseTabBarController ()

@end

@implementation BaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupTabbarAppearance];

    // 组装 4 个 Tab：首页 / 商城 / 社区 / 我的
    // 图标位于 Assets.xcassets/Tabbar 下：tab_home/tab_home_selected 等

    // 首页
    HomeMainVC *home = [[HomeMainVC alloc] init];
    BaseNavigationController *navHome = [[BaseNavigationController alloc] initWithRootViewController:home];
    navHome.tabBarItem = [self tabItemWithTitle:@"首页"
                                        image:@"tab_home"
                                  selectedImg:@"tab_home_selected"];

    // 商城
    KBShopVC *shop = [[KBShopVC alloc] init];
    BaseNavigationController *navShop = [[BaseNavigationController alloc] initWithRootViewController:shop];
    navShop.tabBarItem = [self tabItemWithTitle:@"商城"
                                        image:@"tab_shop"
                                  selectedImg:@"tab_shop_selected"];

    // 社区
    KBCommunityVC *community = [[KBCommunityVC alloc] init];
    community.title = @"社区";
    BaseNavigationController *navCommunity = [[BaseNavigationController alloc] initWithRootViewController:community];
    navCommunity.tabBarItem = [self tabItemWithTitle:@"社区"
                                             image:@"tab_shequ"
                                       selectedImg:@"tab_shequ_selected"];

    // 我的
    MyVC *my = [[MyVC alloc] init];
    BaseNavigationController *navMy = [[BaseNavigationController alloc] initWithRootViewController:my];
    navMy.tabBarItem = [self tabItemWithTitle:@"我的"
                                       image:@"tab_my"
                                 selectedImg:@"tab_my_selected"];

    self.viewControllers = @[navHome, navShop, navCommunity, navMy];
    
    // 测试储存Token
//    [[KBAuthManager shared] saveAccessToken:@"TEST" refreshToken:nil expiryDate:[NSDate dateWithTimeIntervalSinceNow:3600] userIdentifier:nil];
    [[KBAuthManager shared] signOut];
}

- (void)setupTabbarAppearance{
    // 让 TabBar 不透明，子控制器 view 不再延伸到 TabBar 下
      self.tabBar.translucent = NO;
      if (@available(iOS 15.0, *)) {
          UITabBarAppearance *a = [UITabBarAppearance new];
          [a configureWithOpaqueBackground];
          a.backgroundColor = [UIColor whiteColor];
          // 设置选中标题为黑色（iOS 15+ 需通过 appearance）
          NSDictionary *selAttr = @{ NSForegroundColorAttributeName : [UIColor blackColor] };
          a.stackedLayoutAppearance.selected.titleTextAttributes = selAttr;
          a.inlineLayoutAppearance.selected.titleTextAttributes = selAttr;
          a.compactInlineLayoutAppearance.selected.titleTextAttributes = selAttr;
          self.tabBar.standardAppearance = a;
          self.tabBar.scrollEdgeAppearance = a;
      } else if (@available(iOS 13.0, *)) {
          UITabBarAppearance *a = [UITabBarAppearance new];
          [a configureWithOpaqueBackground];
          a.backgroundColor = [UIColor whiteColor];
          NSDictionary *selAttr = @{ NSForegroundColorAttributeName : [UIColor blackColor] };
          a.stackedLayoutAppearance.selected.titleTextAttributes = selAttr;
          a.inlineLayoutAppearance.selected.titleTextAttributes = selAttr;
          a.compactInlineLayoutAppearance.selected.titleTextAttributes = selAttr;
          self.tabBar.standardAppearance = a;
          self.tabBar.tintColor = [UIColor blackColor];
      } else {
          // 老系统用 tintColor/appearance 做回退
          self.tabBar.tintColor = [UIColor blackColor];
          [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}
                                                   forState:UIControlStateSelected];
      }
}

// 统一构造 TabBarItem，原图渲染，避免被系统 Tint 着色
- (UITabBarItem *)tabItemWithTitle:(NSString *)title image:(NSString *)imageName selectedImg:(NSString *)selectedName {
    UIImage *img = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *sel = [[UIImage imageNamed:selectedName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:title image:img selectedImage:sel];
    return item;
}

/*
#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
