//
//  DialView.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 02/02/2023.
//

import SwiftUI

struct DialView: View {
    
    @Environment(\.managedObjectContext) private var viewContext
    
    @EnvironmentObject var boringContext: BoringContext
    @EnvironmentObject var conductor: Conductor

    @State var timer: AbstractTimer
    var isEditingBinding: Binding<Bool>
    
    @State var showSilentModeAlert: Bool = false
    
    var frameSize: CGFloat
    
    var body: some View {
        ZStack {
            
            Image(self.timer.imageName).resizable().saturation(self.isEditingBinding.wrappedValue ? 0.0 : 1.0)
            
            switch self.isEditingBinding.wrappedValue {
            case false:
                Button {
                    if Preferences.hideSilentModeAlerts {
                        self._launchTimer()
                    } else {
                        self.showSilentModeAlert = true
                    }
                } label: {
                    VStack {
                        Spacer()
                        self._dialView().padding(.horizontal)
                        Spacer()
                    }
                }
            case true:
                
                NavigationLink {
                    self._editView(timer: timer, isPresented: $boringContext.isShowingNewData)
                } label: {
                    VStack {
                        Spacer()
                        self._dialView().padding(.horizontal)
                        Spacer()
                    }
                }
                
//                VStack {
//                    Spacer()
//                    self._dialView().padding(.horizontal)
//                    Spacer()
//                }
//
//                HStack {
//                    Spacer()
//                    NavigationLink {
//                        self._editView(timer: timer, isPresented: $boringContext.isShowingNewData)
//                    } label: {
//                        Image(systemName: "gearshape.fill")
//                            .font(.system(size: 30.0))
//                            .padding(.horizontal)
//                            .foregroundColor(Color.accentColor)
//                    }
//                }
            }
            
        }
        .frame(width: frameSize, height: 80.0)
        .cornerRadius(20.0)
        .alert("Make sure your device is not on silent mode", isPresented: $showSilentModeAlert) {
            Button("OK", role: .cancel) {                         self._launchTimer()
            }
            Button("Don't show this again", role: .destructive) {
                self._launchTimer()
                Preferences.hideSilentModeAlerts(true)
            }
        }
    }
    
    @ViewBuilder
    fileprivate func _dialView() -> some View {
        Group {
            switch self.timer {
            case let countdown as Countdown:
                CountdownDialView(countdown: countdown)
                    .environmentObject(Conductor.maestro)
            case let alarm as Alarm:
                AlarmDialView(alarm: alarm)
                    .environmentObject(Conductor.maestro)
            case let stopwatch as Stopwatch:
                StopwatchDialView(stopwatch: stopwatch)
                    .environmentObject(Conductor.maestro)
            default:
                Text("missing dial view")
            }
        }
        .monospaced()
        .font(Font.system(size: 16.0, weight: .semibold))
        .foregroundColor(Color.white)
    }
    
    @ViewBuilder
    fileprivate func _editView(timer: AbstractTimer, isPresented: Binding<Bool>) -> some View {
        switch timer {
        case let countdown as Countdown:
            CountdownEditView(isPresented: isPresented, countdown: countdown)
                .environment(\.managedObjectContext, viewContext)
        case let alarm as Alarm:
            AlarmEditView(alarm: alarm, isPresented: isPresented)
                .environment(\.managedObjectContext, viewContext)
        case let stopwatch as Stopwatch:
            StopwatchEditView(stopwatch: stopwatch, isPresented: isPresented)
                .environment(\.managedObjectContext, viewContext)
        default:
            Text("missing edit view")
        }
    }
    
    fileprivate func _launchTimer() {
        
        TimerRouter.performAction(timer: self.timer) { result in
            switch result {
            case .success:
                break
            case .failure(let failure):
                switch failure {
                case TimerError.notificationAuthorizationMissing:
                    self.boringContext.showPermissionAlert = true
                default:
                    self.boringContext.error = failure
                    self.boringContext.showDefaultAlert = true
                }
            }
        }
        
    }
    
}

struct DialView_Previews: PreviewProvider {
    static var previews: some View {
        
        DialView(
            timer: Countdown.fake(context: PersistenceController.preview.container.viewContext),
            isEditingBinding: .constant(true), frameSize: 150.0)
            .environmentObject(Conductor.maestro)
            .environmentObject(BoringContext())

    }
}
