package com.yidian.shenghuoquan.bmanagercenter.ui.store

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.view.KeyEvent
import com.amap.api.services.core.AMapException
import com.amap.api.services.core.PoiItem
import com.amap.api.services.poisearch.PoiResult
import com.amap.api.services.poisearch.PoiSearch
import com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener
import com.yidian.shenghuoquan.bmanagercenter.ui.store.adapter.PoiSearchAdapter
import com.yidian.bcommon.base.BaseActivity
import com.yidian.bcommon.extensions.hide
import com.yidian.bcommon.extensions.initTitleBar
import com.yidian.bcommon.extensions.show
import com.yidian.shenghuoquan.bmanagercenter.databinding.LayoutActivityServiceProductAddrSearchBinding
import com.yidian.bcommon.utils.TextWatcherAdapter

/**
 * 搜索门店地址
 */
class SearchAddActivity : BaseActivity<LayoutActivityServiceProductAddrSearchBinding>(), OnPoiSearchListener {

  private var adapter = PoiSearchAdapter()
  private var resultIntent = Intent()
  private var key = ""
  private var data: List<PoiItem>? = null

  override fun createViewBinding(): LayoutActivityServiceProductAddrSearchBinding {
    return LayoutActivityServiceProductAddrSearchBinding.inflate(layoutInflater)
  }

  override fun getXPageName(): String {
    return ""
  }

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    initTitleBar(viewBind.include.toolbar, viewBind.include.tvTitle, "选择门店地址")
    initList()
    initClick()
  }

  private fun initClick() {
    viewBind.btnHeaderRight.setOnClickListener {
      finish()
    }
    viewBind.etSearchKey.setOnKeyListener { _, keyCode, event ->
      if (event!!.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
        queryPoi()
      }
      false
    }
    viewBind.etSearchKey.addTextChangedListener(object : TextWatcherAdapter() {

      override fun afterTextChanged(s: Editable?) {
        if (s?.length ?: 0 > 0) {
          viewBind.ivClear.show()
        } else {
          viewBind.ivClear.hide()
        }
      }

    })
    viewBind.ivClear.setOnClickListener {
      viewBind.etSearchKey.setText("")
      adapter.setList(null)
      viewBind.poiSearchList.hide()
      viewBind.pvEmptyPage.show()
    }
  }

  private fun initList() {
    viewBind.poiSearchList.adapter = adapter
    adapter.setOnItemClickListener { _, _, position ->
      data?.let {
        resultIntent.putExtra("title", it[position].title)
        resultIntent.putExtra("cityName", it[position].cityName)
        resultIntent.putExtra("adName", it[position].adName)
        resultIntent.putExtra("latitude", it[position].latLonPoint.latitude.toString())
        resultIntent.putExtra("longitude", it[position].latLonPoint.longitude.toString())
        setResult(100, resultIntent)
        finish()
      }
    }
  }

  override fun onPoiSearched(poiResult: PoiResult?, rCode: Int) {
    if (rCode == AMapException.CODE_AMAP_SUCCESS) {
      if (poiResult?.query != null) {
        val poiItems: List<PoiItem> = poiResult.pois // 取得第一页的poiitem数据，页数从数字0开始
        if (poiItems.isNotEmpty()) {
          viewBind.pvEmptyPage.hide()
          viewBind.poiSearchList.show()
          adapter.setList(poiItems)
          data = poiItems
        }
      }
    }
  }

  override fun onPoiItemSearched(p0: PoiItem?, p1: Int) {
  }

  //poi搜索
  private fun queryPoi() {
    val typeRange = "汽车服务|汽车销售|" +
      "汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|" +
      "住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|" +
      "金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施"
    key = viewBind.etSearchKey.text.toString()
    val query = PoiSearch.Query(key, typeRange, "北京")
    query.pageSize = 1000 // 设置每页最多返回多少条poiitem
    query.pageNum = 1 //设置查第一页
    val poiSearch = PoiSearch(this, query)
    poiSearch.setOnPoiSearchListener(this) //设置数据返回的监听器
    poiSearch.searchPOIAsyn()
  }


}
