//
//  ContentView.swift
//  RiotApp
//
//  Created by Jake MacMullin on 4/11/21.
//

import SwiftUI
import AuthenticationServices

struct ContentView: View {
    @EnvironmentObject var userService: UserService
    @State var activeTab = 0
    
    var body: some View {
        let showOnboarding = Binding<Bool> {
            !userService.hasSeenOnboarding
        } set: { newValue in
            if newValue {
                userService.hasSeenOnboarding = true
            }
        }
        
        TabView(selection: $activeTab) {
            NavigationView {
                LatestArticlesView()
                    .navigationBarTitleDisplayMode(.inline)
            }
            .navigationViewStyle(.stack)
            .tabItem {
                Label(title: {
                    Text("Articles")
                        .textStyle(.tabLabel)
                }, icon: {
                    ImageName.megaphone.image
                        .textStyle(.tabIcon)
                })
            }
            .tag(0)
            
            NavigationView {
                ConversationView()
                    .navigationTitle("Conversation")
                    .navigationBarTitleDisplayMode(.inline)
                    
            }
            .navigationViewStyle(.stack)
            .tabItem {
                Label(title: {
                    Text("Conversation")
                        .textStyle(.tabLabel)
                }, icon: {
                    ImageName.conversation.image
                        .textStyle(.tabIcon)
                })
            }
            .tag(1)
            
            NavigationView {
                WhatsOnView()
                    .navigationTitle("What's On")
                    .navigationBarTitleDisplayMode(.inline)
            }
            .navigationViewStyle(.stack)
            .tabItem {
                Label(title: {
                    Text("What's On")
                        .textStyle(.tabLabel)
                }, icon: {
                    ImageName.calendar.image
                        .textStyle(.tabIcon)
                })
            }
            .tag(2)
            
            NavigationView {
                SearchView()
                    .navigationTitle("Search")
                    .navigationBarTitleDisplayMode(.inline)
            }
            .navigationViewStyle(.stack)
            .tabItem {
                Label(title: {
                    Text("Search")
                        .textStyle(.tabLabel)
                }, icon: {
                    ImageName.search.image
                        .textStyle(.tabIcon)
                })
            }
            .tag(3)
            
            NavigationView {
                ProfileView()
                    .navigationTitle("Profile")
                    .navigationBarTitleDisplayMode(.inline)
            }
            .navigationViewStyle(.stack)
            .tabItem {
                Label(title: {
                    Text("Profile")
                        .textStyle(.tabLabel)
                }, icon: {
                    ImageName.profile.image
                        .textStyle(.tabIcon)
                })
            }
            .tag(4)
        }
        .onReceive(userService.$openDeeplink, perform: { deeplink in
            if let deeplink = deeplink {
                handleDeeplink(deeplink)
            }
        })
        .onOpenURL { url in
            guard let deeplink = Deeplink(url: url) else {
                return
            }
            handleDeeplink(deeplink)
        }
        .onContinueUserActivity(NSUserActivityTypeBrowsingWeb, perform: { activity in
            guard let url = activity.webpageURL,
                let deeplink = Deeplink(url: url) else {
                return
            }
            handleDeeplink(deeplink)
        })
        .fullScreenCover(isPresented: showOnboarding) {
            OnboardingView()
        }
        
    }
    
    private func handleDeeplink(_ deeplink: Deeplink) {
        activeTab = 0
    }
}


#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif
