package com.waytous.anticollision.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.LogUtils
import com.mapbox.geojson.Point
import com.mapbox.maps.CameraOptions
import com.mapbox.maps.MapView
import com.mapbox.maps.Style
import com.mapbox.maps.extension.observable.eventdata.MapLoadingErrorEventData
import com.mapbox.maps.extension.style.layers.generated.lineLayer
import com.mapbox.maps.extension.style.layers.properties.generated.LineCap
import com.mapbox.maps.extension.style.layers.properties.generated.LineJoin
import com.mapbox.maps.extension.style.sources.TileSet
import com.mapbox.maps.extension.style.sources.generated.geoJsonSource
import com.mapbox.maps.extension.style.style
import com.mapbox.maps.plugin.delegates.listeners.OnMapLoadErrorListener
import com.mapbox.maps.renderer.RendererError
import com.mapbox.maps.renderer.RendererSetupErrorListener
import com.waytous.anticollision.databinding.FragmentHomeBinding

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val homeViewModel =
            ViewModelProvider(this)[HomeViewModel::class.java]

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root
        val mapView: MapView = binding.mapView
//        val mapView = MapView(this.context!!)
        mapView.getMapboxMap().setCamera(
            CameraOptions.Builder()
//                .center(
//                Point.fromLngLat(
//                    LATITUDE,
//                    LONGITUDE
//                ))
                .zoom(ZOOM).build()
        )
        mapView.getMapboxMap().addOnMapLoadErrorListener(object :OnMapLoadErrorListener{
            override fun onMapLoadError(eventData: MapLoadingErrorEventData) {
                LogUtils.e("OnMapLoadErrorListener:${eventData.message}")
            }

        })
        mapView.addRendererSetupErrorListener {
                error -> LogUtils.e("RendererSetupErrorListener:${error}")
        }
        mapView.getMapboxMap().loadStyle(style(Style.SATELLITE_STREETS){
            +geoJsonSource(GEOJSON_SOURCE_ID)
            {
                url("asset://xian.geojson")
            }
//            +lineLayer("linelayer", GEOJSON_SOURCE_ID) {
//                lineCap(LineCap.ROUND)
//                lineJoin(LineJoin.ROUND)
//                lineOpacity(0.7)
//                lineWidth(8.0)
//                lineColor("#888")
//            }
        })
        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        private const val GEOJSON_SOURCE_ID = "tile"
        private const val LATITUDE = 108.926593
        private const val LONGITUDE = 34.213389
        private const val ZOOM = 14.0
    }
}