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

import SwiftUI

struct FollowerListView: View {
    
    let userId: Int
    @StateObject var viewModel = FollowerListViewModel()
    
    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(hex: "eeeeee"))
                    
                    Text("\(viewModel.totalCount)")
                        .font(.custom(Font.medium.rawValue, size: 18.3))
                        .foregroundColor(Color(hex: "9970ff"))
                    
                    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(userId: $0)
                                },
                                creatorUnFollow: {
                                    viewModel.creatorUnFollow(userId: $0)
                                }
                            )
                            .onAppear {
                                if index == viewModel.followerListItems.count - 1 {
                                    viewModel.getFollowerList()
                                }
                            }
                        }
                    }
                    .padding(.top, 13.3)
                    .padding(.bottom, 20)
                }
            }
        }
        .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(hex: "9970ff"))
                    .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)
    }
}