//
//  SelectedEventView.swift
//  damus
//
//  Created by William Casarin on 2023-01-23.
//

import SwiftUI

struct SelectedEventView: View {
    let damus: DamusState
    let event: NostrEvent
    let size: EventViewKind

    @Environment(\.colorScheme) var colorScheme

    var pubkey: Pubkey {
        event.pubkey
    }

    @StateObject var bar: ActionBarModel

    /// Whether to apply sepia styling to the entire view (for longform articles)
    var useSepia: Bool {
        event.known_kind == .longform && damus.settings.longform_sepia_mode
    }

    init(damus: DamusState, event: NostrEvent, size: EventViewKind) {
        self.damus = damus
        self.event = event
        self.size = size
        self._bar = StateObject(wrappedValue: make_actionbar_model(ev: event.id, damus: damus))
    }

    var body: some View {
        HStack(alignment: .top) {
            VStack(alignment: .leading) {
                HStack {
                    EventProfile(damus_state: damus, pubkey: pubkey, size: .normal)
                    
                    Spacer()
                    
                    EventMenuContext(damus: damus, event: event)
                        .padding([.bottom], 4)
                }
                .padding(.horizontal)
                .minimumScaleFactor(0.75)
                .lineLimit(1)
                
                ReplyPart(events: damus.events, event: event, keypair: damus.keypair, ndb: damus.ndb)
                    .padding(.horizontal)
                
                ProxyView(event: event)
                    .padding(.top, 5)
                    .padding(.horizontal)

                EventBody(damus_state: damus, event: event, size: size, options: [.wide])

                Mention
                
                HStack {
                    Text(verbatim: "\(format_date(created_at: event.created_at))")
                        .font(.footnote)
                        .foregroundColor(.gray)
                    if let clientTag = event.clientTag {
                        Text(String(format: NSLocalizedString("via %@", comment: "Label indicating which client published the event"), clientTag.name))
                            .font(.footnote)
                            .foregroundColor(.gray)
                            .lineLimit(1)
                            .truncationMode(.tail)
                    }
                }
                .padding([.top, .leading, .trailing])
                
                Divider()
                    .padding([.bottom], 4)
                
                if !bar.is_empty {
                    EventDetailBar(state: damus, target: event.id, target_pk: event.pubkey)
                        .padding(.horizontal)
                    Divider()
                }
                
                EventActionBar(damus_state: damus, event: event)
                    .padding([.top], 4)
                    .padding(.horizontal)

                Divider()
                    .padding([.top], 4)
            }
            .onReceive(handle_notify(.update_stats)) { target in
                guard target == self.event.id else { return }
                Task { await self.bar.update(damus: self.damus, evid: target) }
            }
            .compositingGroup()
        }
        // Apply sepia background to outer HStack for full width coverage
        // Note: foregroundStyle intentionally NOT applied here to preserve UI element contrast
        // (buttons, icons, timestamps). Article text gets sepia styling via EventBody.
        .background(useSepia ? DamusColors.sepiaBackground(for: colorScheme) : Color.clear)
    }
    
    var Mention: some View {
        Group {
            if let mention = first_eref_mention_with_hints(ndb: damus.ndb, ev: event, keypair: damus.keypair) {
                MentionView(damus_state: damus, mention: .note(mention.noteId, index: mention.index), relayHints: mention.relayHints)
                    .padding(.horizontal)
            }
        }
    }
}

struct SelectedEventView_Previews: PreviewProvider {
    static var previews: some View {
        SelectedEventView(damus: test_damus_state, event: test_note, size: .selected)
    }
}
