//
//  CustomWigetView.swift
//  Feels (iOS)
//
//  Created by Trey Tartt on 4/2/22.
//

import SwiftUI

struct CustomWigetView: View {
    @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
    @StateObject private var selectedWidget = StupidAssCustomWidgetObservableObject()

    var body: some View {
        ZStack {
            theme.currentTheme.secondaryBGColor
            VStack {
                ScrollView(.horizontal) {
                    HStack {
                        ForEach(UserDefaultsStore.getCustomWidgets(), id: \.uuid) { widget in
                            CustomWidgetView(customWidgetModel: widget)
                                .frame(width: 50, height:  50)
                                .cornerRadius(10)
                                .onTapGesture {
                                    EventLogger.log(event: "show_widget")
                                    selectedWidget.fuckingWrapped = widget.copy() as? CustomWidgetModel
                                    selectedWidget.showFuckingSheet = true
                                }
                        }
                        RoundedRectangle(cornerRadius: 10).fill().foregroundColor(theme.currentTheme.secondaryBGColor)
                            .frame(width: 50, height: 50)
                            .overlay(
                                Image(systemName: "plus")
                            )
                            .onTapGesture {
                                EventLogger.log(event: "tap_create_new_widget")
                                selectedWidget.fuckingWrapped = CustomWidgetModel.randomWidget
                                selectedWidget.showFuckingSheet = true
                            }
                    }
                    .padding()
                }
                .background(RoundedRectangle(cornerRadius: 10).fill().foregroundColor(theme.currentTheme.bgColor))
                .padding()
                .cornerRadius(10)
                
                Text("[How to add widget](https://support.apple.com/guide/iphone/add-widgets-iphb8f1bf206/ios)")
                    .accentColor(textColor)
                    .padding(.bottom)
            }
        }
        .fixedSize(horizontal: false, vertical: true)
        .cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
        .sheet(isPresented: $selectedWidget.showFuckingSheet) {
            if let fuckingWrapped = selectedWidget.fuckingWrapped {
                CreateWidgetView(customWidget: fuckingWrapped)
            }
        }
    }
}

struct CustomWigetView_Previews: PreviewProvider {
    static var previews: some View {
        CustomWigetView()
    }
}
