package com.lukouguoji.gnj.activity

import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.core.widget.NestedScrollView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.fastjson.JSONObject
import com.lukouguoji.gnj.R
import com.lukouguoji.gnj.adapt.GnjQueryInfoListAdapter
import com.lukouguoji.gnj.adapt.GnjQueryInfoWhListAdapter
import com.lukouguoji.gnj.model.GnjQueryInfo
import com.lukouguoji.gnj.model.GnjQueryInfoWh
import com.lukouguoji.gnj.viewModel.GnjQueryInfoViewModel
import com.lukouguoji.module_base.BaseActivity
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.router.ARouterConstants

@Route(path = ARouterConstants.ACTIVITY_URL_GNJ_QUERY_INFO)
class GnjQueryInfoActivity : BaseActivity(), View.OnClickListener {

    private lateinit var viewModel: GnjQueryInfoViewModel

    private lateinit var scrollView: NestedScrollView
    private lateinit var gncQueryInfoList: RecyclerView
    private lateinit var adapter: GnjQueryInfoListAdapter
    private val gncQueryInfoAdapterList = arrayListOf<GnjQueryInfo>()

    private lateinit var submit: TextView

    private lateinit var refWareHouseInfo: LinearLayout
    private lateinit var wareHouseInfoLayout: LinearLayout
    private lateinit var gncQueryWareHouseInfoList: RecyclerView
    private lateinit var wareHouseInfoAdapter: GnjQueryInfoWhListAdapter
    private val gncQueryWareHouseInfoAdapterList = arrayListOf<GnjQueryInfoWh>()

    @JvmField
    @Autowired
    var id: Int = -9999

    @JvmField
    @Autowired
    var wbNoParam: String? = null

    private fun initView() {
        viewModel = ViewModelProvider(this).get(GnjQueryInfoViewModel::class.java)

        scrollView = findViewById(R.id.scrollView)
        refWareHouseInfo = findViewById(R.id.refWareHouseInfo)
        wareHouseInfoLayout = findViewById(R.id.wareHouseInfoLayout)
        submit = findViewById(R.id.submit)

        val layoutManager = GridLayoutManager(this, 2)
        adapter = GnjQueryInfoListAdapter(this, gncQueryInfoAdapterList)
        gncQueryInfoList = findViewById(R.id.gncQueryInfoList)
        gncQueryInfoList.adapter = adapter
        gncQueryInfoList.layoutManager = layoutManager

        val wareHouseLayoutManager = LinearLayoutManager(this)
        wareHouseInfoAdapter = GnjQueryInfoWhListAdapter(this, gncQueryWareHouseInfoAdapterList)
        gncQueryWareHouseInfoList = findViewById(R.id.gncQueryWareHouseInfoList)
        gncQueryWareHouseInfoList.adapter = wareHouseInfoAdapter
        gncQueryWareHouseInfoList.layoutManager = wareHouseLayoutManager

        refWareHouseInfo.setOnClickListener(this)
        submit.setOnClickListener(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_gnj_query_info)
        setBackArrow("国内进港查询详情")
        initView()

        viewModel.queryWaybillById(id, wbNoParam)
        viewModel.waybillLiveData.observe(this) {
            val status = it.getString("status")
            if (Constant.Result.succ == status) {
                viewModel.wayBillInfo = it.getJSONObject("data")
                //主运单详情
                val maWbObj = viewModel.wayBillInfo.getJSONObject("maWb")
                adapter.append(GnjQueryInfo(1, "运单号", maWbObj.getString("wbNo") ?: ""))
                viewModel.wbNo = maWbObj.getString("wbNo")
                adapter.append(GnjQueryInfo(1, "件数", maWbObj.getIntValue("pc").toString()))
                adapter.append(
                    GnjQueryInfo(
                        1,
                        "重量",
                        "${maWbObj.getDoubleValue("weight")} KG"
                    )
                )
                adapter.append(GnjQueryInfo(1, "承运人", maWbObj.getString("by1") ?: ""))
                adapter.append(GnjQueryInfo(1, "目的港", maWbObj.getString("dest") ?: ""))
                adapter.append(GnjQueryInfo(1, "品名", maWbObj.getString("goods") ?: ""))
                adapter.append(GnjQueryInfo(1, "特码", maWbObj.getString("spCode") ?: ""))
                adapter.append(GnjQueryInfo(1, "代理", maWbObj.getString("agentCode") ?: ""))
                adapter.append(GnjQueryInfo(1, "运单类型", maWbObj.getString("awbType") ?: ""))
                adapter.append(GnjQueryInfo(1, "业务类型", maWbObj.getString("businessType") ?: ""))
                adapter.append(GnjQueryInfo(1, "包装类型", maWbObj.getString("packageType") ?: ""))
                /*adapter.append(GnjQueryInfo(1, "卸货港", maWbObj.getString("dest1") ?: ""))
                adapter.append(GnjQueryInfo(1, "危险品描述", maWbObj.getString("dgrDetail") ?: ""))
                adapter.append(GnjQueryInfo(1, "危险品库位", maWbObj.getString("dgrLocation") ?: ""))
                adapter.append(GnjQueryInfo(1, "航班", maWbObj.getString("flight") ?: ""))
                adapter.append(GnjQueryInfo(1, "入库时间", maWbObj.getString("opDate") ?: ""))
                adapter.append(GnjQueryInfo(1, "出库时间", maWbObj.getString("mclose") ?: ""))
                adapter.append(GnjQueryInfo(1, "收运人", maWbObj.getString("opId") ?: ""))*/

                //关联仓库详情
                val whListArr = viewModel.wayBillInfo.getJSONArray("whList")
                whListArr.forEach { waybill ->
                    val waybillObj = waybill as JSONObject
                    wareHouseInfoAdapter.append(
                        GnjQueryInfoWh(
                            1,
                            waybillObj.getString("wbNo") ?: "",
                            waybillObj.getString("location") ?: "",
                            waybillObj.getIntValue("pc"),
                            waybillObj.getDoubleValue("weight"),
                            waybillObj.getString("flight") ?: "",
                            waybillObj.getString("opDate") ?: "",
                            waybillObj.getString("chargeTime") ?: "",
                            waybillObj.getString("opId") ?: ""
                        )
                    )
                }

            } else {
                //无数据
            }
        }

    }

    override fun onClick(v: View?) {
        v?.let {
            when (it.id) {
                R.id.refWareHouseInfo -> {
                    //开关关联列表
                    if (wareHouseInfoLayout.visibility == View.GONE) {
                        wareHouseInfoLayout.visibility = View.VISIBLE
                    } else {
                        wareHouseInfoLayout.visibility = View.GONE
                    }
                    scrollView.post { scrollView.scrollTo(0, scrollView.bottom) }
                }
                R.id.submit -> {
                    ARouter.getInstance().build(ARouterConstants.ACTIVITY_URL_CARGO_TRACKING)
                        .withString("countryTypeParam", Constant.CargoTracking.gnStatus)
                        .withString("ieFlagParam", Constant.CargoTracking.importStatus)
                        .withString("wbNoParam", viewModel.wbNo)
                        .navigation()
                }
                else -> {
                    Toast.makeText(this, "未找到对应操作", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}