//
//  LNMainViewController.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/2.
//

import Foundation
import UIKit


class LNMainViewController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        LNEventDeliver.addObserver(self)
        
//        if #available(iOS 26, *) {
//            let container = tabBar.subviews.first
//            container?.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
//            container?.layer.shadowOffset = .init(width: 0, height: 2)
//            container?.layer.shadowRadius = 6
//            container?.layer.shadowOpacity = 0.8
//            container?.layer.masksToBounds = false
//        } else {
            let tabBar = LNMainTabBar()
            tabBar.delegate = self
            setValue(tabBar, forKey: "tabBar")
//        }
        
        let home = LNHomeViewController()
        home.tabBarItem.image = .icMainHomeDefault.withRenderingMode(.alwaysOriginal)
        home.tabBarItem.selectedImage = .icMainHomeSelected.withRenderingMode(.alwaysOriginal)
        
        let room = LNRoomListViewController()
        room.tabBarItem.image = .icMainRoomDefault.withRenderingMode(.alwaysOriginal)
        room.tabBarItem.selectedImage = .icMainRoomSelected.withRenderingMode(.alwaysOriginal)
        
        let message = LNIMConversationListController()
        message.tabBarItem.image = .icMainChatDefault.withRenderingMode(.alwaysOriginal)
        message.tabBarItem.selectedImage = .icMainChatSelected.withRenderingMode(.alwaysOriginal)
        
        let mine = LNMineViewController()
        mine.tabBarItem.image = .icMainMineDefault.withRenderingMode(.alwaysOriginal)
        mine.tabBarItem.selectedImage = .icMainMineSelected.withRenderingMode(.alwaysOriginal)
        
        viewControllers = [home, room, message, mine]
        
        if let mainTabBar = tabBar as? LNMainTabBar {
            mainTabBar.setupViews()
        }
        
        selectedViewController = message // 触发消息界面加载逻辑
        selectedViewController = home
        
        delegate = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.setNavigationBarHidden(true, animated: animated)
    }
}

extension LNMainViewController: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController is LNHomeViewController { return true }
        
        if !hasLogin {
            LNLoginPanel.show(container: view)
            
        }
        return hasLogin
    }
}

extension LNMainViewController: LNProfileManagerNotify, LNNetworkMonitorNotify {
    func onUserInfoChanged(userInfo: LNUserProfileVO) {
        guard userInfo.userNo.isMyUid else { return }
        
        if !userInfo.isAvailable {
            view.pushToGenderSetup()
        }
    }
}
