package es.verdnatura.presentation.base

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.location.LocationRequest
import android.os.Bundle
import android.os.Looper
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationResult


interface LocationUpdateCallback {
    fun onLocationReceived(location: LocationResult)
}

abstract class BaseActivity<T : ViewDataBinding> : AppCompatActivity() {

    private val locationCallback = object : LocationCallback() {
        override fun onLocationResult(location: LocationResult) {
            locationUpdateCallback?.onLocationReceived(location)
        }
    }

    var locationUpdateCallback: LocationUpdateCallback? = null

    protected lateinit var binding: T
    private lateinit var fusedLocationClient: com.google.android.gms.location.FusedLocationProviderClient
    private val locationRequest: com.google.android.gms.location.LocationRequest =
        com.google.android.gms.location.LocationRequest.create().apply {
            interval =
                10000
            fastestInterval =
                5000
            priority =
                LocationRequest.QUALITY_LOW_POWER
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutId())
        initDataBinding()
        init()
    }

    @LayoutRes
    abstract fun getLayoutId(): Int
    open fun addBindingVariables() {}
    abstract fun init()


    private fun initDataBinding() {
        binding = DataBindingUtil.setContentView(this, getLayoutId())
        binding.lifecycleOwner = this
        addBindingVariables()
    }

    fun hideKeyboard(view: View) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }

    fun checkLocationPermission(): Boolean {
        return ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.ACCESS_FINE_LOCATION
        ) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.ACCESS_FINE_LOCATION
        ) == PackageManager.PERMISSION_GRANTED
    }

    fun requestLocationUpdates(
        fusedLocationClient: FusedLocationProviderClient
    ) {

        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {

            return
        }

        fusedLocationClient.requestLocationUpdates(
            locationRequest,
            locationCallback,
            Looper.getMainLooper()
        )
    }

    override fun onDestroy() {
        if (::fusedLocationClient.isInitialized) {
            fusedLocationClient.removeLocationUpdates(locationCallback)
        }
        locationUpdateCallback = null
        super.onDestroy()
    }

    fun stopLocationUpdates() {
        locationCallback?.let {
            fusedLocationClient.removeLocationUpdates(it)
        }
    }
}
