import SwiftUI

struct PostHeaderView: View {
    @EnvironmentObject var userService: UserService
        
    var summary: PostSummary
    var onCommentStatusTap: (() -> Void)?
    
    private func toggleBookmark() {
        var bookmarks = userService.userBookmarks
        if userService.userBookmarks.contains(summary.id) {
            bookmarks = bookmarks.filter { $0 != summary.id }
        } else {
            bookmarks.append(summary.id)
        }
        
        Task {
            try await userService.updateBookmarks(postIds: bookmarks)
        }
    }
    
    private var bookmarkImage: Image {
        userService.userBookmarks.contains(summary.id) ? ImageName.System.bookmarkFill.image : ImageName.System.bookmark.image
    }
    
    var body: some View {
        VStack(alignment: .leading, spacing: 6) {
            HStack(alignment: .center) {
                if let topic = summary.primaryTopic {
                    TopicLabel(topic: topic)
                }
                
                Spacer()
                
                Button {
                    toggleBookmark()
                } label: {
                    bookmarkImage
                        .textStyle(.smallIcon, color: ColorName.tint.color)
                        .padding(.horizontal, 10)
                }
                
                if summary.commentStatus == .open {
                    if let onCommentStatusTap = onCommentStatusTap {
                        Button {
                            onCommentStatusTap()
                        } label: {
                            CommentCount(count: summary.commentCount)
                        }
                    } else {
                        CommentCount(count: summary.commentCount)
                    }
                }
            }
            .padding(.bottom, 13)
            
            HStack {
                Text(summary.title)
                    .textStyle(.title3)
                    .multilineTextAlignment(.leading)
                
                Spacer()
            }
            
            HStack(alignment: .center) {
                if let authorId = summary.author.first?.id {
                    NavigationLink {
                        AuthorView(authorId: authorId)
                    } label: {
                        Text(summary.dateAuthorString)
                            .textStyle(.caption1)
                            .multilineTextAlignment(.leading)
                    }
                    .buttonStyle(FlatButtonStyle())
                } else {
                    Text(summary.dateAuthorString)
                        .textStyle(.caption1)
                        .multilineTextAlignment(.leading)
                }
                
                Spacer()
                
            }
        }
    }
}

#if DEBUG
struct PostHeaderView_Previews: PreviewProvider {
    static var previews: some View {
        PostHeaderView(summary: Post.previewContent[0].summary)
    }
}
#endif
