//
//  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 let markWidth = k_ScreenWidth/4
    private lazy var markView: UIView = {
        let bgView = UIView()
        let imageView = UIImageView.createImageView(imageName: "tabbar_select_mark")
        bgView.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.top.centerX.equalToSuperview()
            make.width.equalTo(24)
            make.height.equalTo(8)
        }
        return bgView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        createUI()
        setUpData()
    }

    @objc private func setUpData() {
        viewControllerArray = ["TSHomeVC", "TSEditLiveVC", "TSRandomWallpaperVC", "TSMineVC"]

        selectedImageArray = [
            "tabbar_select_home",
            "tabbar_select_edit",
            "tabbar_select_random",
            "tabbar_select_mine"
        ]
        unselectedImageArray = [
            "tabbar_unSelect_home",
            "tabbar_unSelect_edit",
            "tabbar_unSelect_random",
            "tabbar_unSelect_mine"
        ]

        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
    }

    private func createUI() {
        tabBar.barStyle = .black
        tabBar.isTranslucent = true
        
        
        if #available(iOS 13.0, *) {
            let appearance = UITabBarAppearance()
            appearance.backgroundEffect = UIBlurEffect(style: .dark) // 使用高斯模糊
            tabBar.standardAppearance = appearance

            if #available(iOS 15.0, *) {
                tabBar.scrollEdgeAppearance = appearance // 针对滚动边缘的外观
            }
        }
        
        tabBar.addSubview(markView)
        markView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.width.equalTo(markWidth)
            make.height.equalTo(8)
        }
    }

    func updateMarkViewFrame(){
        markView.snp.updateConstraints { make in
            make.left.equalTo(CGFloat(selectedIndex) * markWidth)
        }
    }
    
    private func tabBarItem(title: String, image: UIImage?, selectedImage: UIImage?, tag: Int) -> UITabBarItem {
        let tabBarItem = UITabBarItem()
        tabBarItem.image = image?.withRenderingMode(.alwaysOriginal)
        tabBarItem.selectedImage = selectedImage?.withRenderingMode(.alwaysOriginal)
        tabBarItem.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -8, right: 0) // 向下移动图标
        return tabBarItem
    }

    var currentNav: UINavigationController? {
        if selectedIndex < (viewControllers?.count ?? 0) {
            return viewControllers?[selectedIndex] as? UINavigationController
        }
        return nil
    }
    
    deinit {
        debugPrint("TSTabBarController deinit")
        NotificationCenter.default.removeObserver(self)
    }
}

extension TSTabBarController: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController){
        updateMarkViewFrame()
    }
}
