package com.lukouguoji.gnc.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.gnc.R
import com.lukouguoji.gnc.adapt.GncQueryInfoListAdapter
import com.lukouguoji.gnc.adapt.GncQueryInfoWhListAdapter
import com.lukouguoji.gnc.model.GncQueryInfo
import com.lukouguoji.gnc.model.GncQueryInfoWh
import com.lukouguoji.gnc.viewModel.GncQueryInfoViewModel
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_GNC_QUERY_INFO)
class GncQueryInfoActivity : BaseActivity(), View.OnClickListener {

    private lateinit var viewModel: GncQueryInfoViewModel

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

    private lateinit var submit: TextView


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

    @JvmField
    @Autowired
    var id: Int = -9999

    @JvmField
    @Autowired
    var wbNoParam: String? = null

    private fun initView() {
        viewModel = ViewModelProvider(this).get(GncQueryInfoViewModel::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 = GncQueryInfoListAdapter(this, gncQueryInfoAdapterList)
        gncQueryInfoList = findViewById(R.id.gncQueryInfoList)
        gncQueryInfoList.adapter = adapter
        gncQueryInfoList.layoutManager = layoutManager

        val wareHouseLayoutManager = LinearLayoutManager(this)
        wareHouseInfoAdapter = GncQueryInfoWhListAdapter(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_gnc_query_info)
        setBackArrow("国内出港查询详情")
        initView()

//        initData()

        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(GncQueryInfo(1, "运单号", maWbObj.getString("wbNo") ?: ""))
                viewModel.wbNo = maWbObj.getString("wbNo")
                adapter.append(GncQueryInfo(1, "件数", maWbObj.getIntValue("pc").toString()))
                adapter.append(
                    GncQueryInfo(
                        1,
                        "重量",
                        "${maWbObj.getDoubleValue("weight")} KG"
                    )
                )
                adapter.append(GncQueryInfo(1, "承运人", maWbObj.getString("by1") ?: ""))
                adapter.append(GncQueryInfo(1, "目的港", maWbObj.getString("dest") ?: ""))
                adapter.append(GncQueryInfo(1, "品名", maWbObj.getString("goods") ?: ""))
                adapter.append(GncQueryInfo(1, "特码", maWbObj.getString("spCode") ?: ""))
                adapter.append(GncQueryInfo(1, "代理", maWbObj.getString("agentCode") ?: ""))
                adapter.append(GncQueryInfo(1, "运单类型", maWbObj.getString("awbType") ?: ""))
                adapter.append(GncQueryInfo(1, "业务类型", maWbObj.getString("businessType") ?: ""))
                adapter.append(GncQueryInfo(1, "包装类型", maWbObj.getString("packageType") ?: ""))
                /*adapter.append(GncQueryInfo(1, "卸货港", maWbObj.getString("dest1") ?: ""))
                adapter.append(GncQueryInfo(1, "危险品描述", maWbObj.getString("dgrDetail") ?: ""))
                adapter.append(GncQueryInfo(1, "危险品库位", maWbObj.getString("dgrLocation") ?: ""))
                adapter.append(GncQueryInfo(1, "航班", maWbObj.getString("flight") ?: ""))
                adapter.append(GncQueryInfo(1, "入库时间", maWbObj.getString("opDate") ?: ""))
                adapter.append(GncQueryInfo(1, "出库时间", maWbObj.getString("mclose") ?: ""))
                adapter.append(GncQueryInfo(1, "收运人", maWbObj.getString("opId") ?: ""))*/

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

            } else {
                //无数据
            }
        }

    }

    private fun initData() {
        gncQueryInfoAdapterList.add(GncQueryInfo(1, "入库时间", "2021-04-05 15:22:33"))
        gncQueryInfoAdapterList.add(GncQueryInfo(1, "运单号", "A014d384"))
        gncQueryInfoAdapterList.add(GncQueryInfo(1, "代理人", "张三"))
        gncQueryInfoAdapterList.add(GncQueryInfo(1, "特码", "A06"))
        gncQueryInfoAdapterList.add(GncQueryInfo(1, "件数", "39件"))
        gncQueryInfoAdapterList.add(GncQueryInfo(1, "重量", "30kg"))
        gncQueryInfoAdapterList.add(GncQueryInfo(1, "品名", "TF"))
        gncQueryInfoAdapterList.add(GncQueryInfo(1, "目的港", "白云国际机场"))
        gncQueryInfoAdapterList.add(GncQueryInfo(1, "航班号", "U23"))
        gncQueryInfoAdapterList.add(GncQueryInfo(1, "运单类型", "配载单"))
        gncQueryInfoAdapterList.add(GncQueryInfo(1, "承运人", "SF"))
        gncQueryInfoAdapterList.add(GncQueryInfo(1, "包装", "木箱"))

        adapter.notifyDataSetChanged()
    }

    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.exportStatus)
                        .withString("wbNoParam", viewModel.wbNo)
                        .navigation()
                }
                else -> {
                    Toast.makeText(this, "未找到对应操作", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}