import SwiftUI

struct ServerConnectingIndicator: View {
    enum ServerConnectingIndicatorState {
        case connecting
        case connected
        case error
    }

    @State var state: ServerConnectingIndicatorState

    var body: some View {
        HStack(spacing: 0) {
            switch state {
            case .connecting:
                ProgressView()
                    .scaleEffect(0.7)
                    .foregroundColor(.Main.gray)

            case .connected:
                Image(systemName: "checkmark")
                    .foregroundColor(.Tango.greenLight)
                    .scaleEffect(0.7)

            case .error:
                Image(systemName: "xmark")
                    .foregroundColor(color)
                    .scaleEffect(0.7)
            }
            Text(text)
                .font(.sub1)
                .foregroundColor(color)
            Spacer()
        }
    }

    private var text: String {
        switch state {
        case .connecting:
            return L10n.ServerConnectingIndicator.State.connecting

        case .connected:
            return L10n.ServerConnectingIndicator.State.connected

        case .error:
            return L10n.ServerConnectingIndicator.State.error
        }
    }

    private var color: Color {
        switch state {
        case .connecting:
            return .Main.gray

        case .connected:
            return .Main.black

        case .error:
            return .Tango.redMedium
        }
    }
}

#if DEBUG
    struct ServerConnectingIndicator_Previews: PreviewProvider {
        static var previews: some View {
            ServerConnectingIndicator(state: .error)
                .previewLayout(.fixed(width: 200, height: 40))
        }
    }
#endif
