package com.rehome.bhdxj.activity

import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.rehome.bhdxj.base.BaseActivityOaToolbarViewBinding
import com.rehome.bhdxj.bean.ZyNewBean
import com.rehome.bhdxj.contans.Contans
import com.rehome.bhdxj.databinding.ActivityQxgdNewBinding
import com.rehome.bhdxj.utils.GsonUtils
import com.rehome.bhdxj.utils.HttpListener
import com.rehome.bhdxj.utils.NohttpUtils
import com.rehome.bhdxj.utils.RSAUtils
import com.rehome.bhdxj.utils.SPUtils
import com.rehome.bhdxj.weight.ListDialog
import com.yolanda.nohttp.NoHttp
import com.yolanda.nohttp.RequestMethod
import com.yolanda.nohttp.rest.OnResponseListener
import com.yolanda.nohttp.rest.RequestQueue
import com.yolanda.nohttp.rest.Response

class QxgdNewActivity : BaseActivityOaToolbarViewBinding<ActivityQxgdNewBinding>() {

    private lateinit var queueNoHttp: RequestQueue
    private lateinit var username: String
    private lateinit var usernames: String
    private val jzList: MutableList<ZyNewBean.DataBean> = mutableListOf()
    private val zyList: MutableList<ZyNewBean.DataBean> = mutableListOf()
    private val gzlxList: MutableList<ZyNewBean.DataBean> = mutableListOf()
    private val gdjbList: MutableList<ZyNewBean.DataBean> = mutableListOf()
//    private val zyList: MutableList<ZyNewBean.DataBean> = mutableListOf()
//    private val zyList: MutableList<ZyNewBean.DataBean> = mutableListOf()



    private val jzDialogDatas: MutableList<String> = mutableListOf()
    private val zyDialogDatas: MutableList<String> = mutableListOf()
    private val gzlxDialogDatas: MutableList<String> = mutableListOf()
    private val gdjbDialogDatas: MutableList<String> = mutableListOf()

    private var jzValue: String? = null
    private var zyValue: kotlin.String? = null
    private var lxvalue: kotlin.String? = null
    private var qxdjValue: kotlin.String? = null

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

    override fun getToolbar() = binding.toolbarView.toolbar

