//
//  IconView.swift
//  Reflect (iOS)
//
//  Created by Trey Tartt on 1/20/22.
//

import SwiftUI
import SwiftData

struct BGViewItem: View {
    @AppStorage(UserDefaultsStore.Keys.moodTint.rawValue, store: GroupUserDefaults.groupDefaults) private var moodTint: MoodTints = .Default

    let mood: Mood
    let size: CGSize
    var color: Color
    let animate: Bool
    let yRowPosition: Float
    
    init(mood: Mood, size: CGSize, animate: Bool, yRowPosition: Float, color: Color) {
        self.color = color
        self.mood = mood
        self.size = size
        self.yRowPosition = yRowPosition
        self.animate = animate
    }
    
    var body: some View {
        FontAwesomeMoodImages.icon(forMood: mood)
            .resizable()
            .frame(width: size.width, height: size.height)
            .foregroundColor(DefaultMoodTint.color(forMood: mood))
        //            .blur(radius: 3)
            .opacity(0.1)
            .accessibilityHidden(true)
    }
}

struct BGView: View, Equatable {
    @AppStorage(UserDefaultsStore.Keys.moodTint.rawValue, store: GroupUserDefaults.groupDefaults) private var moodTint: MoodTints = .Default
    
    var numAcross: Int
    var numDown: Int
    let iconSize = 35
    
    init(screenSize: CGSize = CGSize(width: 393, height: 852)) {
        numAcross = Int(screenSize.width) / iconSize
        numDown = Int(screenSize.height) / iconSize
    }
    
    var randomMood: Mood? {
        return Mood.allValues.randomElement()
    }
    
    var body: some View {
        VStack {
            ForEach(0...numDown, id: \.self) { row in
                HStack {
                    ForEach(0...numAcross, id: \.self) { _ in
                        if let randomMood = randomMood {
                            BGViewItem(mood: randomMood,
                                       size: .init(width: iconSize,height: iconSize),
                                       animate: false,
                                       yRowPosition: Float(row)/Float(numDown),
                                       color: moodTint.color(forMood:randomMood))
                        } 
                    }.frame(minWidth: 0, maxWidth: .infinity)
                        .padding([.trailing, .leading], 13.5)
                }
                .padding(.top, -9)
            }
        }
        .padding(.top, -50)
    }
    
    static func == (lhs: BGView, rhs: BGView) -> Bool {
       return true
    }
}

#if !WIDGET_EXTENSION
struct BGView_Previews: PreviewProvider {
    static var previews: some View {
        BGView().modelContainer(DataController.shared.container)
            .onAppear(perform: {
                DataController.shared.populateMemory()
            })

        BGView()
            .preferredColorScheme(.dark)
            .modelContainer(DataController.shared.container)
    }
}
#endif
