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

import SwiftUI

struct CloudView: View {
    @Binding var entries: [Entry]

    @State var countedWords: [String: Int] = [:]

    var words: [String] {
        Array(countedWords.keys)
    }

    @Environment(\.dismiss) var dismiss

    init(entries: Binding<[Entry]>, countedWords: [String: Int]? = nil) {
        _entries = entries
        if let countedWords {
            _countedWords = State(wrappedValue: countedWords)
        } else {
            _countedWords = State(wrappedValue: countWords())
        }
    }

    var body: some View {
        var isFirst = true
        words.reduce(Text("")) { cloud, word in
            defer { isFirst = false }
            let count = countedWords[word, default: 1]
            let bullet = (isFirst ? Text("") : Text(" • "))
            return cloud + bullet + Text(word)
                .font(fontForCount(count))
                .bold(count > 2)
        }
        .multilineTextAlignment(.center)
        .padding()
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .edgesIgnoringSafeArea(.bottom)
        .background(Color("Sky"))
        .foregroundColor(.white)
        .toolbar {
            ToolbarItem(placement: .destructiveAction) {
                Button {
                    entries.removeAll()
                    dismiss()
                } label: {
                    Image(systemName: "flame")
                }
            }
        }
    }

    func fontForCount(_ count: Int) -> Font {
        switch count {
        case 0, 1: return .body
        case 2: return .title3
        case 3: return .title2
        case 4: return .title
        default: return .largeTitle
        }
    }

    func countWords() -> [String: Int] {
        let words = entries
            .map(\.text)
            .flatMap { $0.split(separator: " ") }
            .map(String.init)
            .filter { $0.count >= 3 }
        return words.reduce(into: [:]) { partialResult, word in
            partialResult[word, default: 0] += 1
        }
    }
}

struct CloudView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            NavigationStack {
                CloudView(entries: .constant([]))
            }
            .previewDisplayName("Empty")

            NavigationStack {
                CloudView(entries: .constant([]), countedWords: [
                    "Coffee" : 2,
                    "Trees" : 1,
                    "Mountains" : 1,
                ])
            }
            .previewDisplayName("Few entries")

            NavigationStack {
                CloudView(entries: .constant([]), countedWords: [
                    "Coffee" : 4,
                    "Trees" : 2,
                    "Mountains" : 3,
                    "Burritos" : 1,
                    "Home" : 5,
                    "My Cat": 2,
                    "Love": 3,
                    "Clean water": 2,
                    "Health": 3,
                ])
            }
            .previewDisplayName("Many entries")
        }
        .tint(.white)
    }
}
