//
//  CountdownView.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 25/01/2023.
//

import SwiftUI
import WidgetKit
import CoreData

struct SingleCountdownView: View {
    
    @Environment(\.widgetFamily) var family: WidgetFamily
    
    var countdown: Countdown
    
    var body: some View {
        VStack {
            HStack {
                VStack(alignment: .leading) {
                    Text(countdown.displayName.uppercased())
                    Text(countdown.duration.minuteSecond)
                }
                Spacer()
            }
            Spacer()
        }
        .padding()
        .monospaced()
        .foregroundColor(Color.white)
        .font(self.font)
        .widgetURL(countdown.url)
    }
    
    private var font: Font {
        switch family {
        case .systemSmall, .systemMedium, .systemLarge, .systemExtraLarge:
            return .body
        case .accessoryCircular:
            return .footnote
        default:
            return .body
        }
    }
    
}


struct LockScreenCountdownView: View {
    
    @Environment(\.widgetFamily) var family: WidgetFamily
    
    var countdown: Countdown
    
    var body: some View {
        VStack {
            Text(countdown.displayName.uppercased())
            Text(countdown.duration.minuteSecond)
        }
        .monospaced()
        .foregroundColor(Color.white)
        .font(self.font)
        .widgetURL(countdown.url)
    }
    
    private var font: Font {
        switch family {
        case .systemSmall, .systemMedium, .systemLarge, .systemExtraLarge:
            return .body
        case .accessoryCircular:
            return .footnote
        default:
            return .body
        }
    }
    
}

struct MultiCountdownView: View {
    
    @Environment(\.widgetFamily) var family: WidgetFamily
    
    private let columns: [GridItem] = [
        GridItem(spacing: 10.0),
        GridItem(spacing: 10.0),
    ]
    
    var countdowns: [Countdown]
    
    var body: some View {
        
        if countdowns.isEmpty {
            VoidView()
        } else {
            
            LazyVGrid(
                columns: columns,
                spacing: 10.0
            ) {
                
                ForEach(countdowns) { countdown in
                    
                    Link(destination: countdown.url) {
                        HStack {
                            
                            VStack(alignment: .leading) {
                                Spacer()
                                Text(countdown.displayName.uppercased())
                                Text(countdown.duration.minuteSecond)
                                Spacer()
                            }
                            Spacer()
                        }
                        .padding(.horizontal)
                        .font(.callout)
                        .background(Image(countdown.imageName))
                        .foregroundColor(.white)
                        .monospaced()
                        .cornerRadius(16.0)
                    }

                }
            }.padding()
            
            
            
//            HStack {
//                ForEach(countdowns) { countdown in
//
//                    Link(destination: countdown.url) {
//                        VStack {
//                            Text(countdown.name ?? "")
//                            Text(countdown.duration.minuteSecond)
//                        }
//                        .font(self.font)
//                        .frame(maxWidth: .infinity, maxHeight: .infinity)
//                    }
//
//                }
//            }.frame(maxWidth: .infinity)
        }
        
    }
    
    private var font: Font {
        switch family {
        case .systemSmall, .systemMedium, .systemLarge, .systemExtraLarge:
            return .title2
        default:
            return .body
        }
    }
    
}

struct CountdownView_Previews: PreviewProvider {
    static var previews: some View {
        
        SingleCountdownView(countdown: Countdown.fake(context: PersistenceController.preview.container.viewContext)).previewContext(WidgetPreviewContext(family: .systemSmall)).background(.black)
        LockScreenCountdownView(countdown: Countdown.fake(context: PersistenceController.preview.container.viewContext)).previewContext(WidgetPreviewContext(family: .accessoryRectangular))
        LockScreenCountdownView(countdown: Countdown.fake(context: PersistenceController.preview.container.viewContext)).previewContext(WidgetPreviewContext(family: .accessoryCircular))
        MultiCountdownView(countdowns: self.countdowns(context: PersistenceController.preview.container.viewContext)).previewContext(WidgetPreviewContext(family: .systemMedium))
    }
    
    static func countdowns(context: NSManagedObjectContext) -> [Countdown] {
        return (0..<4).map { _ in
            Countdown.fake(context: context)
        }
    }
    
}
