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

import SwiftUI

struct FollowCreatorView: View {
    
    @StateObject var viewModel = FollowCreatorViewModel()
    
    @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: 0) {
                    Text("총  ")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color.grayee)
                    
                    Text("\(viewModel.totalCount)")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color.mainRed3)
                    
                    Text(" 명")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color.grayee)
                    
                    Spacer()
                }
                .padding(.horizontal, 13.3)
                .padding(.top, 6.7)
                
                if viewModel.totalCount > 0 {
                    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(creatorId: $0, index: index)
                                    },
                                    showCreatorFollowNotifyDialog: {
                                        creatorId = $0
                                        selectedItemIndex = index
                                        isShowFollowNotifyDialog = true
                                    }
                                )
                                .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)
                    }
                } else {
                    Text("팔로우 중인 채널이 없습니다.")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color.grayee)
                        .frame(maxHeight: .infinity)
                }
            }
            .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.button)
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.leading)
                        .cornerRadius(20)
                        .padding(.bottom, 66.7)
                    Spacer()
                }
            }
            
            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
                    }
                )
            }
        }
    }
}