//
//  TooltipDemoView.swift
//  lichunWebsocket
//
//  Demo view for testing and showcasing the tooltip system
//  This can be used for development testing and removed later
//

import SwiftUI

struct TooltipDemoView: View {
    @StateObject private var tooltipManager = TooltipManager()
    @State private var showDemoControls = true

    var body: some View {
        ZStack {
            AppColors.background
                .ignoresSafeArea()

            VStack(spacing: AppSpacing.lg) {
                Text("Tooltip System Demo")
                    .font(.appLargeTitle)
                    .foregroundColor(AppColors.primaryText)
                    .padding(.top, AppSpacing.xl)

                Spacer()

                // Demo elements with tooltips
                VStack(spacing: AppSpacing.xl) {
                    // Top tooltip demo
                    demoButton(
                        title: "Show Top Tooltip",
                        tooltipId: "demo_top",
                        tooltipTitle: "Top Position",
                        tooltipMessage: "This tooltip appears above the element with an arrow pointing down.",
                        position: .top
                    )

                    // Bottom tooltip demo
                    demoButton(
                        title: "Show Bottom Tooltip",
                        tooltipId: "demo_bottom",
                        tooltipTitle: "Bottom Position",
                        tooltipMessage: "This tooltip appears below the element with an arrow pointing up.",
                        position: .bottom
                    )

                    // Leading tooltip demo
                    demoButton(
                        title: "Show Leading Tooltip",
                        tooltipId: "demo_leading",
                        tooltipTitle: "Leading Position",
                        tooltipMessage: "This tooltip appears to the left of the element.",
                        position: .leading
                    )

                    // Trailing tooltip demo
                    demoButton(
                        title: "Show Trailing Tooltip",
                        tooltipId: "demo_trailing",
                        tooltipTitle: "Trailing Position",
                        tooltipMessage: "This tooltip appears to the right of the element.",
                        position: .trailing
                    )

                    // Long message demo
                    demoButton(
                        title: "Show Long Message",
                        tooltipId: "demo_long",
                        tooltipTitle: "Energy System",
                        tooltipMessage: "Actions cost energy. When you run out of energy, you can refill it using diamonds, or wait for it to regenerate naturally over time. Managing your energy wisely is key to progress!",
                        position: .top
                    )
                }

                Spacer()

                // Control buttons
                if showDemoControls {
                    VStack(spacing: AppSpacing.sm) {
                        Button(action: {
                            tooltipManager.resetAllTooltips()
                        }) {
                            Text("Reset All Tooltips")
                                .font(.appBody)
                                .foregroundColor(AppColors.primaryText)
                                .frame(maxWidth: .infinity)
                                .frame(height: AppSpacing.smallButtonHeight)
                                .background(AppColors.cardBackground)
                                .cornerRadius(AppSpacing.smallCornerRadius)
                        }

                        Text("Seen tooltips: \(countSeenTooltips())")
                            .font(.appCaption)
                            .foregroundColor(AppColors.secondaryText)
                    }
                    .padding(.horizontal, AppSpacing.md)
                }
            }
            .padding(AppSpacing.md)

            // Tooltip overlay
            if let tooltip = tooltipManager.activeTooltip {
                Color.black.opacity(0.4)
                    .ignoresSafeArea()
                    .onTapGesture {
                        tooltipManager.dismissTooltip()
                    }

                TooltipView(tooltip: tooltip) {
                    tooltipManager.dismissTooltip()
                }
                .transition(.scale.combined(with: .opacity))
                .zIndex(100)
            }
        }
    }

    private func demoButton(
        title: String,
        tooltipId: String,
        tooltipTitle: String,
        tooltipMessage: String,
        position: TooltipPosition
    ) -> some View {
        Button(action: {
            tooltipManager.showTooltipIfNeeded(
                tooltipId,
                title: tooltipTitle,
                message: tooltipMessage,
                position: position
            )
        }) {
            HStack {
                Text(title)
                    .font(.appBodyBold)
                    .foregroundColor(AppColors.primaryText)

                Spacer()

                if tooltipManager.hasSeenTooltip(tooltipId) {
                    Image(systemName: "checkmark.circle.fill")
                        .foregroundColor(AppColors.success)
                }
            }
            .padding(AppSpacing.md)
            .frame(maxWidth: .infinity)
            .background(AppColors.cardBackground)
            .cornerRadius(AppSpacing.cornerRadius)
        }
        .padding(.horizontal, AppSpacing.md)
    }

    private func countSeenTooltips() -> Int {
        let demoIds = ["demo_top", "demo_bottom", "demo_leading", "demo_trailing", "demo_long"]
        return demoIds.filter { tooltipManager.hasSeenTooltip($0) }.count
    }
}

// MARK: - Preview

struct TooltipDemoView_Previews: PreviewProvider {
    static var previews: some View {
        TooltipDemoView()
    }
}
