import SwiftUI
import Shared
import LeStorage

struct ContentView: View {
    @State private var showContent = false
    @State private var sessionCount = 0

    let sessionCollection: StoredCollection<Session>

    init() {
        let center = StoreCenter.companion.default_
        self.sessionCollection = center.mainStore.collection(filename: "sessions.json")
        self._sessionCount = State(initialValue: Int(sessionCollection.items.count))
    }

    var body: some View {
        VStack {
            Button("Add New Session") {
                let randomName = "Session-\(Int.random(in: 1000...9999))"
                let newSession = Session()
                newSession.name = randomName
                sessionCollection.addOrUpdate(item: newSession)
                sessionCount = Int(sessionCollection.items.count)
            }

            Text("Total Sessions: \(sessionCount)")

            Button("Click me!") {
                withAnimation {
                    showContent = !showContent
                }
            }

            if showContent {
                VStack(spacing: 16) {
                    Image(systemName: "swift")
                        .font(.system(size: 200))
                        .foregroundColor(.accentColor)
                    Text("SwiftUI: \(LeStorage.shared.GREETING)")
                }
                .transition(.move(edge: .top).combined(with: .opacity))
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
