//
//  ToolboxesWidgets.swift
//  ToolboxesWidgets
//
//  Created by 研发2组 on 2024/9/25.
//

import WidgetKit
import SwiftUI

struct AltitudeProvider: TimelineProvider {
    
   
    let latitude = appGroupDefaults?.double(forKey: "latitude") ?? 0
    let longitude = appGroupDefaults?.double(forKey: "longitude") ?? 0
    let altitude = appGroupDefaults?.double(forKey: "altitude") ?? 0

    
    func placeholder(in context: Context) -> AltitudeEntry {
        AltitudeEntry(date: Date(), data: ["latitude":"\(latitude)","longitude":"\(longitude)","altitude":"\(altitude)"])
    }
    
    func getSnapshot(in context: Context, completion: @escaping (AltitudeEntry) -> Void) {
        let entry = AltitudeEntry(date: Date(), data: ["latitude":"\(latitude)","longitude":"\(longitude)","altitude":"\(altitude)"])
        completion(entry)
    }
    
    func getTimeline(in context: Context, completion: @escaping (Timeline<AltitudeEntry>) -> Void) {
        var entries: [AltitudeEntry] = []
        
        let currentDate = Date()
   
        for mOffset in 0..<60 {
            let entryDate = Calendar.current.date(byAdding: .minute, value: mOffset, to: Calendar.current.startOfDay(for: currentDate))!
            let entry = AltitudeEntry(date: entryDate, data: ["latitude":"\(latitude)","longitude":"\(longitude)","altitude":"\(altitude)"])
            entries.append(entry)
        }
    
        let timeline = Timeline(entries: entries, policy: .atEnd)
        
        completion(timeline)
    }

}


struct AltitudeEntry: TimelineEntry {
    let date: Date
    let data: [ String:String]
}

struct ToolboxesWidgetsEntryView : View {
    var entry: AltitudeProvider.Entry
    @Environment(\.widgetFamily) var family: WidgetFamily

    var body: some View {

        ZStack{
            if family == .systemSmall {
        
                AltitudeSmallView(data: entry.data)
                
            }else  if family == .systemMedium {
                AltitudeMediumView(data: entry.data)

            }else  if family == .systemLarge {
                AltitudeSmallView(data: entry.data)
            }else{
                Text("暂无")
            }
        }
    }
}

@available(iOSApplicationExtension 15.0, *)
struct AltitudeSmallWidget: Widget {
    
    let kind: String = "AltitudeSmallWidget"

    var body: some WidgetConfiguration {
        
        StaticConfiguration(kind: kind, provider: AltitudeProvider()) { entry in
            
            if #available(iOSApplicationExtension 17.0, *) {
                ToolboxesWidgetsEntryView(entry: entry)
                    .containerBackground(Color("WidgetBackground"), for:.widget)
            }else{
                ToolboxesWidgetsEntryView(entry: entry)
            }
          
        }
        .configurationDisplayName("Altitude - small")
        .description("Real-time altitude")
        .supportedFamilies([.systemSmall])
        .disableContentMarginsIfNeeded()
    }
    
}

@available(iOSApplicationExtension 15.0, *)
struct AltitudeMediumWidget: Widget {
    
    let kind: String = "AltitudeMediumWidget"

    var body: some WidgetConfiguration {
        
        StaticConfiguration(kind: kind, provider: AltitudeProvider()) { entry in
            
            if #available(iOSApplicationExtension 17.0, *) {
                ToolboxesWidgetsEntryView(entry: entry)
                    .containerBackground(Color("WidgetBackground"), for:.widget)
            }else{
                ToolboxesWidgetsEntryView(entry: entry)
            }
          
        }
         .configurationDisplayName("Altitude - large")
         .description("Real-time altitude")
        .supportedFamilies([.systemMedium])
        .disableContentMarginsIfNeeded()
    }

}

@available(iOS 15.0, *)
extension WidgetConfiguration {
    func disableContentMarginsIfNeeded() -> some WidgetConfiguration {
        if #available(iOSApplicationExtension 17.0, *) {
            return self.contentMarginsDisabled()
        } else {
            return self
        }
    }
}

