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

import SwiftUI
import Kingfisher

struct EventListView: View {
    
    @ObservedObject var viewModel = EventListViewModel()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                DetailNavigationBar(title: "이벤트")
                
                ScrollView(.vertical, showsIndicators: false) {
                    VStack(spacing: 0) {
                        ForEach(viewModel.events, id: \.self) { event in
                            KFImage(URL(string: event.thumbnailImageUrl))
                                .cancelOnDisappear(true)
                                .downsampling(
                                    size: CGSize(
                                        width: screenSize().width - 26.7,
                                        height: (screenSize().width - 26.7) * 300 / 1000
                                    )
                                )
                                .resizable()
                                .scaledToFill()
                                .frame(
                                    width: screenSize().width - 26.7,
                                    height: (screenSize().width - 26.7) * 300 / 1000
                                )
                                .contentShape(Rectangle())
                                .onTapGesture {
                                    if let _ = event.detailImageUrl {
                                        AppState.shared.setAppStep(step: .eventDetail(event: event))
                                    } else if let link = event.link, link.trimmingCharacters(in: .whitespaces).count > 0, let url = URL(string: link), UIApplication.shared.canOpenURL(url) {
                                        UIApplication.shared.open(url)
                                    }
                                }
                        }
                    }
                }
                .padding(.vertical, 13.3)
            }
        }
        .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
            GeometryReader { geo in
                HStack {
                    Spacer()
                    Text(viewModel.errorMessage)
                        .padding(.vertical, 13.3)
                        .padding(.horizontal, 6.7)
                        .frame(width: geo.size.width - 66.7, alignment: .center)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .background(Color(hex: "3bb9f1"))
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.leading)
                        .fixedSize(horizontal: false, vertical: true)
                        .cornerRadius(20)
                        .padding(.top, 66.7)
                    Spacer()
                }
            }
        }
        .onAppear {
            viewModel.getEvents()
        }
    }
}

struct EventListView_Previews: PreviewProvider {
    static var previews: some View {
        EventListView()
    }
}
