//
//  SelectRecipientView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/10.
//

import SwiftUI
import Kingfisher

struct SelectRecipientView: View {
    
    @ObservedObject var viewModel = SelectRecipientViewModel()
    
    @Binding var isShowing: Bool
    let selectUser: (GetRoomDetailUser) -> Void
    
    var body: some View {
        BaseView {
            VStack(spacing: 20) {
                DetailNavigationBar(title: "받는 사람 검색") {
                    isShowing = false
                }
                
                TextField("닉네임을 입력해주세요", text: $viewModel.searchNickname)
                    .autocapitalization(.none)
                    .disableAutocorrection(true)
                    .font(.custom(Font.medium.rawValue, size: 13.3))
                    .foregroundColor(Color(hex: "eeeeee"))
                    .keyboardType(.default)
                    .padding(.horizontal, 13.3)
                    .frame(width: screenSize().width - 26.7, height: 50)
                    .background(Color(hex: "232323"))
                    .cornerRadius(10)
                
                ScrollView(.vertical, showsIndicators: false) {
                    LazyVStack(spacing: 26.7) {
                        ForEach(viewModel.users, id: \.self) { user in
                            HStack(spacing: 13.3) {
                                KFImage(URL(string: user.profileImageUrl))
                                    .resizable()
                                    .scaledToFill()
                                    .frame(width: 46.7, height: 46.7, alignment: .top)
                                    .clipped()
                                    .cornerRadius(23.3)
                                
                                Text(user.nickname)
                                    .font(.custom(Font.medium.rawValue, size: 13.3))
                                    .foregroundColor(Color(hex: "eeeeee"))
                                
                                Spacer()
                            }
                            .contentShape(Rectangle())
                            .onTapGesture {
                                selectUser(user)
                                isShowing = false
                            }
                        }
                        .frame(width: screenSize().width - 26.7)
                    }
                }
                .frame(width: screenSize().width)
            }
        }
        .onAppear {
            viewModel.searchUser()
        }
    }
}

struct SelectRecipientView_Previews: PreviewProvider {
    static var previews: some View {
        SelectRecipientView(isShowing: .constant(true)) { _ in }
    }
}