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

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
    }
}

struct ContentView: View {
    @State var isEditing = false

    @State var entries: [Entry] = []

    @State var entryText = ""

    var treeImageName: String {
        switch entries.count {
        case 0:
            return "TreeBare"
        case 1:
            return "TreeLeafy"
        case 2:
            return "TreeLeafier"
        case 3:
            return "TreeLeafierAnimals"
        default:
            return "TreeLeafierBlossoms"
        }
    }

    var treeHeight: Double {
        switch entries.count {
        case 0:
            return 100 // bare
        case 1:
            return 300 // leafy
        default:
            return 400 // leafier
        }
    }

    var body: some View {
        NavigationStack {
            VStack(spacing: 0) {
                windButton

                Spacer()

                treeView

                formView
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .edgesIgnoringSafeArea(isEditing ? [] : .bottom)
            .background(Color("Sky"))
        }
    }

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

    @ViewBuilder var treeView: some View {
        Button {
            isEditing.toggle()
        } label: {
            Image(treeImageName)
                .resizable()
                .scaledToFit()
                .foregroundColor(.white)
        }
        .frame(maxHeight: treeHeight)

        Color("Grass")
            .frame(height: 80)
    }

    @ViewBuilder var formView: some View {
        Form {
            TextField("What are you grateful for today?", text: $entryText)
                .background(Color.white)
                .tint(Color.blue)
                .submitLabel(.done)
        }
        .onSubmit {
            if !entryText.isEmpty {
                withAnimation {
                    entries.append(Entry(text: entryText))
                }
            }
            entryText = ""
            isEditing = false
        }
        .frame(maxHeight: isEditing ? 120 : 0)
        .animation(.easeInOut.speed(2), value: isEditing)
    }
}

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: "Burritos"),
            ])
            .previewDisplayName("Leafier")

            ContentView(entries: [
                Entry(text: "Coffee"),
                Entry(text: "Burritos"),
            ])
            .previewDisplayName("Leafier - Adding Entry")

            ContentView(entries: [
                Entry(text: "Coffee"),
                Entry(text: "Burritos"),
                Entry(text: "Mountains"),
            ])
            .previewDisplayName("Leafier with Animals")

            ContentView(entries: [
                Entry(text: "Coffee"),
                Entry(text: "Burritos"),
                Entry(text: "Mountains"),
                Entry(text: "Trees"),
            ])
            .previewDisplayName("Leafier with Blossoms")
        }
        .tint(.white)
        .previewLayout(.sizeThatFits)
    }
}
