import APIClient
import ComponentLibrary
import ComposableArchitecture
import HookActionsClient
import Localization
import SwiftUI

public enum ReportType: String, Equatable, Codable {
    case spam
    case adultContent
    case selfPromotion
    case intellectualProperty
}

@Reducer
public struct ReportHookInappropriateReducer {
    @ObservableState
    public struct State: Equatable, Identifiable {
        public let id = UUID()
        public let hook: Hook
        public var selectedReportType: ReportType = .spam

        public init(hook: Hook) {
            self.hook = hook
        }
    }

    public enum Action {
        case dismiss
        case selectInappropriateType(ReportType)
        case reportTapped
    }

    @Dependency(\.dismiss) var dismiss
    @Dependency(\.hookActionsClient) private var hookActionsClient

    public init() {}

    public var body: some ReducerOf<Self> {
        Reduce<State, Action> { state, action in
            switch action {
            case .dismiss:
                return .run { _ in await self.dismiss() }

            case .selectInappropriateType(let type):
                state.selectedReportType = type
                return .none

            case .reportTapped:
                hookActionsClient.reportInappropriate(state.hook, state.selectedReportType.rawValue)
                return .send(.dismiss)
            }
        }
    }
}

public struct ReportHookInappropriateSheet: View {
    let darkTrait = UITraitCollection(userInterfaceStyle: .dark)

    private func resolveColorForDarkTrait(_ color: Color) -> Color {
        Color(UIColor(color).resolvedColor(with: darkTrait))
    }

    private var backgroundColor: Color {
        return resolveColorForDarkTrait(resolveColorForDarkTrait(Color.SemanticV2.backgroundSmokeThin))
    }

    let store: StoreOf<ReportHookInappropriateReducer>

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

    public var body: some View {
        VStack(spacing: 0) {
            title
            actionButtons
            reportButton
        }
        .padding(12)
        .presentationCornerRadius(28, conditional: true)
        .presentationBackground {
            Rectangle()
                .fill(.ultraThinMaterial)
                .overlay(backgroundColor)
        }
        .presentationDragIndicator(.visible)
        .selfSizingPresentation()
    }

    @ViewBuilder
    private var title: some View {
        Text(L10n.FeatureHooks.reportInappropriate)
            .typographyV1(.reportButtonFont)
            .foregroundStyle(Color.SemanticV2.foregroundPrimary)
            .padding(.vertical, 16)
    }

    @ViewBuilder
    private var actionButtons: some View {
        VStack(spacing: 4) {
            SheetRow(
                title: L10n.FeatureHooks.spam,
                isSelected: store.selectedReportType == .spam,
                action: { store.send(.selectInappropriateType(.spam))
                }
            )
            SheetRow(
                title: L10n.FeatureHooks.inappropriate,
                isSelected: store.selectedReportType == .adultContent,
                action: { store.send(.selectInappropriateType(.adultContent))
                }
            )
            SheetRow(
                title: L10n.FeatureHooks.selfPromotion,
                isSelected: store.selectedReportType == .selfPromotion,
                action: { store.send(.selectInappropriateType(.selfPromotion))
                }
            )
            SheetRow(
                title: L10n.FeatureHooks.intellectualProperty,
                isSelected: store.selectedReportType == .intellectualProperty,
                action: { store.send(.selectInappropriateType(.intellectualProperty))
                }
            )
        }
        .padding(.bottom, 24)
    }

    @ViewBuilder
    private var reportButton: some View {
        Button {
            store.send(.reportTapped)
        } label: {
            Text(L10n.FeatureHooks.report)
                .typographyV1(.reportButtonFont)
                .foregroundStyle(Color.SemanticV2.backgroundPrimary)
                .frame(maxWidth: .infinity)
                .padding(12)
                .background(Color.SemanticV2.foregroundPrimary)
                .cornerRadius(40)
        }
    }
}

public struct SheetRow: View {
    let title: String
    let isSelected: Bool
    let action: () -> Void

    public init(title: String, isSelected: Bool, action: @escaping () -> Void) {
        self.title = title
        self.isSelected = isSelected
        self.action = action
    }

    public var body: some View {
        Button {
            action()
        } label: {
            HStack(spacing: 12) {
                RadioButton(isSelected: isSelected)

                Text(title)
                    .typographyV1(.button.kerning(0.28))
                    .foregroundStyle(Color.SemanticV2.foregroundPrimary)
                Spacer()
            }
            .padding(.vertical, 16)
            .padding(.horizontal, 12)
        }
    }
}

private extension TypographyV1 {
    static let reportButtonFont: TypographyV1 = .init(
        name: "Report Button Font",
        size: 16,
        style: .body,
        weight: .ppNeueMontrealMedium,
        lineHeight: 24
    )
}
