import ComponentLibrary
import ComposableArchitecture
import Localization
import SwiftUI

@Reducer
public struct RemixAnnouncement {
    public init() {}

    public struct State: Equatable {
        public init() {}

        var remixability = RemixabilityReducer.State()
    }

    public enum Action {
        case delegate(Delegate)
        case remixability(RemixabilityReducer.Action)

        public enum Delegate {
            case dismiss
        }
    }

    @Dependency(\.apiClientV2) private var api

    public var body: some ReducerOf<Self> {
        Scope(state: \.remixability, action: \.remixability) {
            RemixabilityReducer()
        }

        Reduce { _, action in
            switch action {
            case .delegate, .remixability:
                return .none
            }
        }
    }
}

public struct RemixAnnouncementView: View {
    private let store: StoreOf<RemixAnnouncement>

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

    public var body: some View {
        Group {
            VStack(spacing: 8) {
                Group {
                    icon

                    Text(L10n.FeatureRemix.announcementTitle)
                        .typographyV1(.announcementTitle)

                    VStack(spacing: 20) {
                        Text(markdown(L10n.FeatureRemix.announcementDetail1))
                        Text(markdown(L10n.FeatureRemix.announcementDetail2))
                        Text(markdown(L10n.FeatureRemix.announcementDetail3))
                    }
                    .typographyV1(.announcementDetail)
                    .opacity(0.75)
                }
                .foregroundStyle(Color.SemanticV2.foregroundPrimary)

                optionButton(L10n.FeatureRemix.announcementButtonYes, isProminent: true) {
                    store.send(.remixability(.optIntoRemixability(true)))
                }
                .padding(.top, 20)

                optionButton(L10n.FeatureRemix.announcementButtonNo) {
                    store.send(.remixability(.optIntoRemixability(false)))
                }
            }
            .padding(.top, 60)
            .padding(.bottom, 10)
            .padding(.horizontal, 20)
            .multilineTextAlignment(.center)
            .background(
                Image(.announcementBackground)
                    .resizable()
                    .aspectRatio(1, contentMode: .fill)
            )
            .clipShape(.rect(cornerRadius: 32))
            .overlay(alignment: .topTrailing) {
                closeButton
            }
            .padding(.horizontal, 40)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .ignoresSafeArea(.all)
        .background(Color.SemanticV2.backgroundPrimary.opacity(0.75))
        .colorScheme(.dark)
    }

    private func markdown(_ text: String) -> AttributedString {
        (try? AttributedString(markdown: text)) ?? AttributedString(text)
    }

    @ViewBuilder
    private var icon: some View {
        ZStack {
            Image.Icon.remix
                .resizable()
                .frame(width: 40, height: 40)
                .position(x: 20, y: 20)

            NewLabel()
                .foregroundStyle(.black)
                .position(x: 39, y: 34)
        }
        .frame(width: 57, height: 43)
    }

    @ViewBuilder
    private var closeButton: some View {
        Button {
            store.send(.remixability(.optIntoRemixability(false)))
            store.send(.delegate(.dismiss))
        } label: {
            Image.Icon.close
        }
        .padding(10)
        .foregroundStyle(Color.SemanticV2.foregroundPrimary)
        .background(
            Color(red: 0.05, green: 0.03, blue: 0.03)
                .opacity(0.25)
                .blur(radius: 12)
        )
        .clipShape(.circle)
        .padding(16)
    }

    @ViewBuilder
    private func optionButton(_ title: String, isProminent: Bool = false, _ action: @escaping () -> Void) -> some View {
        Button(title, action: action)
            .padding(12)
            .typographyV1(.subtitleMedium)
            .frame(maxWidth: .infinity)
            .foregroundStyle(isProminent ? Color.SemanticV2.backgroundPrimary : Color.SemanticV2.foregroundSecondary)
            .background(isProminent ? Color.SemanticV2.foregroundPrimary : .clear)
            .clipShape(.capsule)
    }
}
