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

import SwiftUI
import MAMapKit

class MapViewManager: ObservableObject {
    static let shared = MapViewManager()
    var mapView: MAMapView = MAMapView()

    func load() {

        self.mapView.showsCompass = false
        self.mapView.showsScale = false
        self.mapView.showsUserLocation = true
        self.mapView.userTrackingMode = .follow
        let  selectedLayer = UserDefaults.standard.string(forKey: "selectedMapLayer") ?? "satellite"
        self.mapView.mapType = selectedLayer == "satellite" ? .satellite : .standard
    }
    
    func loadUserLoc() {
        self.mapView.showsUserLocation = true
        self.mapView.userTrackingMode = .follow
    }
}


struct MapView: UIViewRepresentable {
    
    @Binding var selectedLayer: String

    func makeUIView(context: Context) -> MAMapView {
        let mapView = MapViewManager.shared.mapView
        return mapView
    }

    func updateUIView(_ mapView: MAMapView, context: Context) {
        mapView.mapType = selectedLayer == "satellite" ? .satellite : .standard
    }
}
