//
//  SearchChannelView.swift
//  SodaLive
//
//  Created by klaus on 1/9/25.
//

import SwiftUI
import Kingfisher

struct SearchChannelView: View {
    
    @StateObject var viewModel = ExplorerViewModel()
    @State private var isFocused: Bool = false
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                DetailNavigationBar(title: "채널 탐색")
                
                HStack(spacing: 0) {
                    Image("ic_title_search_black")
                    
                    FocusedTextField(
                        text: $viewModel.channel,
                        isFirstResponder: isFocused
                    )
                    .padding(.horizontal, 13.3)
                    .onAppear {
                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
                            isFocused = true
                        }
                    }
                    
                    if !viewModel.channel.isEmpty {
                        Image("ic_close_white")
                            .resizable()
                            .frame(width: 20, height: 20)
                            .onTapGesture {
                                viewModel.channel = ""
                            }
                    }
                }
                .padding(.horizontal, 21.3)
                .frame(height: 50)
                .frame(maxWidth: .infinity)
                .background(Color.gray22)
                .overlay(
                    RoundedRectangle(cornerRadius: 6.7)
                        .strokeBorder(lineWidth: 1)
                        .foregroundColor(Color.graybb)
                )
                .padding(.horizontal, 13.3)
                
                ScrollView(.vertical, showsIndicators: false) {
                    if viewModel.channelResponses.count > 0 {
                        VStack(spacing: 26.7) {
                            ForEach(viewModel.channelResponses, id: \.self) { channel in
                                HStack(spacing: 13.3) {
                                    KFImage(URL(string: channel.profileImageUrl))
                                        .cancelOnDisappear(true)
                                        .downsampling(size: CGSize(width: 46.7, height: 46.7))
                                        .resizable()
                                        .frame(width: 46.7, height: 46.7)
                                        .clipShape(Circle())
                                    
                                    Text(channel.nickname)
                                        .font(.custom(Font.medium.rawValue, size: 13.3))
                                        .foregroundColor(Color.grayee)
                                    Spacer()
                                }
                                .frame(maxWidth: .infinity)
                                .contentShape(Rectangle())
                                .onTapGesture {
                                    AppState.shared.setAppStep(step: .creatorDetail(userId: channel.id))
                                }
                            }
                        }
                    } else {
                        Text("검색 결과가 없습니다.")
                            .font(.custom(Font.medium.rawValue, size: 18.3))
                            .foregroundColor(.white)
                            .padding(.top, 20)
                    }
                }
                .padding(.vertical, 20)
                .padding(.horizontal, 13.3)
            }
            .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
                GeometryReader { geo in
                    HStack {
                        Spacer()
                        Text(viewModel.errorMessage)
                            .padding(.vertical, 13.3)
                            .frame(width: geo.size.width - 66.7, alignment: .center)
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .background(Color.button)
                            .foregroundColor(Color.white)
                            .multilineTextAlignment(.center)
                            .cornerRadius(20)
                            .padding(.top, 66.7)
                        Spacer()
                    }
                }
            }
            .onAppear {
                viewModel.channel = UserDefaults.string(forKey: .searchChannel)
            }
            .onTapGesture {
                hideKeyboard()
            }
        }
    }
}

#Preview {
    SearchChannelView()
}
