package com.bc_chat.contacts.ui.search

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import com.bc_chat.contacts.R
import com.zhaohaoting.framework.abs.contract.BaseContract
import com.zhaohaoting.framework.abs.presenter.AbsActivity
import com.zhaohaoting.framework.utils.SimpleTextWatcher
import kotlinx.android.synthetic.main.search_layout.*

/**
 * author：Created by zhaohaoting on 2019/3/1
 * email：526309416@qq.com
 * desc：搜索
 */

abstract class SearchActivity : AbsActivity<BaseContract.BasePresenter>() {

    var mAdapter: RecyclerView.Adapter<out RecyclerView.ViewHolder>? = null

    override fun initPresenter(): BaseContract.BasePresenter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.search_layout)
        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        val searchView = findViewById<EditText>(R.id.et_search)
        toolbar.setNavigationOnClickListener { v -> closeActivity() }
        searchView.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                query(v.text.toString())
            }
            return@setOnEditorActionListener false
        }
        searchView.addTextChangedListener(object : SimpleTextWatcher() {
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                query(s.toString())
            }
        })
        recycler_view.setHasFixedSize(true)
        recycler_view.layoutManager = LinearLayoutManager(this)
        init();
        mAdapter = getAdapter()
        recycler_view.adapter = mAdapter
    }

    abstract fun init()

    abstract fun getAdapter(): RecyclerView.Adapter<*>?

    abstract fun query(query: String?)
}
