//
//  CustomTabBarController.swift
//  QuickSearchLocation
//
//  Created by mac on 2024/4/10.
//

import UIKit

class CustomTabBarController: UITabBarController {
    
    lazy var blurTabBar: UIView = {
       
        let _blurTabBar = UIView(frame: CGRect(x: 0, y: -1, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
        _blurTabBar.isUserInteractionEnabled = true
        self.tabBar.addSubview(_blurTabBar)
        return _blurTabBar
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置 tabbar 样式
        self.setTabBarAppearence()
        // 添加 tab 页
        self.addFoundationTab()
        // 默认进入页面到主页
        self.selectedIndex = 1
    }
    
    func addFoundationTab() {
        
        self.addFriendTab()
        self.addHomeTab()
        self.addCheckTab()
        self.addMineTab()
    }
}

extension CustomTabBarController {
    
    func setTabBarAppearence() {
        
        self.tabBar.backgroundColor = .white
        self.tabBar.shadowImage = UIImage.image(color: .hexStringColor(hexString: "#EEEEEE"), size: CGSize(width: QSLConst.qsl_kScreenW, height: 1))
    }
}

extension CustomTabBarController: UITabBarControllerDelegate {
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        switch item.tag {
        case 0:
            gravityInstance?.track(QSLGravityConst.tab_friend)
            break
        case 1:
            gravityInstance?.track(QSLGravityConst.tab_location)
            break
        case 2:
            gravityInstance?.track(QSLGravityConst.tab_message)
            break
        case 3:
            gravityInstance?.track(QSLGravityConst.tab_mine)
            break
        default:
            break
        }
    }
}

extension CustomTabBarController {
    
    /// 添加 好友 Tab
    func addFriendTab() {
        
        let imageInsets = UIEdgeInsets.zero
        let titlePostion = UIOffset(horizontal: 0, vertical: 0)
        
        let vc = QSLFriendController()
        self.addChildViewController(childVc: vc, title: "好友", image: "tab_friends_normal", selectedImage: "tab_friends_selected", imageInsets: imageInsets, titlePosition: titlePostion, index: 0)
    }
    
    /// 添加 主页定位 Tab
    func addHomeTab() {
        
        let imageInsets = UIEdgeInsets.zero
        let titlePostion = UIOffset(horizontal: 0, vertical: 0)
        
        let vc = QSLHomeController()
        self.addChildViewController(childVc: vc, title: "定位", image: "tab_location_normal", selectedImage: "tab_location_selected", imageInsets: imageInsets, titlePosition: titlePostion, index: 1)
    }
    
    /// 添加 消息 Tab
    func addCheckTab() {
        
        let imageInsets = UIEdgeInsets.zero
        let titlePostion = UIOffset(horizontal: 0, vertical: 0)
        
        let vc = QSLMessageController()
        self.addChildViewController(childVc: vc, title: "消息", image: "tab_message_normal", selectedImage: "tab_message_selected", imageInsets: imageInsets, titlePosition: titlePostion, index: 2)
    }
    
    /// 添加我的 Tab
    func addMineTab() {
        
        let imageInsets = UIEdgeInsets.zero
        let titlePostion = UIOffset(horizontal: 0, vertical: 0)
        
        let vc = QSLMineController()
        self.addChildViewController(childVc: vc, title: "我的", image: "tab_mine_normal", selectedImage: "tab_mine_selected", imageInsets: imageInsets, titlePosition: titlePostion, index: 3)
    }
}
