package com.example.zonic

import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.zonic.databinding.ActivityViewAnimalBinding
import com.example.zonic.databinding.ItemBinding
import net.sourceforge.jtds.jdbc.DateTime
import java.sql.Connection
import java.sql.Statement
import java.sql.Timestamp

data class ZonicList(
//    var id: Int,
    var location: String,
    var animal: String,
    var description: String,
    var dateTime: Timestamp
)
class ViewAnimal : AppCompatActivity() {
    private var connectionHelper: ConnectionHelper? = null
    private var connection: Connection? = null
    private var _binding: ActivityViewAnimalBinding? = null
    private var listbase: MutableList<ZonicList> = arrayListOf()
    val binding: ActivityViewAnimalBinding
        get() = _binding!!
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = ActivityViewAnimalBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val recyclerView: RecyclerView = findViewById<RecyclerView?>(R.id.listAnimalInfo)
        recyclerView.layoutManager = LinearLayoutManager(this)


        try {
            connectionHelper = ConnectionHelper
            connection = ConnectionHelper.Connection()
            val query = ("SELECT * FROM ZonicInfo")
            val statement = connection!!.prepareStatement(query)
            if (connection != null) {
                val resultSet = statement.executeQuery()
                while (resultSet.next()) {
                    val z = ZonicList(//resultSet.getInt(0),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getTimestamp(5))
                    listbase.add(z)
                }
            }
            setRecyclerView(listbase)
        } catch (ex: java.lang.Exception) {
            Toast.makeText(this, "Failed to upload data", Toast.LENGTH_SHORT).show()
        }
    }
    fun backClick(view: View) {
        onBackPressed()
    }
    class MainRecycleViewHolder(var binding: ItemBinding): RecyclerView.ViewHolder(binding.root)
    class MainRecycleViewAdapter(var infoAnimalCollection: MutableList<ZonicList>): RecyclerView.Adapter<MainRecycleViewHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainRecycleViewHolder {
            var layoutInflater = LayoutInflater.from(parent.context)
            var binding = ItemBinding.inflate(layoutInflater)
            return MainRecycleViewHolder(binding)
        }

        override fun getItemCount(): Int {
            return infoAnimalCollection.size
        }

        override fun onBindViewHolder(holder: MainRecycleViewHolder, position: Int) {
            holder.binding.tvType.text = infoAnimalCollection[position].animal
            holder.binding.tvDescription.text = infoAnimalCollection[position].description
            holder.binding.tvLocation.text = infoAnimalCollection[position].location
            holder.binding.tvDate.text = infoAnimalCollection[position].dateTime.toString()
        }
    }
    fun setRecyclerView(itemCollection: MutableList<ZonicList>){
        binding.listAnimalInfo.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false)
        binding.listAnimalInfo.adapter = MainRecycleViewAdapter(itemCollection)
    }
}