//
//  SQTooltip.swift
//  AlloVoisinsSwiftUI
//
//  Created by Victor on 25/11/2024.
//

import SwiftUI

struct SQTooltip: View {
    let text: String
    @Binding var isVisible: Bool
    @State private var tooltipHeight: CGFloat = 0

    private struct Constants {
        static let cornerRadius: CGFloat = 8
        static let padding: CGFloat = 16
        static let maxWidth: CGFloat = 300
        static let spacing: CGFloat = 4
    }

    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            SQText(text)
                .sqColor(.white)
                .fixedSize(horizontal: false, vertical: true)
                .padding(Constants.padding)
                .background(
                    GeometryReader { geo in
                        Color.sqNeutral(100)
                            .clipShape(RoundedRectangle(cornerRadius: Constants.cornerRadius))
                            .onAppear {
                                tooltipHeight = geo.size.height
                            }
                    }
                )
                .frame(maxWidth: Constants.maxWidth)
        }
        .position(x: 210, y: 0)
        .offset(y: -(tooltipHeight/2 + Constants.spacing))
        .opacity(isVisible ? 1 : 0)
        .animation(.easeInOut(duration: 0.2), value: isVisible)
    }
}
