package com.example.zonic

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.Spinner
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.example.zonic.ConnectionHelper.Connection
import java.sql.Connection
import java.sql.Statement
import java.sql.Timestamp
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

private val dateTime: Timestamp? = null
private val v: View? = null


class AddAnimal : AppCompatActivity() {
    private var selectType: String? = null
    private var description: String? = null
    private var location: String? = null
    private var connectionHelper: ConnectionHelper? = null
    private var connection: Connection? = null
    private var dTime: LocalDateTime = LocalDateTime.now()
    private var datta: String? = null
    private var formatter: DateTimeFormatter =  DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_info_animal)
        dTime = LocalDateTime.now()
        datta = dTime.format(formatter)
        val animalArray = resources.getStringArray(R.array.animals_types_array)

        // access the spinner
        val spinner = findViewById<Spinner>(R.id.Animal)
        if (spinner != null) {
            val adapter = ArrayAdapter(
                this,
                android.R.layout.simple_spinner_item, animalArray
            )
            spinner.adapter = adapter

            spinner.onItemSelectedListener = object :
                AdapterView.OnItemSelectedListener {
                override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                    selectType = spinner.selectedItem.toString()
                }

                override fun onNothingSelected(parent: AdapterView<*>) {
                    // write code to perform some action
                }
            }
        }
    }

    fun addClick(view: View){
        try {
            val descriptionET: EditText = findViewById<EditText?>(R.id.AnimalDef)
            description = descriptionET.text.toString()
            val locationET: EditText = findViewById<EditText?>(R.id.locationDe)
            location = locationET.text.toString()
            PostData()
        }
        catch(e:Exception){
            Log.e("Ооопс, ошибочка", "убубу" + e.message)
        }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun PostData() {
        try {
            connectionHelper = ConnectionHelper
            connection = Connection()
            val query =
                ("Insert Into ZonicInfo([Location], Animal, [Description], [DateTime]) Values('" + location + "', '" + selectType + "', '" + description + "', '" + datta + "')")
            if (connection != null) {
                val statement: Statement = connection!!.createStatement()
                statement.execute(query)
                connection!!.close()
                Toast.makeText(this, "Data was uploaded data", Toast.LENGTH_SHORT).show()
                onBackPressed()
            }
        } catch (ex: java.lang.Exception) {
            Toast.makeText(this, "Failed to upload data", Toast.LENGTH_SHORT).show()
        }
    }
    fun backkClick(view: View) {
        onBackPressed()
    }
}