    override fun initView() {
        initToolbar("缺陷工单提交", "提交") {

        }

        queueNoHttp = NoHttp.newRequestQueue(1)
        val userIdTemp = SPUtils.get(this, Contans.USERID, "") as String
        if (!TextUtils.isEmpty(userIdTemp)) {
            username = RSAUtils.decryptBASE64StrLocal(userIdTemp);
        }else{
            username = ""
        }
        usernames = SPUtils.get(this, Contans.USERNAME, "") as String
        if(!TextUtils.isEmpty(usernames)){
            binding.ilFxr.content = usernames
        }

        binding.etQxms.setContentTextSize(54)

        binding.ilJz.setOnClickListener(View.OnClickListener { v: View? ->
            val dialog = ListDialog(
                context, jzDialogDatas
            ) { _: String?, position: Int ->
                jzValue = jzList[position].value
                binding.ilJz.content = jzList[position].description
            }
            dialog.show()
        })

        binding.ilZy.setOnClickListener(View.OnClickListener { v: View? ->
            val dialog = ListDialog(
                context, zyDialogDatas
            ) { _: String?, position: Int ->
                zyValue = zyList[position].value
                binding.ilZy.content = zyList[position].description
            }
            dialog.show()
        })

        binding.ilQxlx.setOnClickListener(View.OnClickListener { v: View? ->
            val dialog = ListDialog(
                context, gzlxDialogDatas
            ) { _: String?, position: Int ->
                lxvalue = gzlxList[position].value
                binding.ilQxlx.content = gzlxList[position].description
            }
            dialog.show()
        })

        binding.ilQxdj.setOnClickListener(View.OnClickListener { v: View? ->
            val dialog = ListDialog(
                context, gdjbDialogDatas
            ) { _: String?, position: Int ->
                qxdjValue = gdjbList[position].value
                binding.ilQxdj.content = gdjbList[position].description
            }
            dialog.show()
        })

        binding.ilWz.setOnClickListener(View.OnClickListener { v: View? ->
            val intent = Intent(context, QxgdSearchAddressActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
            startActivity(intent)
        })




    }

    override fun initData() {
        //检查网络连接
        checkConnectServer()
    }

    private fun initJZdatas() {
        jzDialogDatas.clear()
        for (bean in jzList) {
            jzDialogDatas.add(bean.description)
        }
    }

    private fun initZYdatas() {
        zyDialogDatas.clear()
        for (bean in zyList) {
            zyDialogDatas.add(bean.description)
        }
    }

    private fun initLXdatas() {
        gzlxDialogDatas.clear()
        for (bean in gzlxList) {
            gzlxDialogDatas.add(bean.description)
        }
    }

    private fun initQXDJdatas() {
        gdjbDialogDatas.clear()
        for (bean in gdjbList) {
            gdjbDialogDatas.add(bean.description)
        }
    }

    private fun getDropDown(type:String) {
        var param = HashMap<String,String>()
        if(type=="jz"){
            //机组
            param["action"] = "QXGD_JZ_GET";
        }else if(type=="zy"){
            //专业
            param["action"] = "QXGD_ZY_GET";
        }else if(type=="gdjb"){
            //工单级别
            param["action"] = "QXGD_QXDJ_GET";
        }else if(type=="gzlx"){
            //工作类型
            param["action"] = "QXGD_GZLX_GET";
        }else if(type=="gzlb"){
            //故障类别
            param["action"] = "QXGD_GZLB_GET";
        }else if(type=="gzxx"){
            //故障现象
            param["action"] = "QXGD_GZXX_GET";
        }

        val json = GsonUtils.GsonString(param)


        val url = Contans.MAXIMOIP + Contans.QX_GET_ZY_DROP
        showLog(url)
        val request = NoHttp.createStringRequest(url, RequestMethod.POST)
        request.setDefineRequestBodyForJson(json)

        queueNoHttp.add(12, request, object : OnResponseListener<String> {
            override fun onStart(what: Int) {

            }

            override fun onSucceed(what: Int, response: Response<String>?) {
                val result = response?.get()
                showLog(result)

                val bean = GsonUtils.GsonToBean(
                    result,
                    ZyNewBean::class.java
                )
                if (bean != null && bean.state == 1 && bean.data != null&& bean.data.size>0) {

                    if(type=="jz"){
                        //机组
                        showLog("机组")
                        jzList.clear()
                        jzList.addAll(bean.data)
                        initJZdatas()
                    }else if(type=="zy"){
                        //专业
                        showLog("专业")
                        zyList.clear()
                        zyList.addAll(bean.data)
                        initZYdatas()
                    }else if(type=="gdjb"){
                        //工单级别
                        showLog("工单级别")
                        gdjbList.clear()
                        gdjbList.addAll(bean.data)
                        initQXDJdatas()
                    }else if(type=="gzlx"){
                        //工作类型
                        showLog("工作类型")
                        gzlxList.clear()
                        gzlxList.addAll(bean.data)
                        initLXdatas()
                    }else if(type=="gzlb"){
                        //故障类别
                        showLog("故障类别")
                    }else if(type=="gzxx"){
                        //故障现象
                        showLog("故障现象")
                    }
                }
                /**
                 * {"state": 1,"msg":"查询成功","data":[
                 * {"description":"保卫","value":"保卫"},
                 * {"description":"电气","value":"电气"},
                 * {"description":"辅助","value":"辅助"},
                 * {"description":"辅助维护","value":"辅助维护"},
                 * {"description":"锅炉","value":"锅炉"},
                 * {"description":"化验","value":"化验"},
                 * {"description":"环化","value":"环化"},
                 * {"description":"机组启停","value":"机组启停"},
                 * {"description":"金属","value":"金属"},
                 * {"description":"其他","value":"其他"},
                 * {"description":"汽机","value":"汽机"},
                 * {"description":"燃料","value":"燃料"},
                 * {"description":"燃料电仪","value":"燃料电仪"},
                 * {"description":"燃料机械","value":"燃料机械"},
                 * {"description":"热控","value":"热控"},
                 * {"description":"生产公用","value":"生产公用"},
                 * {"description":"生技","value":"生技"},
                 * {"description":"土建","value":"土建"},
                 * {"description":"消防","value":"消防"},
                 * {"description":"信息","value":"信息"},
                 * {"description":"运行","value":"运行"},
                 * {"description":"运行A值","value":"运行A值"},
                 * {"description":"运行B值","value":"运行B值"},
                 * {"description":"运行C值","value":"运行C值"},
                 * {"description":"运行D值","value":"运行D值"},
                 * {"description":"运行E值","value":"运行E值"},
                 * {"description":"综合服务","value":"综合服务"}]}
                 */

                //专业
//                if(type=="zy"){
//                    val zyQxData = GsonUtils.GsonToBean(result, ZyQxData::class.java)
//                    if (zyQxData != null&&zyQxData.state == 1) {
//                        if (zyQxData.data != null&&zyQxData.data.size > 0) {
//                            showLog(zyQxData.data.size.toString())
//                            val zyList = DataSupport.findAll(Zy::class.java)
//                            if(zyList!=null&&zyList.size>0){
//                                showLog("zyList size > 0")
//                                showLog(GsonUtils.GsonString(zyList))
//                                showLog(zyList.size.toString())
//                                DataSupport.deleteAll(Zy::class.java)
//                                DataSupport.saveAll(zyQxData.data)
//                            }else{
//                                showLog("zyList size == 0")
//                                DataSupport.saveAll(zyQxData.data)
//                            }
//                        }
//                    }
//                }
            }

            override fun onFailed(what: Int, response: Response<String>?) {

            }

            override fun onFinish(what: Int) {

            }
        })

    }

    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")
                        //缺陷工单下拉数据
                        //机组
                        getDropDown("jz")
                        //专业
                        getDropDown("zy")
                        //工单级别
                        getDropDown("gdjb")
                        //工作类型
                        getDropDown("gzlx")
                    }else{
                        showToast("无法连接到服务器，请检查网络环境")
                    }
                }
            }

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

    private fun checkConnectServerGzlb() {
        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")
                        //缺陷工单下拉数据
                        //故障类别
                        getDropDown("gzlb")
                    }else{
                        showToast("无法连接到服务器，请检查网络环境")
                    }
                }
            }

            override fun onFailed(what: Int, response: Response<String?>?) {
                showLog("connect server onFailed")
                showToast("无法连接到服务器，请检查网络环境")
            }
        }, true, true, "连接服务器...")
    }
    private fun checkConnectServerGzxx() {
        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")
                        //缺陷工单下拉数据
                        //故障现象
                        getDropDown("gzxx")
                    }else{
                        showToast("无法连接到服务器，请检查网络环境")
                    }
                }
            }

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