//
//  ContentView.swift
//  MemoryTree
//
//  Created by Work on 2023-02-05.
//

import Boutique
import SwiftUI

struct Entry: Hashable, Identifiable, Codable {
    let id: UUID
    let text: String

    init(id: UUID = UUID(), text: String) {
        self.id = id
        self.text = text
    }
}

extension Store where Item == Entry {
    static let entriesStore = Store<Entry>(
        storage: SQLiteStorageEngine.default(appendingPath: "Entries")
    )
}

struct ContentView: View {
    @State var isEditing = false

//    @Stored(in: .entriesStore) var entries
    @State var entries: [Entry] = []

    @State var entryText = ""

    var treeImageName: String {
        if entries.isEmpty {
            return "TreeBare"
        } else if entries.count < 3 {
            return "TreeLeafy"
        } else {
            return "TreeLeafier"
        }
    }

    var treeHeight: Double {
        if entries.isEmpty {
            return 100
        } else if entries.count < 3 {
            return 300
        } else {
            return 400
        }
    }

    var body: some View {
        NavigationStack {
            VStack(spacing: 0) {
                NavigationLink(destination: CloudView(entries: $entries)) {
                    Image(systemName: "wind.snow")
                        .imageScale(.large)
                        .foregroundColor(.white)
                }
                .padding()
                .frame(maxWidth: .infinity, alignment: .trailing)
                .opacity(entries.isEmpty ? 0 : 1)

                Spacer()

                Button {
                    isEditing = true
                } label: {
                    Image(treeImageName)
                        .renderingMode(.template)
                        .resizable()
                        .scaledToFit()
                        .foregroundColor(.white)
                }
                .frame(maxHeight: treeHeight)

                Color.green
                    .frame(height: 80)

                Form {
                    TextField("What are you grateful for today?", text: $entryText)
                        .background(Color.white)
                        .tint(Color.blue)
                }
                .onSubmit {
//                    Task {
//                        try await $entries.insert(Entry(text: entryText))
//                    }
                    entries.append(Entry(text: entryText))
                    entryText = ""
                    isEditing = false
                }
                .frame(maxHeight: isEditing ? 120 : 0)
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .edgesIgnoringSafeArea(.bottom)
            .background(Color.blue)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
                .previewDisplayName("Bare")

            ContentView(isEditing: true)
                .previewDisplayName("Adding Entry")

            ContentView(entries: [Entry(text: "Coffee")])
                .previewDisplayName("Leafy")

            ContentView(isEditing: true, entries: [Entry(text: "Coffee")])
                .previewDisplayName("Leafy - Adding Entry")

            ContentView(entries: [
                Entry(text: "Coffee"),
                Entry(text: "Marion berries"),
                Entry(text: "Burritos"),
            ])
            .previewDisplayName("Leafier")
        }
        .tint(.white)
    }
}
