import ComponentLibrary
import ComposableArchitecture
import Localization
import SwiftUI

@Reducer
public struct Offline {
    @ObservableState
    public struct State: Equatable {
        var isRetrying = false
        public init() {}
    }

    public enum Action: Equatable {
        case delegate(Delegate)

        public enum Delegate: Equatable {
            case retry
        }
    }

    public init() {}

    public var body: some ReducerOf<Self> {
        Reduce { _, action in
            switch action {
            case .delegate:
                // Catch-all
                return .none
            }
        }
    }
}

public struct OfflineScreen: View {
    let store: StoreOf<Offline>

    public init(store: StoreOf<Offline>) {
        self.store = store
    }

    public var body: some View {
        VStack(spacing: 16) {
            Spacer()
            Image.Icon.offline
                .foregroundStyle(Color.SemanticV1.iconTertiary)
            Text(L10n.FeatureApp.offlineMessage)
                .typographyV1(.body2)
                .foregroundStyle(Color.SemanticV1.textSecondary)
                .multilineTextAlignment(.center)
            Button {
                store.send(.delegate(.retry))
            } label: {
                Text(L10n.FeatureApp.retry)
                    .typographyV1(.body1)
                    .foregroundStyle(Color.SemanticV1.textLink)
                    .opacity(store.isRetrying ? 0 : 1)
                    .overlay {
                        ProgressView()
                            .progressViewStyle(.circular)
                            .opacity(store.isRetrying ? 1 : 0)
                    }
            }
            Spacer()
        }
        .padding(.horizontal, 32)
    }
}
