//
//  ClockTabBarManager.swift
//  TheNoiseClock
//
//  Created by Matt Bruce on 9/8/25.
//

import SwiftUI

/// Component that manages tab bar visibility for display mode
struct ClockTabBarManager: View {
    
    // MARK: - Properties
    let isDisplayMode: Bool
    let animated: Bool
    
    // MARK: - Body
    var body: some View {
        EmptyView()
            .onAppear {
                setTabBarHidden(isDisplayMode, animated: false)
            }
            .onDisappear {
                setTabBarHidden(false, animated: false)
            }
            .onChange(of: isDisplayMode) { _, newValue in
                setTabBarHidden(newValue, animated: animated)
            }
    }
    
    // MARK: - Private Methods
    private func setTabBarHidden(_ hidden: Bool, animated: Bool) {
        #if canImport(UIKit)
        guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
              let window = windowScene.windows.first,
              let tabBarController = window.rootViewController?.findTabBarController() else { return }
        
        let tabBar = tabBarController.tabBar
        let changes = {
            tabBar.alpha = hidden ? 0 : 1
        }
        if animated {
            UIView.animate(withDuration: AppConstants.AnimationDurations.short, animations: changes)
        } else {
            changes()
        }
        tabBar.isUserInteractionEnabled = !hidden
        #endif
    }
}

// MARK: - Preview
#Preview {
    ClockTabBarManager(isDisplayMode: false, animated: false)
}
