package com.example.zonic

import android.Manifest
import android.content.ContentValues.TAG
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.PointF
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.yandex.mapkit.MapKit
import com.yandex.mapkit.MapKitFactory
import com.yandex.mapkit.geometry.Point
import com.yandex.mapkit.layers.ObjectEvent
import com.yandex.mapkit.location.Location
import com.yandex.mapkit.location.LocationListener
import com.yandex.mapkit.location.LocationManager
import com.yandex.mapkit.location.LocationStatus
import com.yandex.mapkit.mapview.MapView
import com.yandex.mapkit.user_location.UserLocationLayer
import com.yandex.mapkit.user_location.UserLocationObjectListener
import com.yandex.mapkit.user_location.UserLocationView


class YandexMap : AppCompatActivity(), UserLocationObjectListener, LocationListener {
    lateinit var mapview:MapView
    lateinit var locationManager: LocationManager
    lateinit var myLocationListener: LocationListener
    lateinit var userLocation: UserLocationLayer
    lateinit var myLocation: Point
    private var sLocation: String? = null
    private var sLocation2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MapKitFactory.setApiKey("28e6d937-fb53-497e-9441-4c3d487b4a06")
        requestLocationPermission()

        MapKitFactory.initialize(this)
        setContentView(R.layout.activity_map_test)
        mapview = findViewById(R.id.mapview)

        var mapKit:MapKit = MapKitFactory.getInstance()
        requestLocationPermission()

        userLocation = mapKit.createUserLocationLayer(mapview.mapWindow)
        userLocation.isVisible = true
        userLocation.setObjectListener(this)

        locationManager = mapKit.createLocationManager()
        sLocation2 = sLocation

    }
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putBoolean("haveApiKey", true)
    }

    override fun onStart() {
        mapview.onStart()
        MapKitFactory.getInstance().onStart()
        super.onStart()
    }

    override fun onStop() {
        mapview.onStop()
        MapKitFactory.getInstance().onStop()
        super.onStop()
    }
    private fun requestLocationPermission(){
        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION), 0)
            return
        }
    }
    override fun onObjectAdded(userLocationView: UserLocationView) {
        userLocation.setAnchor(
            PointF((mapview.width() *0.5).toFloat(), (mapview.height()*0.5).toFloat()),
            PointF((mapview.width() *0.5).toFloat(), (mapview.height()*0.83).toFloat())
        )
    }

    override fun onObjectRemoved(userLocationView: UserLocationView) {
    }

    override fun onObjectUpdated(userLocationView: UserLocationView, objectEvent: ObjectEvent) {
    }

    override fun onLocationUpdated(location: Location) {
        if (myLocation == null) {
            //moveCamera(location.getPosition(), COMFORTABLE_ZOOM_LEVEL)
        }
        myLocation = location.getPosition() //this user point
        sLocation = myLocation.getLatitude().toString() + "," + myLocation.getLongitude()
        Log.w(TAG, "my location - " + myLocation.getLatitude() + "," + myLocation.getLongitude())
    }

    override fun onLocationStatusUpdated(locationStatus: LocationStatus) {
        if (locationStatus == LocationStatus.NOT_AVAILABLE) {
            Log.w("Error: ", "LOCATION NOT FOUND");
        }
        if (locationStatus == LocationStatus.AVAILABLE){
            Log.w("OK: ", "LOCATION FOUND");
        }
    }
    fun clickAdd(view: View){
        try {
            val intent = Intent(this, AddAnimal::class.java)
            startActivity(intent)
        }
        catch(e:Exception){
            Log.e("Тема ошибки", "убубу" + e.message)
            Toast.makeText(this, "Не получилось перейти, увы", Toast.LENGTH_SHORT).show()
        }
    }
    fun viewClick(view: View){
        try {
            val intent = Intent(this, ViewAnimal::class.java)
            startActivity(intent)
        }
        catch(e:Exception){
            Log.e("Тема ошибки", "убубу" + e.message)
        }
    }
}