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

import SwiftUI
import FirebaseRemoteConfig

struct SplashView: View {
    
    @State private var isShowForcedUpdatePopup = false
    @State private var isShowUpdatePopup = false
    
    var body: some View {
        ZStack(alignment: .top) {
            LinearGradient(
                gradient: Gradient(colors: [Color(hex: "a0e2ff"), Color(hex: "ecfaff")]),
                startPoint: .bottom,
                endPoint: .top
            ).ignoresSafeArea()
            
            Image("splash_bubble")
                .padding(.top, 11)
            
            VStack(spacing: 0) {
                Image("splash_text")
                    .padding(.top, 111)
                
                Image("splash_logo")
                    .padding(.top, 77.3)
                
                Spacer()
                
                Image("vividnext_logo")
                    .padding(.bottom, 36)
            }
        }
        .onAppear {
            fetchLastestVersion()
        }
    }
    
    private func fetchLastestVersion() {
        let remoteConfig = RemoteConfig.remoteConfig()
        let configSettings = RemoteConfigSettings()
        configSettings.minimumFetchInterval = 300
        remoteConfig.configSettings = configSettings
        
        remoteConfig.fetch { status, error in
            if status == .success {
                remoteConfig.activate { changed, error in
                    checkAppVersion(latestVersion: remoteConfig["ios_latest_version"].stringValue)
                }
            } else {
                withAnimation {
                    DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
                        AppState.shared.setAppStep(step: .main)
                    }
                }
            }
        }
    }
    
    private func checkAppVersion(latestVersion: String?) {
        let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
        
        if let latestVersion = latestVersion, let version = version {
            let latestVersions = latestVersion.split(separator: ".")
            let versions = version.split(separator: ".")
            
            let latestMajor = Int(latestVersions[0])!
            let latestMinor = Int(latestVersions[1])!
            let latestPatch = Int(latestVersions[2])!
            
            let major = Int(versions[0])!
            let minor = Int(versions[1])!
            let patch = Int(versions[2])!
            
            if latestMajor > major || (latestMajor == major && latestMinor > minor) {
                self.isShowForcedUpdatePopup = true
            } else if latestMajor == major && latestMinor == minor && latestPatch > patch {
                self.isShowUpdatePopup = true
            } else {
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    withAnimation {
                        AppState.shared.setAppStep(step: .main)
                    }
                }
            }
        } else {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                withAnimation {
                    AppState.shared.setAppStep(step: .main)
                }
            }
        }
    }
}

struct SplashView_Previews: PreviewProvider {
    static var previews: some View {
        SplashView()
    }
}
