//
//  FollowerListView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/11.
//

import SwiftUI

struct FollowerListView: View {
    
    let userId: Int
    @StateObject var viewModel = FollowerListViewModel()
    
    @State private var isShowFollowNotifyDialog: Bool = false
    @State private var creatorId: Int = 0
    @State private var selectedItemIndex: Int = 0
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                DetailNavigationBar(title: "팔로워 리스트")
                
                HStack(spacing: 4) {
                    Text("전체")
                        .font(.custom(Font.bold.rawValue, size: 18.3))
                        .foregroundColor(Color.grayee)
                    
                    Text("\(viewModel.totalCount)")
                        .font(.custom(Font.medium.rawValue, size: 18.3))
                        .foregroundColor(Color.button)
                    
                    Spacer()
                }
                .padding(.top, 26.7)
                .padding(.horizontal, 13.3)
                
                ScrollView(.vertical, showsIndicators: false) {
                    LazyVStack(spacing: 0) {
                        ForEach(0..<viewModel.followerListItems.count, id: \.self) { index in
                            let item = viewModel.followerListItems[index]
                            FollowerListItemView(
                                item: item,
                                creatorFollow: {
                                    viewModel.creatorFollow(creatorId: $0, index: index)
                                },
                                showCreatorFollowNotifyDialog: {
                                    creatorId = $0
                                    selectedItemIndex = index
                                    isShowFollowNotifyDialog = true
                                }
                            )
                            .onAppear {
                                if index == viewModel.followerListItems.count - 1 {
                                    viewModel.getFollowerList()
                                }
                            }
                        }
                    }
                    .padding(.top, 13.3)
                    .padding(.bottom, 20)
                }
            }
            
            if isShowFollowNotifyDialog {
                CreatorFollowNotifyDialog(
                    isShowing: $isShowFollowNotifyDialog,
                    onClickNotifyAll: {
                        viewModel.creatorFollow(
                            creatorId: creatorId,
                            index: selectedItemIndex,
                            follow: true,
                            notify: true
                        )
                        creatorId = 0
                        selectedItemIndex = -1
                    },
                    onClickNotifyNone: {
                        viewModel.creatorFollow(
                            creatorId: creatorId,
                            index: selectedItemIndex,
                            follow: true,
                            notify: false
                        )
                        creatorId = 0
                        selectedItemIndex = -1
                    },
                    onClickUnFollow: {
                        viewModel.creatorFollow(
                            creatorId: creatorId,
                            index: selectedItemIndex,
                            follow: false,
                            notify: false
                        )
                        creatorId = 0
                        selectedItemIndex = -1
                    }
                )
            }
        }
        .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .bottom, autohideIn: 2) {
            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(.leading)
                    .cornerRadius(20)
                    .padding(.bottom, 66.7)
                Spacer()
            }
        }
        .onAppear {
            viewModel.userId = userId
            viewModel.getFollowerList()
        }
    }
}

struct FollowerListView_Previews: PreviewProvider {
    static var previews: some View {
        FollowerListView(userId: 0)
    }
}
