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

import SwiftUI

struct MapLayerView: View {
    @State private var selectedLayer: MapLayer = .satellite
    @Environment(\.presentationMode) var presentationMode
       var body: some View {
           VStack(spacing: 30) {
               
               HStack{
                   Button(action: {
                              presentationMode.wrappedValue.dismiss()
                          }) {
                              Image("nav_back")
                                  .foregroundColor(.black)
                          }
                   Spacer()
                   Text(L("SelectLayer"))
                       .font(.system(size: 16, weight: .bold))
                       .foregroundColor(.black)
                   
                   Spacer()
                   
               }
               .padding(.horizontal,15.0)
               
               HStack( alignment: .top,spacing: 15) {

                   Button(action: {
                       selectedLayer = .standard
                       saveLayerSelection(layer: .standard)
                   }) {
                       VStack(alignment: .center,spacing: 10) {
                           RoundedRectangle(cornerRadius: 10)
                               .stroke(selectedLayer == .standard ? Color.customdeepblue : Color.clear, lineWidth: 3)
                           
                               .overlay(
                                   Image("standard_layer")
                                    .resizable()
                                    .clipped()
                                    .aspectRatio(contentMode: .fit)
                                    .clipShape(RoundedRectangle(cornerRadius: 10))
                                   
                               )
                           Text(L("Standard"))
                               .foregroundColor(.black)
                       }.frame( height: 130)
                   }
            
                   Button(action: {
                       selectedLayer = .satellite
                       saveLayerSelection(layer: .satellite)
                   }) {
                       VStack (alignment: .center,spacing: 10){
                           RoundedRectangle(cornerRadius: 10)
                               .stroke(selectedLayer == .satellite ? Color.customdeepblue : Color.clear, lineWidth: 3)
                               
                               .overlay(
                                   Image("satellite_layer")
                                    .resizable()
                                    .clipped()
                                    .aspectRatio(contentMode: .fit)
                                    .clipShape(RoundedRectangle(cornerRadius: 10))
                                   
                               )
                           Text(L("Satellite"))
                               .foregroundColor(.black)
                       }
                       .frame( height: 130)
                   }
               }
             
               .padding(.horizontal, 20)
               
               Spacer()
           }
           .gradientBackground()
           .edgesIgnoringSafeArea(.bottom)
           .navigationBarBackButtonHidden(true)
           .onAppear {
               selectedLayer = loadLayerSelection()
           }
       }
       
       enum MapLayer: String {
           case satellite
           case standard
       }

       func saveLayerSelection(layer: MapLayer) {
           UserDefaults.standard.set(layer.rawValue, forKey: "selectedMapLayer")
       }

       func loadLayerSelection() -> MapLayer {
           let savedLayer = UserDefaults.standard.string(forKey: "selectedMapLayer") ?? MapLayer.satellite.rawValue
           return MapLayer(rawValue: savedLayer) ?? .satellite
       }
    
}


struct MapLayerView_Previews: PreviewProvider {
    static var previews: some View {
        MapLayerView()
    }
}
