import Localization
import SwiftUI

public struct VersioningPill: View {
    @Environment(\.colorScheme) var colorScheme

    public enum DisplayState: Equatable {
        case loading
        case v3_5
        case v4
    }

    let state: DisplayState
    let hideNonPillModelTag: Bool
    let action: () -> Void

    public init(
        state: DisplayState,
        hideNonPillModelTag: Bool = false,
        action: @escaping () -> Void = {}
    ) {
        self.state = state
        self.hideNonPillModelTag = hideNonPillModelTag
        self.action = action
    }

    public var body: some View {
        ZStack {
            switch state {
            case .loading:
                Color.clear
                    .frame(width: Constants.loadingPillSize, height: Constants.loadingPillSize)
                    .overlay {
                        ProgressView()
                            .progressViewStyle(.circular)
                    }

            case .v3_5, .v4:
                Button {
                    action()
                } label: {
                    HStack {
                        ancillaryTextView

                        Text(primaryPillText)
                            .typographyV1(.body1)
                            .foregroundStyle(textColor)
                            .padding(.leading, leadingPadding)
                            .padding(.trailing, trailingPadding)
                            .padding(.bottom, 3)
                            .background(pillBackgroundView)
                    }
                }
                .buttonStyle(.plain)
            }
        }
    }
}

private extension VersioningPill {
    enum Constants {
        static let loadingPillSize: CGFloat = 20.0
        static let pillCornerRadius: CGFloat = 4.0
        static let pillLineWidth: CGFloat = 0.5
        static let v4Copy: String = "v4"
        static let v3Dot5Copy: String = "v3.5"
        static var upgradeToV4Copy: String {
            return "\(L10n.FeatureBrandedAlert.upgradeTo) \(v4Copy)"
        }
    }

    var isDarkMode: Bool {
        return colorScheme == .dark
    }

    var leadingPadding: CGFloat {
        switch state {
        case .loading:
            return .zero
        case .v3_5:
            return 8.0
        case .v4:
            return 6
        }
    }

    var trailingPadding: CGFloat {
        switch state {
        case .loading:
            return .zero
        case .v3_5:
            return 7.5
        case .v4:
            return 4.5
        }
    }

    var textColor: Color {
        if isDarkMode {
            Color.SemanticV1.v4Accent
        } else {
            Color.SemanticV1.textPrimary
        }
    }

    var primaryPillText: String {
        switch state {
        case .loading:
            return ""
        case .v3_5:
            return Constants.upgradeToV4Copy
        case .v4:
            return Constants.v4Copy
        }
    }

    @ViewBuilder
    var ancillaryTextView: some View {
        if hideNonPillModelTag {
            EmptyView()
        } else {
            switch state {
            case .loading, .v4:
                EmptyView()
            case .v3_5:
                Text(Constants.v3Dot5Copy)
                    .typographyV1(.body1)
                    .foregroundStyle(Color.SemanticV1.textPrimary.opacity(0.5))
            }
        }
    }

    @ViewBuilder
    var pillBackgroundView: some View {
        if isDarkMode {
            RoundedRectangle(cornerRadius: Constants.pillCornerRadius)
                .stroke(Color.SemanticV1.v4Accent, lineWidth: Constants.pillLineWidth)
        } else {
            RoundedRectangle(cornerRadius: Constants.pillCornerRadius)
                .fill(Color.SemanticV1.v4Accent)
        }
    }
}
