//
//  NW_RootTabBarVC.swift
//  wallpaper_BProject
//
//  Created by 忆海16 on 2024/8/28.
//

import UIKit

class NW_RootTabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let home = NW_HomeVC()
        home.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "home_n"), selectedImage: UIImage(named: "home_s"))
//        home.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)

        let rank = NW_RankingVC()
        rank.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "ranking_n"), selectedImage: UIImage(named: "ranking_s"))
//        rank.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)

        let community = NW_CommunityVC()
        community.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "community_n"), selectedImage: UIImage(named: "community_s"))
//        community.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        
        
        let mine = NW_MineVC()
        mine.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "mine_n"), selectedImage: UIImage(named: "mine_s"))
//        mine.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)

        viewControllers = [home, rank ,community,mine]
        self.delegate = self

        // 设置TabBar字体颜色
        let normalAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor.gray,
            .font: UIFont.systemFont(ofSize: 14, weight: .medium)
        ]
        
        let selectedAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor.hexSting(color: "#2c2c2c", alpha: 1),
            .font: UIFont.boldSystemFont(ofSize: 14)
        ]
        
        UITabBarItem.appearance().setTitleTextAttributes(normalAttributes, for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes(selectedAttributes, for: .selected)
        
        tabBar.barTintColor = UIColor.hexSting(color: "#F9F5F0", alpha: 1)
        tabBar.shadowImage = UIImage()
        tabBar.backgroundImage = UIImage()

    }
    
}
extension NW_RootTabBarVC: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if let tabBarItems = tabBarController.tabBar.items, let selectedViewControllerIndex = tabBarController.viewControllers?.firstIndex(of: viewController) {
            let selectedItem = tabBarItems[selectedViewControllerIndex]
            // 仅对选定项目应用缩放动画
            let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
            scaleAnimation.duration = 0.2
            scaleAnimation.fromValue = 0.9
            scaleAnimation.toValue = 1.1
            scaleAnimation.autoreverses = true
            scaleAnimation.repeatCount = 1

            // 将动画应用于所选项目的视图
            if let selectedView = selectedItem.value(forKey: "view") as? UIView {
                selectedView.layer.add(scaleAnimation, forKey: nil)
            }
        }
        return true
    }
}
