//
//  ContentMainBannerView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/11.
//

import SwiftUI
import Kingfisher

struct ContentMainBannerView: View {
    
    @StateObject private var viewModel = ContentMainBannerViewModel()
    
    var body: some View {
        ZStack {
            if !viewModel.bannerList.isEmpty {
                VStack(spacing: 0) {
                    TabView(selection: $viewModel.currentIndex) {
                        ForEach(0..<viewModel.bannerList.count, id: \.self) { index in
                            let item = viewModel.bannerList[index]
                            if let url = item.thumbnailImageUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
                                KFImage(URL(string: url))
                                    .cancelOnDisappear(true)
                                    .downsampling(
                                        size: CGSize(
                                            width: screenSize().width - 26.7,
                                            height: (screenSize().width - 26.7) * 0.53
                                        )
                                    )
                                    .resizable()
                                    .scaledToFill()
                                    .frame(
                                        width: screenSize().width - 26.7,
                                        height: (screenSize().width - 26.7) * 0.53
                                    )
                                    .onTapGesture {
                                        switch item.type {
                                        case .EVENT:
                                            AppState.shared.setAppStep(step: .eventDetail(event: item.eventItem!))
                                        case .CREATOR:
                                            AppState.shared.setAppStep(step: .creatorDetail(userId: item.creatorId!))
                                        case .LINK:
                                            if let link = item.link, link.trimmingCharacters(in: .whitespaces).count > 0, let url = URL(string: link), UIApplication.shared.canOpenURL(url) {
                                                UIApplication.shared.open(url)
                                            }
                                        }
                                    }
                                    .cornerRadius(4.7)
                            } else {
                                KFImage(URL(string: item.thumbnailImageUrl))
                                    .cancelOnDisappear(true)
                                    .downsampling(
                                        size: CGSize(
                                            width: screenSize().width - 26.7,
                                            height: (screenSize().width - 26.7) * 0.53
                                        )
                                    )
                                    .resizable()
                                    .scaledToFill()
                                    .frame(
                                        width: screenSize().width - 26.7,
                                        height: (screenSize().width - 26.7) * 0.53
                                    )
                                    .onTapGesture {
                                        switch item.type {
                                        case .EVENT:
                                            AppState.shared.setAppStep(step: .eventDetail(event: item.eventItem!))
                                        case .CREATOR:
                                            AppState.shared.setAppStep(step: .creatorDetail(userId: item.creatorId!))
                                        case .LINK:
                                            if let link = item.link, link.trimmingCharacters(in: .whitespaces).count > 0, let url = URL(string: link), UIApplication.shared.canOpenURL(url) {
                                                UIApplication.shared.open(url)
                                            }
                                        }
                                    }
                                    .cornerRadius(4.7)
                            }
                        }
                    }
                    .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
                    .frame(
                        width: screenSize().width - 26.7,
                        height: (screenSize().width - 26.7) * 0.53
                    )
                    
                    HStack(spacing: 4) {
                        ForEach(0..<viewModel.bannerList.count, id: \.self) { index in
                            Capsule()
                                .foregroundColor(index == viewModel.currentIndex ? Color(hex: "3bb9f1") : Color(hex: "909090"))
                                .frame(
                                    width: index == viewModel.currentIndex ? 18 : 6,
                                    height: 6
                                )
                                .tag(index)
                        }
                    }
                    .padding(.top, 13.3)
                }
                .frame(maxWidth: .infinity)
                .onAppear {
                    viewModel.timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
                }
                .onDisappear {
                    viewModel.timer.upstream.connect().cancel()
                }
                .onReceive(viewModel.timer) { _ in
                    DispatchQueue.main.async {
                        withAnimation {
                            if viewModel.currentIndex == viewModel.bannerList.count - 1 {
                                viewModel.currentIndex = 0
                            } else {
                                viewModel.currentIndex += 1
                            }
                        }
                    }
                }
            }
            
            if viewModel.isLoading {
                ActivityIndicatorView()
                    .frame(width: 100, height: 100)
            }
        }
        .frame(maxWidth: .infinity)
        .onAppear {
            viewModel.getBannerList()
        }
    }
}

struct ContentMainBannerView_Previews: PreviewProvider {
    static var previews: some View {
        ContentMainBannerView()
    }
}
