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

import SwiftUI

struct BGViewItem: View {
    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 = mood.color
        self.mood = mood
        self.size = size
        self.yRowPosition = yRowPosition
        self.animate = animate
    }
    
    var body: some View {
        Mood.allValues.randomElement()?.icon
            .resizable()
            .frame(width: size.width, height: size.height)
            .foregroundColor(color)
//            .blur(radius: 3)
            .opacity(0.1)
    }
}

struct BGView: View, Equatable {
    var numAcross: Int
    var numDown: Int
    let iconSize = 35
    
    init() {
        let screenWidth = UIScreen.main.bounds.width
        numAcross = Int(screenWidth)/iconSize
        
        let screenHeight = UIScreen.main.bounds.height
        numDown = Int(screenHeight)/iconSize
    }
    
    var body: some View {
        VStack {
            ForEach(0...numDown, id: \.self) { row in
                HStack {
                    ForEach(0...numAcross, id: \.self) { _ in
                        BGViewItem(mood: Mood.allValues.randomElement()!,
                                   size: .init(width: iconSize,height: iconSize),
                                   animate: false,
                                   yRowPosition: Float(row)/Float(numDown))
                    }.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
    }
}

struct BGView_Previews: PreviewProvider {
    static var previews: some View {
        BGView().environment(\.managedObjectContext, PersistenceController.shared.viewContext)
            .onAppear(perform: {
                PersistenceController.shared.populateMemory()
            })
        
        BGView()
            .preferredColorScheme(.dark)
            .environment(\.managedObjectContext, PersistenceController.shared.viewContext)
    }
}
