//
//  MapRepresentable.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/18.
//

import Foundation
import SwiftUI
import MapboxMaps
import UIKit

struct MapRepresentable: UIViewControllerRepresentable{
    
    let mapVC = MapViewController()
    func makeUIViewController(context: Context) -> some UIViewController {
        mapVC
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        
    }
}

class MapViewController : UIViewController{
    
    internal var mapView: MapView!
    
    public var ownShipSymbol = MapSource(layer: .OwnShip)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let centerCoordinate = CLLocationCoordinate2D(latitude: 37.8, longitude: -96)
        let options = MapInitOptions(cameraOptions: CameraOptions(center: centerCoordinate,
                                                                  zoom: 2))

        mapView = MapView(frame: view.bounds, mapInitOptions: options)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        
        self.view.addSubview(mapView)

        
        mapView.mapboxMap.onNext(event: .mapLoaded) { [self] _ in
            self.addLayers()
            
            self.updateOwnShip(location: CLLocationCoordinate2D(latitude: 45, longitude: 100))
        }
        
    }
    
    func addLayers(){
        
        try! mapView.mapboxMap.style.addImage(UIImage(named: "account")!, id: IconImage.OwnShip.rawValue)

        let point = Point(LocationCoordinate2D(latitude: 0, longitude: 0))
        var feature = Feature(geometry: point)
//        feature.properties = [PropertyKey.IconImage.rawValue: .string(IconImage.OwnShip.rawValue)]
        ownShipSymbol.source.data = .feature(feature)
        var ownShipSymbolLayer = SymbolLayer(id: ownShipSymbol.layerId)
        ownShipSymbolLayer.source = ownShipSymbol.sourceId
        ownShipSymbolLayer.iconImage = .expression(Exp(.get) {
            PropertyKey.IconImage.rawValue
        })
        ownShipSymbolLayer.iconRotationAlignment = .constant(.map)
        ownShipSymbolLayer.iconAllowOverlap = .constant(true)
        ownShipSymbolLayer.iconIgnorePlacement = .constant(true)
        ownShipSymbolLayer.iconRotate = .expression(Exp(.get) {
            PropertyKey.Bearing.rawValue
        })

        try! mapView.mapboxMap.style.addSource(ownShipSymbol.source, id: ownShipSymbol.sourceId)
        try? mapView.mapboxMap.style.addLayer(ownShipSymbolLayer)
        
    }
    
    func updateOwnShip(location: CLLocationCoordinate2D){
        var geoJSON = Feature(geometry: Point(location))
        geoJSON.properties = [PropertyKey.Bearing.rawValue: .number(90),
                              PropertyKey.IconImage.rawValue: .string(IconImage.OwnShip.rawValue)]
        try! self.mapView.mapboxMap.style.updateGeoJSONSource(withId: self.ownShipSymbol.sourceId,
                                                              geoJSON: .feature(geoJSON))
    }

    
    
    
    enum PropertyKey : String {
        case IconImage
        case Bearing
    }
    
    enum IconImage: String, CaseIterable{
        case OwnShip
    }
}

