//
//  DataView.swift
//  Toolboxes
//
//  Created by 研发2组 on 2024/9/21.
//

import SwiftUI

struct DataView: View {

    @EnvironmentObject var compassManager: CompassManager

    var body: some View {
        VStack(spacing: 0.0){
            
            ZStack(alignment: .top) {
                
                Image("data_topbg")
                    .resizable()
                    .scaledToFill()
                   // .frame(height: 200)
                    .ignoresSafeArea(edges: .top)
                
                VStack{
                  
                    HStack(alignment: .top) {
                        Spacer()
                        
                        Button(action: {
                            compassManager.requestLocation()
                        
                        }) {
                            Image("data_reload")
                                .resizable() // 确保图像可调整大小
                                .aspectRatio(contentMode: .fit) // 保持宽高比
                                .circularBackground(size: 44)
                        }
                    }.padding(.horizontal,15.0)
                    
                    
                    HStack(alignment: .bottom,spacing: 3) {
                        Image("data_mountain")
                            .resizable()
                            .frame(width: 35, height: 30)
                            .scaledToFit()
                            .clipped()

                        Text(String(format: "%.1f", compassManager.altitude))
                            .font(.system(size: 60, weight: .bold))
                            .baselineOffset(0.0)
                            .alignmentGuide(.bottom) { d in d[.lastTextBaseline] }
                        Text(L("m"))
                            .font(.system(size: 15))
                    }
                }
          
            }

            
            BarChartView(compassManager: compassManager)
                .frame(maxWidth: .infinity,maxHeight: 200)
                .padding(.horizontal, 15)
                .padding(.top,-120)
               
            
            HStack(alignment: .center, spacing: 0){
                
                VStack(alignment: .trailing,spacing: 30){
                    InfoView(imagename:"data_date",title: L("Date"), value: getCurrentFormattedDate())
                    
                    Rectangle().fill(Color.customLine).frame(maxWidth: .infinity, maxHeight: 1)
                    
                    InfoView(imagename:"data_lon",title: L("Longitude"), value: "\(convertCoordinatesToDMS(latitudeAsDouble: String(compassManager.latitude), longitudeAsDouble: String(compassManager.longitude)).longitudeDMS)")
                    
                    Rectangle().fill(Color.customLine).frame(maxWidth: .infinity, maxHeight: 1)
                    
                    InfoView(imagename:"data_pre",title: L("Pressure"), value: String(format: "%.0fbp", compassManager.pressure))
                }
                
                Rectangle().fill(Color.customLine).frame(maxWidth: 1,maxHeight: .infinity)
                
                VStack(alignment: .leading,spacing: 30){
                    InfoView(imagename:"data_step",title: L("Step"), value: "\(compassManager.step)" + "\(L("step"))")
                    
                    Rectangle().fill(Color.customLine).frame(maxWidth: .infinity, maxHeight: 1)
                    
                    InfoView(imagename:"data_lat",title: L("Latitude"), value:"\(convertCoordinatesToDMS(latitudeAsDouble: String(compassManager.latitude), longitudeAsDouble: String(compassManager.longitude)).latitudeDMS)")
                    
                    Rectangle().fill(Color.customLine).frame(maxWidth: .infinity, maxHeight: 1)
                    
                    InfoView(imagename:"data_cb",title: L("Magnetic North"), value: String(format: "%.0f", compassManager.magneticHeading))
                }
            }
            .padding(.all,15.0)
            .padding(.top,-15)
            
            Spacer().frame(height: 20.0)

        }
        .gradientBackground()
       
//        .navigationBarHidden(true)
    }

    // Function to adjust bar heights
    func barHeight(for index: Int) -> CGFloat {
        let values = [200, 200, 300, 200, 300, 300, 200]
        return CGFloat(values[index]) / 5
    }
}

struct DataView_Previews: PreviewProvider {
    static var previews: some View {
        DataView()
    }
}
