package com.example.pasos

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.telephony.SmsManager
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat


class MainActivity : AppCompatActivity() {

    private val SEND_SMS_REQ: Int = 100
    private val MY_LOCATION_REQ: Int = 101

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val dangerBttn: Button = findViewById(R.id.dangerBttn)
        val preset1Bttn: Button = findViewById(R.id.preset1Bttn)
        val preset2Bttn: Button = findViewById(R.id.preset2Bttn)
        val preset3Bttn: Button = findViewById(R.id.preset3Bttn)

        CheckPermission(Manifest.permission.SEND_SMS, SEND_SMS_REQ)
        CheckPermission(Manifest.permission.ACCESS_FINE_LOCATION, MY_LOCATION_REQ)

        dangerBttn.setOnClickListener {
            SendSms("[CHUCKLES] I'M IN DANGER")
        }
        preset1Bttn.setOnClickListener {
            SendSms("Voy de regreso a casa.")
        }
        preset2Bttn.setOnClickListener {
            SendSms("Estoy caminando sola/o por la calle.")
        }
        preset3Bttn.setOnClickListener {
            SendSms("Me quede a pie.")
        }
    }

    private fun CheckPermission(perm: String, code: Int) {
        if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, arrayOf(perm), code)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        if (requestCode == SEND_SMS_REQ) {
            if (grantResults.size > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "SMS PERMISSION GRANTED", Toast.LENGTH_SHORT)
            }
            else {
                Toast.makeText(this, "SMS PERMISSION DENIED", Toast.LENGTH_SHORT)
            }
        }
        else if (requestCode == MY_LOCATION_REQ) {
            if (grantResults.size > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "LOCATION PERMISSION GRANTED", Toast.LENGTH_SHORT)
            }
            else {
                Toast.makeText(this, "LOCATION PERMISSION DENIED", Toast.LENGTH_SHORT)
            }
        }
    }

    private fun SendSms(msg: String) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.SEND_SMS), SEND_SMS_REQ)
        }
        else {
            SmsManager.getDefault().sendTextMessage("+17875551234", null, msg, null, null)
            Toast.makeText(this, "SMS Sent", Toast.LENGTH_SHORT).show()
        }
    }

    fun changeActivity(view: View) {
        val intent = Intent(this, Main2Activity::class.java)
        startActivity(intent)
    }
}