package com.example.trolley_app

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import java.net.URL

import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T

import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import org.json.JSONObject
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.util.Log
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import okhttp3.RequestBody
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.R.string
import android.content.Context
import okhttp3.OkHttpClient
import org.json.JSONException
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.location.LocationManager
import android.content.Context.LOCATION_SERVICE
import androidx.core.content.ContextCompat.getSystemService
import androidx.core.app.ComponentActivity
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import androidx.core.app.ActivityCompat


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val permissions = arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION)
        ActivityCompat.requestPermissions(this, permissions,0)

        //Funcion del botdon Paradas que al precionarce, va a llevar al usuario
        // a la pagina de lista de paradas.
        val openParadas:Button = findViewById(R.id.button_paradas)
        openParadas.setOnClickListener {
            val intent = Intent(this, ListaDeParadas::class.java)
            startActivity(intent)

        }

            val btn_click_me:Button = findViewById(R.id.track)
            // set on-click listener
            btn_click_me.setOnClickListener {

                val url = "http://136.145.231.39/sender.php"

                val request = Request.Builder().url(url).build()

                val client =OkHttpClient()

                client.newCall(request).enqueue(object : Callback{

                    override fun onResponse(call: Call, response: Response) {
                        val body = response?.body?.string()
                        println(body)

                    }

                    override fun onFailure(call: Call, e: IOException) {
                        println("no connection")
                    }
                })

            }

            val vamo:Button = findViewById(R.id.sendit)

        vamo.setOnClickListener {
            val lm = getSystemService(Context.LOCATION_SERVICE) as LocationManager
            val location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER)
            val longitude = location.longitude
            val latitude = location.latitude

            println("Esto es: "+latitude)
            println("Esto es: "+longitude)


            val jsonObject = JSONObject()
            try {
                jsonObject.put("longitude", longitude)
                jsonObject.put("latitude", latitude)


            } catch (e: JSONException) {
                e.printStackTrace()
            }


            val client = OkHttpClient()
            val JSON = "application/json; charset=utf-8".toMediaTypeOrNull()

            // put your json here
            val body = RequestBody.create(JSON, jsonObject.toString())
            val request = Request.Builder()
                .url("http://136.145.231.39/json-receiver.php")
                .post(body)
                .build()

            client.newCall(request).enqueue(object : Callback{

                override fun onResponse(call: Call, response: Response) {
                    println("yeee")
                }


                override fun onFailure(call: Call, e: IOException) {
                    println("it sucks dude")
                }
            })


        }



        }
    }
