//
//  UITabBarController+Extension.swift
//  QuickSearchLocation
//
//  Created by mac on 2024/4/10.
//

import UIKit

extension UITabBarController {
    
    /// 设置 tabbar 的样式
    func addChildViewController(childVc: UIViewController,
                                title: String,
                                image: String,
                                selectedImage: String,
                                imageInsets: UIEdgeInsets,
                                titlePosition: UIOffset,
                                index: Int)
    {
        self .configureChildViewController(childVc: childVc, title: title, image: image, selectedImage: selectedImage, imageInsets: imageInsets, titlePosition: titlePosition, index: index)
        let nav:QSLBaseNavController = QSLBaseNavController.init(rootViewController: childVc)
        self.addChild(nav)
    }
    
    func configureChildViewController(childVc: UIViewController,
                                      title: String,
                                      image: String,
                                      selectedImage: String,
                                      imageInsets: UIEdgeInsets,
                                      titlePosition: UIOffset,
                                      index: Int) {
        
        childVc.tabBarItem.tag = index
        // 同时设置tabbar和navigationBar的文字
        childVc.title = title
        childVc.tabBarItem.title = title
        // 设置子控制器的图片
        childVc.tabBarItem.image = UIImage(named: image)
        //声明显示图片的原始式样 不要渲染
        childVc.tabBarItem.selectedImage = UIImage(named: selectedImage)?.withRenderingMode(.alwaysOriginal)
        childVc.tabBarItem.imageInsets = imageInsets
        childVc.tabBarItem.titlePositionAdjustment = titlePosition
        
        // 设置文字颜色
        var selectedDict:[NSAttributedString.Key : Any] = [NSAttributedString.Key : Any]()
        selectedDict[NSAttributedString.Key.foregroundColor] = QSLColor.themeMainColor;
        childVc.tabBarItem.setTitleTextAttributes(selectedDict, for: .selected)
        self.tabBar.tintColor = QSLColor.themeMainColor;
        
        var normalDict:[NSAttributedString.Key : Any] = [NSAttributedString.Key : Any]()
        normalDict[NSAttributedString.Key.foregroundColor] = QSLColor.Color_888
        normalDict[NSAttributedString.Key.font] = UIFont.textF(10)
        childVc.tabBarItem.setTitleTextAttributes(normalDict, for: .normal)

        if #available(iOS 13.0, *) {
            childVc.tabBarController?.tabBar.unselectedItemTintColor = UIColor.clear
        }
    }
}
