//  TabBarVC
//
//  Created by MacBook Pro on 14/05/24.
//


import UIKit

class TabBarVC: UITabBarController {
    
    var upperLineView: UIView!
    
    let spacing: CGFloat = 12

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        customizeTabBarItemFont()
        
        UITabBar.appearance().tintColor = #colorLiteral(red: 0.0349480696, green: 0.0643344149, blue: 0.4407724142, alpha: 1)
//        UITabBar.appearance().unselectedItemTintColor = #colorLiteral(red: 0.0349480696, green: 0.0643344149, blue: 0.4407724142, alpha: 1)
        UITabBar.appearance().unselectedItemTintColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tabBar.frame.size.height = 95
        tabBar.frame.origin.y = view.frame.height - 95
    }
    
    // function to set tab bar item font
    func customizeTabBarItemFont() {
        // Set the font attributes
        let attributes = [NSAttributedString.Key.font: FontCustom.shareInstance.customFont(fontName: .Exo2_Bold, size: 12)]
        
        // Apply the attributes to the appearance proxy of UITabBarItem
        UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)
    }
    
    // function to customize tab bar item icon size
    func customizeTabBarItemIconSize() {
        // Get a reference to the tab bar controller
        if let tabBarController = self.tabBarController {
            // Loop through each tab bar item
            for item in tabBarController.tabBar.items! {
                // Adjust the image insets to increase the icon size
                item.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -8, right: 0)
            }
        }
    }
    
    ///Add tabbar item indicator uper line
//    func addTabbarIndicatorView(index: Int, isFirstTime: Bool = false){
//        guard let tabView = tabBar.items?[index].value(forKey: "view") as? UIView else {
//            return
//        }
//        if !isFirstTime{
//            upperLineView.removeFromSuperview()
//        }
//        upperLineView = UIView(frame: CGRect(x: tabView.frame.minX + spacing, y: tabView.frame.minY + 0.1, width: tabView.frame.size.width - spacing * 2, height: 4))
//        upperLineView.backgroundColor = UIColor.systemPink
//        tabBar.addSubview(upperLineView)
//    }

}

extension TabBarVC: UITabBarControllerDelegate {
    
    // Implement the tabBarController(_:didSelect:) method to handle tab changes
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        // This method will be called whenever a tab is selected
            // Do something based on the selected index
        print("Selected tab index: \(tabBarController.selectedIndex)")
        
        if tabBarController.selectedIndex == 1{
            
        }
    }
    
    // Implement the tabBarController(_:shouldSelect:) method to control tab selection
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        
        if viewController == tabBarController.viewControllers?[1] {
            let sb = UIStoryboard(name: "Home", bundle: nil)
            let vc = sb.instantiateViewController(withIdentifier: "ExploreWokaVC") as! ExploreWokaVC
            vc.modalPresentationStyle = .overCurrentContext
            vc.modalTransitionStyle = .crossDissolve
            present(vc, animated: true, completion: nil)
            return false
        }
        // Return false to prevent tab selection
        return true
    }
}
