//
//  TSTabBarController.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//


import UIKit

class TSTabBarController: UITabBarController {
    private var viewControllerArray: [String] = []
    private var titleArray: [String] = []
    private var selectedImageArray: [String] = []
    private var unselectedImageArray: [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        createUI()
        setUpData()
    }

    @objc private func setUpData() {
        viewControllerArray = ["TSLightVC", "TSMirrorVC", "TSFlashVC", "TSSettingVC"]

        selectedImageArray = [
            "tabbar_select_light",
            "tabbar_select_mirror",
            "tabbar_select_flash",
            "tabbar_select_setting"
        ]
        
        unselectedImageArray = [
            "tabbar_unSelect_light",
            "tabbar_unSelect_mirror",
            "tabbar_unSelect_flash",
            "tabbar_unSelect_setting"
        ]
        
        titleArray = [
            "Light",
            "Mirror",
            "Flash",
            "Setting"
        ]

        var tabArray: [UINavigationController] = []

        for i in 0..<viewControllerArray.count {
            if let rootViewController = viewControllerArray[i].toInstance(of: UIViewController.self) {
                let nav = TSBaseNavigationC(rootViewController: rootViewController)
                nav.view.backgroundColor = "#050505".color
                nav.tabBarItem = tabBarItem(
                    title: titleArray[i].localized,
                    image: UIImage(named: unselectedImageArray[i]),
                    selectedImage: UIImage(named: selectedImageArray[i]),
                    tag: i
                )
                tabArray.append(nav)
            }
        }

        self.viewControllers = tabArray
    }

    private func createUI() {
        tabBar.isTranslucent = false
        tabBar.backgroundColor = "#050505".color
        tabBar.backgroundImage = UIImage.colorFrom(color: .black, size: CGSize(width: k_ScreenWidth, height: k_TabBar_Height))
        
        // 自定义 TabBarItem 字体颜色和选中颜色
        UITabBar.appearance().unselectedItemTintColor = UIColor.textAssist // 未选中颜色
        UITabBar.appearance().tintColor = UIColor.white // 选中颜色
    }

    private func tabBarItem(title: String, image: UIImage?, selectedImage: UIImage?, tag: Int) -> UITabBarItem {
        let tabBarItem = UITabBarItem()
        tabBarItem.title = title
        tabBarItem.image = image?.withRenderingMode(.alwaysOriginal)
        tabBarItem.selectedImage = selectedImage?.withRenderingMode(.alwaysOriginal)
        tabBarItem.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -4, right: 0) // 向下移动图标
        tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4) // 向上移动标题
        
        // 设置 TabBarItem1 样式
//        tabBarItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 14), .foregroundColor: UIColor.gray], for: .normal)
//        tabBarItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 14), .foregroundColor: UIColor.red], for: .selected)
        
        return tabBarItem
    }

    var currentNav: UINavigationController? {
        if selectedIndex < (viewControllers?.count ?? 0) {
            return viewControllers?[selectedIndex] as? UINavigationController
        }
        return nil
    }
    
    deinit {
        debugPrint("TSTabBarController deinit")
        NotificationCenter.default.removeObserver(self)
    }
}
