import SwiftUI

struct PostSummaryView: View {
    @EnvironmentObject var userService: UserService
    var post: Post
    var showYear: Bool = false
        
    private var dateString: String {
        showYear ? post.longDateAuthorString : post.dateAuthorString
    }
    
    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 body: some View {
        VStack {
            HStack {
                if let topic = post.primaryTopic {
                    TopicLabel(topic: topic)
                }
                Spacer()
                
                Button {
                    toggleBookmark()
                } label: {
                    bookmarkImage
                        .textStyle(.smallIcon, color: ColorName.tint.color)
                        .padding(.horizontal, 10)
                }
                
                if post.commentStatus == .open {
                    NavigationLink {
                        PostDetailView(post: post, viewState: .conversation)
                    } label: {
                        CommentCount(count: post.commentCount)
                    }
                }
            }
            
        HStack(alignment: .top, spacing: 20) {
            
            VStack(alignment: .leading, spacing: 3) {
                
                HStack {
                    Text(post.title.rendered)
                        .textStyle(.headline)
                        .multilineTextAlignment(.leading)
                        .lineLimit(nil)
                        .fixedSize(horizontal: false, vertical: true)
                    Spacer()
                }
                .padding(.bottom, 10)
                
                HStack(alignment: .bottom) {
                    if let author = post.author.first {
                        NavigationLink {
                            AuthorView(authorId: author.id)
                        } label: {
                            Text(dateString)
                                .textStyle(.caption1)
                                .multilineTextAlignment(.leading)
                        }

                    } else {
                        Text(dateString)
                            .textStyle(.caption1)
                            .multilineTextAlignment(.leading)
                    }
                    
                    
                    Spacer()
                    
                    
                }
            }
            
            if let url = post.thumbnail.banner {
                URLImage(url: url)
                    .frame(width: 96, height: 96)
                    .clipped()
                    .clipShape(RoundedRectangle(cornerRadius: 2))
                    .padding(.top, 3)
            }
        }
        }
    }
}

struct TopicLabel: View {
    var topic: Topic
    
    var body: some View {
        NavigationLink {
            TopicPostsView(topic: topic)
        } label: {
            Text(topic.name)
                .textStyle(.topic, color: topic.foregroundColor)
                .textCase(.uppercase)
                .padding(.horizontal, 8)
                .padding(.vertical, 4.5)
                .background(
                    Capsule()
                        .fill(topic.backgroundColor)
                )
        }
        .buttonStyle(FlatButtonStyle())
    }
}

struct CommentCount: View {
    var count: Int
    
    var body: some View {
        HStack(spacing: 3) {
            ImageName.conversation.image
                .textStyle(.smallIcon, color: ColorName.tint.color)
            
            Text("\(count)")
                .textStyle(.caption1, color: ColorName.tint.color)
        }
    }
}

#if DEBUG
struct PostSummaryView_Previews: PreviewProvider {
    
    static var previews: some View {
        PostSummaryView(post: Post.previewContent[0])
    }
}
#endif
