//
//  AltitudeMedView.swift
//  Toolboxes
//
//  Created by 研发2组 on 2024/10/7.
//

import WidgetKit
import SwiftUI

struct AltitudeMediumView: View {
    let data: [ String:String]
    let scalefont : Double = 0.15
    
    var body: some View {
      
        GeometryReader { geometry in
           VStack{
                VStack(alignment: .leading) {
                    
                    HStack{
                        HStack(spacing: 3){
                            Image("w_shan")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(width: geometry.size.height * scalefont * 1.5,height: geometry.size.height * scalefont * 1.5)
                            Text(L("Altitude"))
                                .font(.system(size: geometry.size.height * scalefont, weight: .bold))
                                .foregroundColor(Color.black)
                        }.padding(.leading,-10)
                        
                        Spacer()
                        
                        HStack(alignment: .bottom){
                           
                            Text(String(format: "%.1fm", Double(data["altitude"]!)!))
                                .font(.system(size: geometry.size.height * scalefont, weight: .bold))
                                .foregroundColor(.customdeepblue)
                                .lineLimit(1)
                        }
                    }
         
                    Spacer()
                
                    HStack{
                        Text("\(L("Latitude")):\(  convertCoordinatesToDMS(latitudeAsDouble: data["latitude"]!, longitudeAsDouble: data["longitude"]!).latitudeDMS)")
                            .font(.system(size: geometry.size.height * scalefont * 0.6))
                            .lineLimit(1)
                            .foregroundColor(Color.black)
                        
                        Spacer()
                        
                        Text("\(L("Longitude")):\(convertCoordinatesToDMS(latitudeAsDouble: data["latitude"]!, longitudeAsDouble: data["longitude"]!).longitudeDMS)")
                            .font(.system(size: geometry.size.height * scalefont * 0.6))
                            .lineLimit(1)
                            .foregroundColor(Color.black)
                    }
                }
                .padding(.all, geometry.size.height * 0.1)
                .frame(maxHeight: .infinity)
                
                Spacer()
                
                ZStack(alignment: .top){
                
                    HStack{
                        Image("altitude_ruler_h")
                            .resizable() // 确保图像可调整大小
                            .aspectRatio(contentMode: .fit)
                            .frame(width: geometry.size.width )
                            
                    }
                    .padding([.top,.bottom],5).frame(width: geometry.size.width)
                   
                    HStack(spacing: 0.0){
                        HStack{
                            
                            if let altitude = Double(data["altitude"]!) , altitude > 0 {
                                let calculatedWidth = altitude / 10000.0 * geometry.size.height
                                let safeWidth = max(0, calculatedWidth - geometry.size.height * 0.1)
                                Spacer()
                                    .frame(width: safeWidth, height: 0)  // Spacer 通常不需要设置高度
                            }
                           
                            Image("altitude_point_h")
                                .resizable() // 确保图像可调整大小
                                .aspectRatio(contentMode: .fit)
                                .frame(width: geometry.size.height * 0.1 * 0.75,height: geometry.size.height * 0.1 )
                            
                            Spacer()
                        }

                    }
                }
                .background(Color.white)
            
            }  .gradientBackground(colors: [.customgradient_top, .customgradient_bottom])
        }
    }
}

struct AltitudeMediumView_Previews: PreviewProvider {
    static var previews: some View {
  
        AltitudeMediumView(data: ["latitude":"\(0.00)","longitude":"\(0.00)","altitude":"\(3.00)"])
    }
}

