package com.example.notepadapp
import android.app.SearchManager
import android.content.Context
import android.content.Intent
import android.icu.text.CaseMap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.*
import android.widget.BaseAdapter
import android.widget.Toast
import android.widget.SearchView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.ticitet.view.*
class MainActivity : AppCompatActivity() {
var listofnode =  ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// listofnode.add(Note(1,"have to go out side ","take new shoe for market "))
//istofnode.add(Note(2,“football”, “play football with friends”))
//listofnode.add(Note(3,“gym”,“go gym at 5PM”))
    loadQuery("%")
}
fun loadQuery(title:String){
    var dbmanager= Dbmanager(this)
    val projection = arrayOf("ID","Title","Description")
    val selectionArgs =   arrayOf(title)
    val cursor = dbmanager.Query(projection,"title like ?",selectionArgs,"title")
    listofnode.clear()
    if(cursor.moveToFirst()) {
        do {
            val ID = cursor.getInt(cursor.getColumnIndex("ID"))
            val Title = cursor.getString(cursor.getColumnIndex("Title"))
            val Description = cursor.getString(cursor.getColumnIndex(" Description "))
             listofnode.add(Note(ID,Title,Description))
        }while(cursor.moveToNext())
    }
    var nodeadpater = noteAdapter(listofnode)
    list.adapter = nodeadpater
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.mainmenu,menu)
    val sv:SearchView = menu!!.findItem(R.id.app_bar_search).actionView as SearchView
    val sm = getSystemService(Context.SEARCH_SERVICE) as SearchManager
    sv.setSearchableInfo(sm.getSearchableInfo(componentName))
    sv.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
        override fun onQueryTextSubmit(query: String?): Boolean {
            Toast.makeText(applicationContext,query,Toast.LENGTH_LONG).show()
            loadQuery("%"+ query +"%")
            return false
        }
        override fun onQueryTextChange(newText: String?): Boolean {
            return false
        }
    })
    return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if(item!= null){
        when(item.itemId){
            R.id.te->{
                var intent = Intent(this,MainActivity2::class.java)
                startActivity(intent)
            }
        }
    }
    return super.onOptionsItemSelected(item)
}
inner class noteAdapter:BaseAdapter{
var listofnode = ArrayList()
constructor(listofnode:ArrayList):super(){
this.listofnode = listofnode
}
   override fun getCount(): Int {
       return listofnode.size
   }
   override fun getItem(position: Int): Any {
       return listofnode[position]
   }
   override fun getItemId(position: Int): Long {
       return position.toLong()
   }
   override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
       var myview = layoutInflater.inflate( R.layout.ticitet,null)
       var mynode = listofnode[position]
       myview.textView.text = mynode.notename
       myview.textView2.text = mynode.notedes
       return myview
   }
}
}