import SwiftUI

struct FeaturePostView: View {
    @EnvironmentObject var userService: UserService
        
    private func toggleBookmark() {
        var bookmarks = userService.userBookmarks
        if userService.userBookmarks.contains(post.id) {
            bookmarks = bookmarks.filter { $0 != post.id }
        } else {
            bookmarks.append(post.id)
        }
        
        Task {
            try await userService.updateBookmarks(postIds: bookmarks)
        }
    }
    
    private var bookmarkImage: Image {
        userService.userBookmarks.contains(post.id) ? ImageName.System.bookmarkFill.image : ImageName.System.bookmark.image
    }
    
    var post: Post
    
    var body: some View {
        
        VStack(alignment: .leading, spacing: 3) {
            
            ZStack(alignment: .topLeading) {
                if let url = post.thumbnail.banner {
                    
                    URLImage(url: url)
                        .aspectRatio(1/0.525, contentMode: .fill)
                        .clipped()
                        .clipShape(RoundedRectangle(cornerRadius: 2))
                }
                
                HStack {
                    if let topic = post.primaryTopic {
                        TopicLabel(topic: topic)
                    }
                    Spacer()
                    
                    Button {
                        toggleBookmark()
                    } label: {
                        bookmarkImage
                            .textStyle(.smallIcon, color: ColorName.tint.color)
                            .padding(.horizontal, 10)
                            .padding(.vertical, 2)
                            .background(
                                Capsule()
                                    .fill(ColorName.background.color)
                            )
                            .padding(.horizontal, 5)
                    }
                    
                    if post.commentStatus == .open {
                        NavigationLink {
                            PostDetailView(post: post, viewState: .conversation)
                        } label: {
                            CommentCount(count: post.commentCount)
                                .padding(.horizontal, 10)
                                .padding(.vertical, 2)
                                .background(
                                    Capsule()
                                        .fill(ColorName.background.color)
                                )
                        }
                    }
                }
                .padding(10)
            }
            
            
            .padding(.bottom, 10)
            
            HStack {
                Text(post.title.rendered)
                    .textStyle(.headline)
                    .multilineTextAlignment(.leading)
                    .lineLimit(nil)
                Spacer()
            }
            .padding(.top, 2)
            .padding(.bottom, 2)
            
            HStack(alignment: .bottom) {
                if let author = post.author.first {
                    NavigationLink {
                        AuthorView(authorId: author.id)
                    } label: {
                        Text(post.dateAuthorString)
                            .textStyle(.caption1)
                            .multilineTextAlignment(.leading)
                    }

                } else {
                    Text(post.dateAuthorString)
                        .textStyle(.caption1)
                        .multilineTextAlignment(.leading)
                }
                
                Spacer()
            }
            
            Spacer()
        }
    }
}

#if DEBUG
struct FeaturePostView_Previews: PreviewProvider {
    static var previews: some View {
        ScrollView {
            VStack {
                FeaturePostView(post: Post.previewContent.first!)
            }
            .padding(.horizontal)
        }
    }
}
#endif
