//
//  ExplorerView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/09.
//

import SwiftUI
import Kingfisher

struct ExplorerView: View {
    
    @StateObject var viewModel = ExplorerViewModel()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                HStack(spacing: 0) {
                    Image("ic_title_search_black")
                    
                    TextField("채널명을 입력해 보세요", text: $viewModel.channel)
                        .autocapitalization(.none)
                        .disableAutocorrection(true)
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color(hex: "eeeeee"))
                        .accentColor(Color(hex: "3bb9f1"))
                        .keyboardType(.default)
                        .padding(.horizontal, 13.3)
                    
                    if !viewModel.channel.isEmpty {
                        Image("ic_close_white")
                            .resizable()
                            .frame(width: 20, height: 20)
                            .onTapGesture {
                                viewModel.channel = ""
                            }
                    }
                }
                .padding(.horizontal, 21.3)
                .frame(width: screenSize().width - 26.7, height: 50)
                .background(Color(hex: "222222"))
                .overlay(
                    RoundedRectangle(cornerRadius: 6.7)
                        .strokeBorder(lineWidth: 1)
                        .foregroundColor(Color(hex: "bbbbbb"))
                )
                .padding(.top, 20)
                
                ZStack {
                    if viewModel.channel.count > 1 {
                        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(hex: "eeeeee"))
                                            
                                            Spacer()
                                        }
                                        .frame(width: screenSize().width - 26.7)
                                        .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, 40)
                            }
                        }
                        .padding(.vertical, 40)
                        .padding(.horizontal, 26.7)
                    } else {
                        ScrollView(.vertical, showsIndicators: false) {
                            VStack(spacing: 26.7) {
                                ForEach(0..<viewModel.explorerSections.count, id: \.self) { index in
                                    let section = viewModel.explorerSections[index]
                                    if !section.creators.isEmpty {
                                        ExplorerSectionView(section: section)
                                    }
                                }
                            }
                            .padding(.vertical, 40)
                            .padding(.horizontal, 26.7)
                        }
                    }
                }
            }
            .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(hex: "3bb9f1"))
                            .foregroundColor(Color.white)
                            .multilineTextAlignment(.center)
                            .cornerRadius(20)
                            .padding(.top, 66.7)
                        Spacer()
                    }
                }
            }
            .onAppear {
                viewModel.getExplorer()
            }
            .onTapGesture {
                hideKeyboard()
            }
        }
    }
}

struct ExplorerView_Previews: PreviewProvider {
    static var previews: some View {
        ExplorerView()
    }
}