//
//  ChevronView.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 02/05/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//

import SwiftUI

struct ChevronView: View {

    @Binding var rotate: Bool

    var body: some View {
        Button(action: {
            self.rotate.toggle()
        }) {
            Image(systemName: "chevron.right.circle")
                .imageScale(.large)
                .rotationEffect(.degrees(rotate ? 270 : 90))
                .animation(.easeInOut, value: rotate)
                .padding(.vertical)
        }
    }
}

struct ContextInfoButton: View {
    let title: String
    let message: String
    let popoverWidth: CGFloat

    @State private var showsPopover = false

    init(
        title: String,
        message: String,
        popoverWidth: CGFloat = 280
    ) {
        self.title = title
        self.message = message
        self.popoverWidth = popoverWidth
    }

    var body: some View {
        Button {
            showsPopover.toggle()
        } label: {
            Image(systemName: "info.circle")
                .font(.body.weight(.semibold))
                .foregroundColor(.secondary)
        }
        .buttonStyle(.plain)
        .accessibilityLabel("\(title) info")
        .popover(isPresented: $showsPopover, arrowEdge: .top) {
            VStack(alignment: .leading, spacing: 10) {
                Text(title)
                    .font(.headline)
                Text(message)
                    .font(.body)
                    .fixedSize(horizontal: false, vertical: true)
            }
            .padding(16)
            .frame(width: popoverWidth, alignment: .leading)
        }
    }
}

struct ContextInfoHeader: View {
    let title: String
    let message: String

    var body: some View {
        HStack(spacing: 8) {
            Text(title)
            Spacer(minLength: 0)
            ContextInfoButton(title: title, message: message)
        }
    }
}
