//
//  ContentView.swift
//  StickerSlack
//
//  Created by neon443 on 15/10/2025.
//

import SwiftUI
import Haptics

struct ContentView: View {
	@StateObject var hoarder: EmojiHoarder = .shared
	
	var body: some View {
		NavigationSplitView {
			if #available(iOS 18, *) {
				TabView {
					Tab("Browse", systemImage: "square.grid.2x2.fill") {
						BrowseView()
					}
					
//					Tab {
//						List {
//							Text("\(searchTerm.isEmpty ? hoarder.emojis.count : hoarder.filteredEmojis.count) Emoji")
//
//							ForEach(hoarder.filteredEmojis, id: \.self) { name in
//								if let emoji = hoarder.trie.dict[name] {
//									EmojiRow(emoji: emoji)
//								}
//							}
//						}
//						.onChange(of: searchTerm) { _ in
//							hoarder.filterEmojis(by: searchTerm)
//						}
//						.refreshable {
//							Task.detached {
//								await hoarder.refreshDB()
//							}
//							searchTerm = ""
//						}
//					} label: {
//						Label("Search", systemImage: "magnifyingglass")
//					}
					
					Tab("Downloaded", systemImage: "arrow.down.circle.fill") {
						DownloadedView()
					}
					
					Tab("Tree", systemImage: "tree.fill") {
						TrieTestingView()
					}
					
					Tab("Search", systemImage: "magnifyingglass", role: .search) {
						NavigationStack {
							SearchView()
						}
					}
				}
				.searchable(text: $hoarder.searchTerm, placement: .navigationBarDrawer)
//				.searchable(text: $hoarder.searchTerm)
//				.modifier(tabViewActivationSearchActivation())
			} else {
				TabView {
					DownloadedView()
						.tabItem {
							Label("Downloaded", systemImage: "arrow.down.circle.fill")
						}
					BrowseView()
						.tabItem {
							Label("Browse", systemImage: "square.grid.2x2.fill")
						}
					TrieTestingView()
						.tabItem {
							Label("Trie", systemImage: "tree.fill")
						}
					SearchView()
						.tabItem {
							Label("Search", systemImage: "magnifyingglass")
						}
				}
			}
		} detail: {
			Text("")
		}
	}
}

#Preview {
	ContentView(hoarder: EmojiHoarder(localOnly: true))
}
