//
//  FollowCreatorView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/19.
//

import SwiftUI

struct FollowCreatorView: View {
    
    @StateObject var viewModel = FollowCreatorViewModel()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                DetailNavigationBar(title: "팔로잉 채널리스트")
                
                HStack(spacing: 0) {
                    Text("총  ")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color(hex: "eeeeee"))
                    
                    Text("\(viewModel.totalCount)")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color(hex: "dd4500"))
                    
                    Text(" 명")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color(hex: "eeeeee"))
                    
                    Spacer()
                }
                .padding(.horizontal, 13.3)
                
                ScrollView(.vertical, showsIndicators: false) {
                    VStack(spacing: 13.3) {
                        ForEach(0..<viewModel.creatorList.count, id: \.self) { index in
                            let creator = viewModel.creatorList[index]
                            
                            FollowCreatorItemView(
                                creator: creator,
                                onClickFollow: { viewModel.creatorFollow(userId: $0) },
                                onClickUnFollow: { viewModel.creatorUnFollow(userId: $0) }
                            )
                                .padding(.horizontal, 20)
                                .onTapGesture {
                                    AppState.shared
                                        .setAppStep(step: .creatorDetail(userId: creator.creatorId))
                                }
                                .onAppear {
                                    if index == viewModel.creatorList.count - 1 {
                                        viewModel.getFollowedCreatorAllList()
                                    }
                                }
                        }
                    }
                    .padding(.top, 13.3)
                }
            }
            .onAppear {
                viewModel.getFollowedCreatorAllList()
            }
            .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()
                }
            }
        }
    }
}