//
//  CustomizeView.swift
//  Feels (iOS)
//
//  Created by Trey Tartt on 2/19/22.
//

import SwiftUI

struct CustomizeView: View {
    @State private var showSettings = false
    @State private var sampleListEntry = PersistenceController.shared.randomEntries(count: 1).first!
    @AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
    @AppStorage(UserDefaultsStore.Keys.textColor.rawValue, store: GroupUserDefaults.groupDefaults) private var textColor: Color = DefaultTextColor.textColor
    
    var body: some View {
        ScrollView {
            settingsButtonView
            VStack {
                Group {
                    CustomWigetView()
                    IconPickerView()
                    ThemePickerView()
                    Divider()
                    sampleEntryView
                    ImagePackPickerView()
                }
                Group {
                    TintPickerView()
                    TextColorPickerView()
                }
                Divider()
                DayFilterPickerView()
                Divider()
                ShapePickerView()
                Divider()
                PersonalityPackPickerView()
            }
        }
        .onAppear(perform: {
            EventLogger.log(event: "show_customize_view")
        })
        .sheet(isPresented: $showSettings) {
            SettingsView()
        }
        .padding()
        .background(
            theme.currentTheme.bg
                .edgesIgnoringSafeArea(.all)
        )
    }
    
    private var settingsButtonView: some View {
        HStack {
            Spacer()
            Button(action: {
                showSettings.toggle()
            }, label: {
                Image(systemName: "gear")
                    .foregroundColor(Color(UIColor.darkGray))
                    .font(.system(size: 20))
            }).padding(.trailing)
        }
    }
    
    private var sampleEntryView: some View {
        ZStack {
            theme.currentTheme.secondaryBGColor
            
            VStack {
                HStack {
                    Spacer()
                    Image(systemName: "arrow.triangle.2.circlepath.circle")
                        .resizable()
                        .frame(width: 20, height: 20, alignment: .trailing)
                        .foregroundColor(Color(UIColor.systemGray))
                        .onTapGesture {
                            sampleListEntry = PersistenceController.shared.randomEntries(count: 1).first!
                        }
                }
                Spacer()
            }.padding()
            
            VStack(alignment:.leading) {
                Text(String(localized: "customize_view_view_example_row"))
                    .padding([.leading, .top])
                    .foregroundColor(textColor)
                Divider()
                EntryListView(entry: sampleListEntry)
                    .padding()
            }
        }
        .fixedSize(horizontal: false, vertical: true)
        .cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
    }
    
    
}

struct CustomizeView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            CustomizeView()
            
            CustomizeView()
                .preferredColorScheme(.dark)
        }
    }
}
