package com.swago.user

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.swago.baseswago.activity.BaseXActivity
import com.swago.baseswago.util.AppContext
import com.swago.lib_country_picker.Country
import com.swago.lib_country_picker.Language
import com.swago.lib_country_picker.SideBar
import com.swago.user.adapter.CAdapter
import com.swago.user.databinding.ActivityCountryBinding

/**
 *@date 2021/11/20 16:32
 *description:
 */
class CountryActivity : BaseXActivity<ActivityCountryBinding>() {

    private val allCountries = ArrayList<Country>()
    private val selectedCountries = ArrayList<Country>()

    override fun loadData() {}

    override fun initOther() {
        setBackView(binding.toolbar.ivBack)
        binding.toolbar.tvTitle.text = AppContext.getContext().resources.getString(R.string.selecte_country)

        Country.load(this,Language.ENGLISH)
        allCountries.clear()
        allCountries.addAll(Country.getAll())
        selectedCountries.clear()
        selectedCountries.addAll(allCountries)

        val adapter = CAdapter(selectedCountries)
        val manager =LinearLayoutManager(this)
        binding.rv.layoutManager =  manager
        binding.rv.adapter = adapter
        binding.rv.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.VERTICAL))

        binding.sideBar.addIndex("#",binding.sideBar.indexes.size)
        binding.sideBar.onLetterChangeListener = object:SideBar.OnLetterChangeListener{
            override fun onLetterChange(letter: String?) {
                binding.tvLetter.visibility = View.VISIBLE
                binding.tvLetter.text = letter?:""
                val position = adapter.getLetterPosition(letter)
                if (position!= -1){
                    manager.scrollToPositionWithOffset(position,0)
                }
            }

            override fun onReset() {
                binding.tvLetter.visibility = View.GONE
            }

        }

        adapter.setOnClickListener {
            val data = Intent()
            data.putExtra("country", it)
            setResult(RESULT_OK, data)
            finish()
        }
    }

    override fun initLiveData() {}


}