package com.rehome.bhdxj.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.rehome.bhdxj.R
import com.rehome.bhdxj.adapter.QxgdSearchAddressAdapter
import com.rehome.bhdxj.base.BaseActivityOaToolbarViewBinding
import com.rehome.bhdxj.bean.QxgdAddressBean
import com.rehome.bhdxj.bean.UserInfo
import com.rehome.bhdxj.contans.Contans
import com.rehome.bhdxj.databinding.ActivityQxgdSearchAddressBinding
import com.rehome.bhdxj.utils.GsonUtils
import com.rehome.bhdxj.utils.HttpListener
import com.rehome.bhdxj.utils.NohttpUtils
import com.rehome.bhdxj.utils.OAToolbar
import com.yolanda.nohttp.NoHttp
import com.yolanda.nohttp.RequestMethod
import com.yolanda.nohttp.rest.Response

class QxgdSearchAddressActivity : BaseActivityOaToolbarViewBinding<ActivityQxgdSearchAddressBinding>() {

    private lateinit var adapter: QxgdSearchAddressAdapter
    private var datas: MutableList<QxgdAddressBean.DataBean> = mutableListOf()



    override fun getViewBinding() = ActivityQxgdSearchAddressBinding.inflate(layoutInflater)

    override fun getToolbar() = binding.toolbarView.toolbar

    override fun initView() {
        initToolbar("选择位置", "") {

        }


        adapter = QxgdSearchAddressAdapter(context, datas
        ) { position ->
            val item: QxgdAddressBean.DataBean = datas[position];
            val resultIntent = Intent()
            val bundle = Bundle()
            bundle.putString("item", GsonUtils.GsonString(item))
            resultIntent.putExtras(bundle)
            setResult(RESULT_OK, resultIntent)
            finish();
        }
        binding.lv.adapter = adapter
        binding.tvNodata.visibility= View.VISIBLE
        binding.lv.visibility= View.GONE

        //QxgdSearchAddressAdapter
        //adapter_qxgd_search_address

        binding.tvSearch.setOnClickListener(View.OnClickListener {
            if(TextUtils.isEmpty(binding.etAddressKey.text.toString().trim())){
                showToast("查询关键字不能为空，请输入查询位置")
            }else{
                //检查网络连接
                checkConnectServer()
            }
        })
    }

    override fun initData() {

    }


    private fun checkConnectServer() {
        var param = HashMap<String,String>()
        val json = GsonUtils.GsonString(param)
        val url = Contans.MAXIMOIP + Contans.check_server_connect
        showLog(url)
        showLog(json)
        val request = NoHttp.createStringRequest(
            url,
            RequestMethod.POST
        )
        request.setDefineRequestBodyForJson(json)
        NohttpUtils.getInstance().add(this, 0, request, object : HttpListener<String?> {
            override fun onSucceed(what: Int, response: Response<String?>?) {
                val result = response?.get()
                if (result != null) {
                    showLog("-----checkConnectServer onSucceed----")
                    showLog(result)
                    if(result == "1"){
                        showLog("connect server success")
                        //查询位置
                        searchData()
                    }else{
                        showToast("无法连接到服务器，请检查网络环境")
                    }
                }
            }

            override fun onFailed(what: Int, response: Response<String?>?) {
                showLog("connect server onFailed")
                showToast("无法连接到服务器，请检查网络环境")
            }
        }, true, true, "连接服务器...")
    }

    private fun searchData() {
        val param = HashMap<String,String>()
        param["action"]="QXGD_WZSB_GET"
        param["ms"]=binding.etAddressKey.text.toString().trim()
        val json = GsonUtils.GsonString(param)
        val url = Contans.MAXIMOIP + Contans.QX_GET_ZY_DROP
        showLog(url)
        showLog(json)
        val request = NoHttp.createStringRequest(
            url,
            RequestMethod.POST
        )
        request.setDefineRequestBodyForJson(json)
        NohttpUtils.getInstance().add(this, 0, request, object : HttpListener<String?> {
            override fun onSucceed(what: Int, response: Response<String?>?) {
                val result = response?.get()
                showLog(result)
                val qxgdAddressBean = GsonUtils.GsonToBean(result, QxgdAddressBean::class.java)
                if (qxgdAddressBean != null&&qxgdAddressBean.state==1&&qxgdAddressBean.data!=null&&qxgdAddressBean.data.size>0) {
                    binding.tvNodata.visibility= View.GONE
                    binding.lv.visibility= View.VISIBLE
                    datas.clear()
                    datas.addAll(qxgdAddressBean.data)
                    adapter.notifyDataSetChanged()
                }else{
                    binding.tvNodata.visibility= View.VISIBLE
                    binding.lv.visibility= View.GONE
                    datas.clear()
                    adapter.notifyDataSetChanged()
                }
            }

            override fun onFailed(what: Int, response: Response<String?>?) {
                showLog("connect server onFailed")
                showToast("查询数据失败,请检查网络环境")
            }
        }, true, true, "连接服务器...")
    }
}