//
//  CreatorCommunityAllView.swift
//  SodaLive
//
//  Created by klaus on 2023/12/15.
//

import SwiftUI

struct CreatorCommunityAllView: View {
    
    let creatorId: Int
    
    @StateObject var viewModel = CreatorCommunityAllViewModel()
    @StateObject var playerManager = CreatorCommunityMediaPlayerManager.shared
    
    var body: some View {
        GeometryReader { proxy in
            BaseView(isLoading: $viewModel.isLoading) {
                VStack(spacing: 0) {
                    DetailNavigationBar(title: "커뮤니티")
                    
                    ScrollView(.vertical, showsIndicators: false) {
                        LazyVStack(spacing: 26.7) {
                            ForEach(0..<viewModel.communityPostList.count, id: \.self) { index in
                                let item = viewModel.communityPostList[index]
                                CreatorCommunityAllItemView(
                                    item: item,
                                    onClickLike: {
                                        viewModel.communityPostLike(postId: item.postId)
                                    },
                                    onClickComment: {
                                        viewModel.postId = item.postId
                                        viewModel.isShowCommentListView = true
                                    },
                                    onClickWriteComment: { comment in
                                        viewModel.createCommunityPostComment(
                                            comment: comment,
                                            postId: item.postId
                                        )
                                    },
                                    onClickShowReportMenu: {
                                        viewModel.postId = item.postId
                                        viewModel.isShowReportMenu = true
                                    },
                                    onClickPurchaseContent: {
                                        viewModel.postId = item.postId
                                        viewModel.postPrice = item.price
                                        viewModel.postIndex = index
                                        viewModel.isShowPostPurchaseView = true
                                    }
                                )
                                .onAppear {
                                    if index == viewModel.communityPostList.count - 1 {
                                        viewModel.getCommunityPostList()
                                    }
                                }
                            }
                        }
                    }
                }
                .sheet(
                    isPresented: $viewModel.isShowCommentListView,
                    content: {
                        CreatorCommunityCommentListView(
                            isPresented: $viewModel.isShowCommentListView,
                            creatorId: creatorId,
                            postId: viewModel.postId
                        )
                    }
                )
                
                ZStack {
                    if viewModel.isShowReportMenu {
                        VStack(spacing: 0) {
                            CreatorCommunityMenuView(
                                isShowing: $viewModel.isShowReportMenu,
                                isShowCreatorMenu: creatorId == UserDefaults.int(forKey: .userId),
                                modifyAction: {
                                    let postId = viewModel.postId
                                    AppState.shared
                                        .setAppStep(
                                            step: .creatorCommunityModify(
                                                postId: postId,
                                                onSuccess: creatorCommunityModifySuccess
                                            )
                                        )
                                    viewModel.postId = 0
                                },
                                deleteAction: {
                                    if creatorId == UserDefaults.int(forKey: .userId) {
                                        viewModel.isShowDeleteConfirm = true
                                    }
                                },
                                reportAction: {
                                    viewModel.isShowReportView = true
                                }
                            )
                            
                            if proxy.safeAreaInsets.bottom > 0 {
                                Rectangle()
                                    .foregroundColor(Color(hex: "222222"))
                                    .frame(width: proxy.size.width, height: 15.3)
                            }
                        }
                        .ignoresSafeArea()
                    }
                    
                    if viewModel.isShowReportView {
                        CreatorCommunityReportView(isShowing: $viewModel.isShowReportView) { reason in
                            viewModel.report(reason: reason)
                        }
                    }
                    
                    if viewModel.isShowDeleteConfirm {
                        SodaDialog(
                            title: "게시물 삭제",
                            desc: "삭제하시겠습니까?",
                            confirmButtonTitle: "삭제",
                            confirmButtonAction: {
                                viewModel.deleteCommunityPost()
                                viewModel.isShowDeleteConfirm = false
                            },
                            cancelButtonTitle: "취소",
                            cancelButtonAction: {
                                viewModel.isShowDeleteConfirm = false
                            }
                        )
                    }
                    
                    if viewModel.isShowPostPurchaseView {
                        CommunityPostPurchaseDialog(
                            isShowing: $viewModel.isShowPostPurchaseView,
                            can: viewModel.postPrice
                        ) {
                            viewModel.purchaseCommunityPost()
                        }
                    }
                    
                    if playerManager.isLoading {
                        LoadingView()
                    }
                }
            }
            .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
                GeometryReader { geo in
                    HStack {
                        Spacer()
                        Text(viewModel.errorMessage)
                            .padding(.vertical, 13.3)
                            .frame(width: screenSize().width - 66.7, alignment: .center)
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .background(Color.button)
                            .foregroundColor(Color.white)
                            .multilineTextAlignment(.center)
                            .cornerRadius(20)
                            .padding(.top, 66.7)
                        Spacer()
                    }
                }
            }
            .popup(isPresented: $playerManager.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
                GeometryReader { geo in
                    HStack {
                        Spacer()
                        Text(playerManager.errorMessage)
                            .padding(.vertical, 13.3)
                            .frame(width: screenSize().width - 66.7, alignment: .center)
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .background(Color.button)
                            .foregroundColor(Color.white)
                            .multilineTextAlignment(.center)
                            .cornerRadius(20)
                            .padding(.top, 66.7)
                        Spacer()
                    }
                }
            }
            .onAppear {
                viewModel.creatorId = creatorId
                viewModel.getCommunityPostList()
            }
            .onDisappear {
                CreatorCommunityMediaPlayerManager.shared.stopContent()
            }
        }
    }
    
    private func creatorCommunityModifySuccess() {
        viewModel.getCommunityPostList()
    }
}

struct CreatorCommunityAllView_Previews: PreviewProvider {
    static var previews: some View {
        CreatorCommunityAllView(creatorId: 0)
    }
}
