//
//  ImagePackPickerView.swift
//  Reflect (iOS)
//
//  Created by Trey Tartt on 4/2/22.
//

import SwiftUI

struct ImagePackPickerView: View {
    @AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
    @AppStorage(UserDefaultsStore.Keys.moodTint.rawValue, store: GroupUserDefaults.groupDefaults) private var moodTint: MoodTints = .Default
    @AppStorage(UserDefaultsStore.Keys.moodImages.rawValue, store: GroupUserDefaults.groupDefaults) private var imagePack: MoodImages = .FontAwesome
    @AppStorage(UserDefaultsStore.Keys.customMoodTintUpdateNumber.rawValue, store: GroupUserDefaults.groupDefaults) private var customMoodTintUpdateNumber: Int = 0
    
    var body: some View {
        ZStack {
            Text(String(customMoodTintUpdateNumber))
                .hidden()
            theme.currentTheme.secondaryBGColor
            VStack {
                ForEach(MoodImages.allCases, id: \.rawValue) { images in
                    ZStack {
                        VStack {
                            HStack {
                                ForEach(Mood.allValues, id: \.self) { mood in
                                    images.icon(forMood: mood)
                                        .resizable()
                                        .aspectRatio(contentMode: .fit)
                                        .frame(width: 35, height: 35)
                                        .foregroundColor(
                                            moodTint.color(forMood: mood)
                                        )
                                        .accessibilityLabel(mood.strValue)
                                }
                                .frame(minWidth: 0, maxWidth: .infinity)
                            }
                            .contentShape(Rectangle())
                            .background(
                                RoundedRectangle(cornerRadius: 10, style: .continuous)
                                    .fill(imagePack == images ? theme.currentTheme.bgColor : .clear)
                                    .padding([.top, .bottom], -3)
                            )
                            .onTapGesture {
                                let impactMed = UIImpactFeedbackGenerator(style: .heavy)
                                impactMed.impactOccurred()
                                imagePack = images
                                AnalyticsManager.shared.track(.iconPackChanged(packId: images.rawValue))
                            }
                            if images.rawValue != (MoodImages.allCases.sorted(by: { $0.rawValue > $1.rawValue }).first?.rawValue) ?? 0 {
                                Divider()
                            }
                        }
                    }
                }
            }
            .padding()
        }
        .fixedSize(horizontal: false, vertical: true)
        .cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
    }
}

struct ImagePackPickerView_Previews: PreviewProvider {
    static var previews: some View {
        ImagePackPickerView()
    }
}
