import SwiftUI

struct PostListingView: View {
    var featurePost: Post? = nil
    var posts: [Post]
    var showYear: Bool = false
    var articlesPerAd: Int = 7
    
    private let columns: [GridItem] = [GridItem(.adaptive(minimum: 300))]
    
    private func showAdAtIndex(_ index: Int) -> Bool {
        if articlesPerAd == 0 {
            return false
        }

        let offsetIndex = featurePost == nil ? index + 1 : index + 2
        
        return offsetIndex > 0 && offsetIndex.isMultiple(of: articlesPerAd)
    }
    
    private func adSlotForIndex(_ index: Int) -> GADSlot? {
        guard showAdAtIndex(index) else {
            return nil
        }
        
        let adIndex = index / articlesPerAd
        
        let pool = GADSlot.articleListPool
        
        let adSlotIndex = adIndex % pool.count
        let adSlot = pool[adSlotIndex]
        return adSlot
    }
    
    var body: some View {
        VStack(spacing: 0) {
            if let featurePost = featurePost {
                NavigationLink(destination: PostDetailView(post: featurePost)) {
                    VStack(spacing: 0) {
                        FeaturePostView(post: featurePost)
                            .padding(.bottom, 9)
                        
                        Rectangle()
                            .fill(ColorName.separator.color)
                            .frame(height: 1)
                    }
                    .padding(.horizontal)
                    .padding(.top, 26)
                    .padding(.bottom, 4)
                }
                .buttonStyle(FlatButtonStyle())
                .background(ColorName.background.color)
            }
            
            LazyVGrid(columns: columns, alignment: .center, spacing: 10) {
                ForEach(posts.indices, id: \.self) { postIndex in
                    NavigationLink(destination: PostDetailView(post: posts[postIndex])) {
                        VStack(spacing: 0) {
                            PostSummaryView(post: posts[postIndex], showYear: showYear)
                                .padding(.top, 9)
                                .padding(.bottom, 18)
                            
                            if !showAdAtIndex(postIndex) {
                                Rectangle()
                                    .fill(ColorName.separator.color)
                                    .frame(height: 1)
                            }
                        }
                        .padding(.horizontal)
                    }
                    .buttonStyle(FlatButtonStyle())
                    
                    if let adSlot = adSlotForIndex(postIndex) {
                        VStack(spacing: 0) {
                            Rectangle()
                                .fill(ColorName.separator.color)
                                .frame(height: 1)
                            AdView(adSlot: adSlot)
                            Rectangle()
                                .fill(ColorName.separator.color)
                                .frame(height: 1)
                        }
                        .padding(.vertical, 5)
                    }
                    
                }
            }
            .background(ColorName.background.color)
        }
    }
}

#if DEBUG
struct Previews_PostListingView_Previews: PreviewProvider {
    static var previews: some View {
        PostListingView(posts: Post.previewContent, articlesPerAd: 1)
    }
}
#endif
