package com.example.darshil

import android.content.Intent
import android.database.Cursor
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import android.widget.ListView
import java.sql.ClientInfoStatus

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }


        val view1 = findViewById(R.id.txtans) as TextView
        val savebtn = findViewById(R.id.savebtn) as Button
        val deletebtn = findViewById(R.id.deletebtn) as Button
        val updatebtn = findViewById(R.id.updatebtn) as Button
        val viewbtn = findViewById(R.id.viewbtn) as Button

        var idinput = findViewById(R.id.inputid) as EditText
        var nameinput = findViewById(R.id.inputname) as EditText
        var acnoinput = findViewById(R.id.inputacno) as EditText

        var listview1 = findViewById<ListView>(R.id.listview1)

        savebtn.setOnClickListener(View.OnClickListener {
            
            var databasehelper: databasehelper = databasehelper(this)
            val result: Boolean = databasehelper.addcustomer(
                customer(
                    Integer.parseInt(idinput.text.toString()),
                    nameinput.text.toString(),
                    acnoinput.text.toString()
                )
            )
            if (result == true) {
                Toast.makeText(this, "SAVE data", Toast.LENGTH_LONG).show()
            }

        })

        deletebtn.setOnClickListener(View.OnClickListener {
            var databasehelper: databasehelper = databasehelper(this)
            val result: Boolean = databasehelper.deletecustomer(
                customer(
                    Integer.parseInt(
                        idinput.text.toString()
                    ), "", ""
                )
            )
            if (result == true) {
                Toast.makeText(this, "deletedata", Toast.LENGTH_LONG).show()
            }
        })


        updatebtn.setOnClickListener(View.OnClickListener {
            var databasehelper: databasehelper = databasehelper(this)
            val result: Boolean = databasehelper.updatecustomer(
                customer(
                    Integer.parseInt(idinput.text.toString()),
                    nameinput.text.toString(),
                    acnoinput.text.toString()
                )
            )

            if (result == true) {
                Toast.makeText(this, "update data", Toast.LENGTH_LONG).show()
            }
        })




        viewbtn.setOnClickListener {

            val databasehelper = databasehelper(this)

            val cursor: Cursor = databasehelper.viewcustomer()

            val list = ArrayList<String>()

            if (cursor.count > 0) {
                while (cursor.moveToNext()) {

                    val id = cursor.getInt(0)
                    val name = cursor.getString(1)
                    val acno = cursor.getString(2)

                    list.add("$id - $name ($acno)")
                }
            }

            val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, list)

            listview1.adapter = adapter
        }






    }
}
