//
//  TimersView.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 10/03/2023.
//

import SwiftUI

struct TimersView: View {
    
    @EnvironmentObject var boringContext: BoringContext

    @Environment(\.managedObjectContext) private var viewContext
    
    @Binding var isEditing: Bool
    
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \AbstractTimer.order, ascending: true)],
        animation: .default)
    private var timers: FetchedResults<AbstractTimer>
    
    fileprivate let itemSpacing: CGFloat = 10.0

    var body: some View {
        
        let abstractTimers: [AbstractTimer] = Array(self.timers)
        if abstractTimers.count > 0 {
            
            GeometryReader { reader in
                
                let columns: [GridItem] = self._columns()
                let width: CGFloat = reader.size.width / CGFloat(columns.count) - 5.0
                
                ScrollView {
                    
                    LazyVGrid(
                        columns: columns,
                        spacing: itemSpacing
                    ) {
                        
                        ReorderableForEach(items: abstractTimers) { timer in
                            
                            DialView(timer: timer, isEditingBinding: self.$isEditing, frameSize: width, handler: { timer in
                                self._handleSiriTips(timer: timer)
                            })
                            .environment(\.managedObjectContext, viewContext)
                            .environmentObject(Conductor.maestro)
                            .environmentObject(boringContext)
                        } moveAction: { from, to in
                            self._reorder(from: from, to: to)
                        }
                        
                    }
                }
            }.padding(.horizontal, itemSpacing)
        } else {
            Text("You'll find your timers here. Start by creating them on the left screen").multilineTextAlignment(.center)
        }
        
    }
    
    fileprivate func _reorder(from: IndexSet, to: Int) {
        var timers: [AbstractTimer] = Array(self.timers)
        timers.move(fromOffsets: from, toOffset: to)
        for (i, countdown) in timers.enumerated() {
            countdown.order = Int16(i)
        }
        do {
            try viewContext.save()
        } catch {
            Logger.error(error)
            self.boringContext.error = error
        }
    }
    
    fileprivate func _handleSiriTips(timer: AbstractTimer) {
        let timerId = timer.stringId
        if !Preferences.timerSiriTips.contains(timerId) {
            self.boringContext.siriTimer = timer
            Preferences.timerSiriTips.insert(timerId)
        }
    }
    
    fileprivate func _columnCount() -> Int {
        #if os(iOS)
        if UIDevice.isPhoneIdiom {
            return 2
        } else {
            return 3
        }
        #else
        return 3
        #endif
    }
    
    fileprivate func _columns() -> [GridItem] {
        return (0..<self._columnCount()).map { _ in GridItem(spacing: 10.0) }
    }
        
}

struct TimersView_Previews: PreviewProvider {
    static var previews: some View {
        TimersView(isEditing: .constant(false))
    }
}
