//
//  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] = []
    
    private var interceptVC: UINavigationController = UINavigationController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        createUI()
        setUpData()
    }

    @objc private func setUpData() {
        viewControllerArray = ["TSEmojisVC", "TSGenmojiVC","TSAIChatContainerVC", "TSWallpaperVC"]
        titleArray = ["Emojis","Genmoji","AIChat","Wallpaper"]
        selectedImageArray = [
            "tabbar_select_emoji",
            "tabbar_select_aiemoji",
            "tabbar_select_aichat",
            "tabbar_select_keyboard"
        ]
        unselectedImageArray = [
            "tabbar_unSelect_emoji",
            "tabbar_unSelect_aiemoji",
            "tabbar_unSelect_aichat",
            "tabbar_unSelect_keyboard"
        ]

        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 = UIColor.black
                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
        interceptVC = tabArray.last!
    }

    private func createUI() {
        tabBar.barStyle = .black
        tabBar.isTranslucent = true
        
        
        tabBar.isTranslucent = false
        tabBar.backgroundColor = "#262626".uiColor
        tabBar.backgroundImage = UIImage.colorFrom(color: tabBar.backgroundColor!, size: CGSize(width: k_ScreenWidth, height: k_TabBar_Height))
        
        // 自定义 TabBarItem 字体颜色和选中颜色
        UITabBar.appearance().unselectedItemTintColor = .white.withAlphaComponent(0.4)// 未选中颜色
        UITabBar.appearance().tintColor = UIColor.themeColor // 选中颜色
        
        delegate = self
    }

    private func tabBarItem(title: String, image: UIImage?, selectedImage: UIImage?, tag: Int) -> UITabBarItem {
        let tabBarItem = UITabBarItem()
        tabBarItem.image = image?.withRenderingMode(.alwaysOriginal)
        tabBarItem.title = title
        tabBarItem.selectedImage = selectedImage?.withRenderingMode(.alwaysOriginal)
//        tabBarItem.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -8, right: 0) // 向下移动图标
        return tabBarItem
    }

    deinit {
        debugPrint("TSTabBarController deinit")
        NotificationCenter.default.removeObserver(self)
    }
    

}


extension TSTabBarController:UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool{
        if interceptVC == viewController{
            kPresentModalVC(target: self, modelVC: TSWallpaperVC())
            return false
        }
        return true
    }
}
