//
//  MeterConnectionActionView.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 29/03/2026.
//  Co-authored-by: GPT-5.3-Codex.
//  Copyright © 2026 Bogdan Timofte. All rights reserved.
//

import SwiftUI

struct MeterConnectionActionView: View {
    let operationalState: Meter.OperationalState
    let compact: Bool

    var body: some View {
        if operationalState == .notPresent {
            HStack(spacing: 10) {
                Image(systemName: "exclamationmark.triangle.fill")
                    .foregroundColor(.orange)
                Text("Not found at this time.")
                    .fontWeight(.semibold)
                Spacer()
            }
            .padding(compact ? 12 : 16)
            .meterCard(tint: .orange, fillOpacity: 0.14, strokeOpacity: 0.18)
        }
    }
}

struct MeterConnectionToolbarButton: View {
    let operationalState: Meter.OperationalState
    let showsTitle: Bool
    let connectAction: () -> Void
    let disconnectAction: () -> Void

    private var connected: Bool {
        operationalState >= .peripheralConnectionPending
    }

    private var actionTint: Color {
        connected ? Color(red: 0.66, green: 0.39, blue: 0.35) : Color(red: 0.20, green: 0.46, blue: 0.43)
    }

    private var title: String {
        connected ? "Disconnect" : "Connect"
    }

    private var systemImage: String {
        connected ? "link.badge.minus" : "bolt.horizontal.circle.fill"
    }

    var body: some View {
        if operationalState != .notPresent {
            Button(action: {
                if connected {
                    disconnectAction()
                } else {
                    connectAction()
                }
            }) {
                if showsTitle {
                    Label(title, systemImage: systemImage)
                } else {
                    Image(systemName: systemImage)
                }
            }
            .foregroundStyle(actionTint)
            .accessibilityLabel(title)
            .help(title)
        }
    }
}
