//
//  RootTabBarVC.swift
//  Funny_sounds
//
//  Created by 忆海16 on 2024/8/14.
//

import UIKit

class RootTabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let collectVC = FS_CollectVC()
        collectVC.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "collect"), selectedImage: UIImage(named: "collect"))
        collectVC.tabBarItem.imageInsets = UIEdgeInsets(top: 15, left: 0, bottom: -15, right: 0)

        let homeVC = FS_HomeVC()
        homeVC.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "home"), selectedImage: UIImage(named: "home"))
        homeVC.tabBarItem.imageInsets = UIEdgeInsets(top: 15, left: 0, bottom: -15, right: 0)

        let settingVC = FS_SettingVC()
        settingVC.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "setting"), selectedImage: UIImage(named: "setting"))
        settingVC.tabBarItem.imageInsets = UIEdgeInsets(top: 15, left: 0, bottom: -15, right: 0)
        
        let recording = testRecording()
        recording.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "setting"), selectedImage: UIImage(named: "setting"))
        recording.tabBarItem.imageInsets = UIEdgeInsets(top: 15, left: 0, bottom: -15, right: 0)

        viewControllers = [collectVC, homeVC ,recording,settingVC]
        self.delegate = self

        // 使用自定义的 CustomTabBar
        let customTabBar = CustomTabBar()
        setValue(customTabBar, forKey: "tabBar")
        
        customTabBar.backgroundImage = UIImage(named: "tabbarbg")
        customTabBar.backgroundColor = UIColor.hexSting(color: "#003954", alpha: 1)
        
        self.selectedIndex = 1
        
    }
    
    
//    override func viewDidLayoutSubviews() {
//        super.viewDidLayoutSubviews()
//
//        // 更新背景视图的位置和大小
//        if let customTabBar = tabBar as? CustomTabBar {
//            customTabBar.backgroundImage = UIImage(named: "tabbarbg")
//            customTabBar.backgroundColor = UIColor.hexSting(color: "#003954", alpha: 1)
//        }
//    }
    
}

extension 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
    }
}
