//
//  ControlView.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 09/03/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//

import SwiftUI

struct ControlView: View {
    
    @EnvironmentObject private var meter: Meter
    var compactLayout: Bool = false
    var availableSize: CGSize? = nil
    var showsHeader: Bool = true

    var body: some View {
        VStack(alignment: .leading, spacing: 14) {
            if showsHeader {
                HStack {
                    Text("Controls")
                        .font(.headline)
                    Spacer()
                    Text(meter.reportsCurrentScreenIndex ? "Device Screen" : "Page Controls")
                        .font(.caption.weight(.semibold))
                        .foregroundColor(.secondary)
                }
            }

            if compactLayout {
                if usesExpandedCompactLayout {
                    Spacer(minLength: 0)

                    VStack(spacing: 12) {
                        HStack(spacing: 12) {
                            ControlActionButtonView(
                                title: "Prev",
                                symbol: "chevron.left",
                                tint: .indigo,
                                compact: true,
                                isExpandedCompactLayout: usesExpandedCompactLayout,
                                action: { meter.previousScreen() }
                            )

                            ControlCurrentScreenCardView(
                                reportsCurrentScreenIndex: meter.reportsCurrentScreenIndex,
                                currentScreenDescription: meter.currentScreenDescription,
                                isExpandedCompactLayout: usesExpandedCompactLayout
                            )
                                .frame(maxWidth: .infinity, minHeight: 112)
                                .padding(.horizontal, 14)
                                .meterCard(tint: meter.color, fillOpacity: 0.06, strokeOpacity: 0.10)
                        }

                        HStack(spacing: 12) {
                            ControlActionButtonView(
                                title: "Rotate",
                                symbol: "rotate.right.fill",
                                tint: .orange,
                                compact: true,
                                isExpandedCompactLayout: usesExpandedCompactLayout,
                                action: { meter.rotateScreen() }
                            )

                            ControlActionButtonView(
                                title: "Next",
                                symbol: "chevron.right",
                                tint: .indigo,
                                compact: true,
                                isExpandedCompactLayout: usesExpandedCompactLayout,
                                action: { meter.nextScreen() }
                            )
                        }
                    }

                    Spacer(minLength: 0)
                } else {
                    HStack(spacing: 10) {
                        ControlActionButtonView(
                            title: "Prev",
                            symbol: "chevron.left",
                            tint: .indigo,
                            compact: true,
                            isExpandedCompactLayout: usesExpandedCompactLayout,
                            action: { meter.previousScreen() }
                        )

                        ControlCurrentScreenCardView(
                            reportsCurrentScreenIndex: meter.reportsCurrentScreenIndex,
                            currentScreenDescription: meter.currentScreenDescription,
                            isExpandedCompactLayout: usesExpandedCompactLayout
                        )
                            .frame(maxWidth: .infinity, minHeight: 82)
                            .padding(.horizontal, 10)
                            .meterCard(tint: meter.color, fillOpacity: 0.06, strokeOpacity: 0.10)

                        ControlActionButtonView(
                            title: "Rotate",
                            symbol: "rotate.right.fill",
                            tint: .orange,
                            compact: true,
                            isExpandedCompactLayout: usesExpandedCompactLayout,
                            action: { meter.rotateScreen() }
                        )

                        ControlActionButtonView(
                            title: "Next",
                            symbol: "chevron.right",
                            tint: .indigo,
                            compact: true,
                            isExpandedCompactLayout: usesExpandedCompactLayout,
                            action: { meter.nextScreen() }
                        )
                    }
                }
            } else {
                HStack(spacing: 12) {
                    ControlActionButtonView(
                        title: "Prev",
                        symbol: "chevron.left",
                        tint: .indigo,
                        compact: true,
                        isExpandedCompactLayout: usesExpandedCompactLayout,
                        action: { meter.previousScreen() }
                    )

                    ControlCurrentScreenCardView(
                        reportsCurrentScreenIndex: meter.reportsCurrentScreenIndex,
                        currentScreenDescription: meter.currentScreenDescription,
                        isExpandedCompactLayout: usesExpandedCompactLayout
                    )
                    .frame(maxWidth: .infinity, minHeight: 92)
                    .padding(.horizontal, 12)
                    .meterCard(tint: meter.color, fillOpacity: 0.06, strokeOpacity: 0.10)

                    ControlActionButtonView(
                        title: "Next",
                        symbol: "chevron.right",
                        tint: .indigo,
                        compact: true,
                        isExpandedCompactLayout: usesExpandedCompactLayout,
                        action: { meter.nextScreen() }
                    )
                }

                ControlActionButtonView(
                    title: "Rotate Screen",
                    symbol: "rotate.right.fill",
                    tint: .orange,
                    compact: false,
                    isExpandedCompactLayout: usesExpandedCompactLayout,
                    action: { meter.rotateScreen() }
                )
            }
        }
        .frame(maxWidth: .infinity, maxHeight: compactLayout ? .infinity : nil, alignment: .topLeading)
    }

    private var usesExpandedCompactLayout: Bool {
        compactLayout && (availableSize?.height ?? 0) >= 520
    }

}

struct ControlView_Previews: PreviewProvider {
    static var previews: some View {
        ControlView()
    }
}
