package com.lukouguoji.gjc.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.gjc.R
import com.lukouguoji.gjc.adapt.GjcQueryInfoListAdapter
import com.lukouguoji.gjc.adapt.GjcQueryInfoWhListAdapter
import com.lukouguoji.gjc.model.GjcQueryInfo
import com.lukouguoji.gjc.model.GjcQueryInfoWh
import com.lukouguoji.gjc.viewModel.GjcQueryInfoViewModel
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_GJC_QUERY_INFO)
class GjcQueryInfoActivity : BaseActivity(), View.OnClickListener {

    private lateinit var viewModel: GjcQueryInfoViewModel

    private lateinit var scrollView: NestedScrollView
    private lateinit var queryInfoList: RecyclerView
    private lateinit var adapter: GjcQueryInfoListAdapter
    private val queryInfoAdapterList = arrayListOf<GjcQueryInfo>()

    private lateinit var submit: TextView

    private lateinit var refWareHouseInfo: LinearLayout
    private lateinit var wareHouseInfoLayout: LinearLayout
    private lateinit var queryWareHouseInfoList: RecyclerView
    private lateinit var wareHouseInfoAdapter: GjcQueryInfoWhListAdapter
    private val queryWareHouseInfoAdapterList = arrayListOf<GjcQueryInfoWh>()

    @JvmField
    @Autowired
    var id: Int = -9999

    @JvmField
    @Autowired
    var wbNoParam: String? = null

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

        val wareHouseLayoutManager = LinearLayoutManager(this)
        wareHouseInfoAdapter = GjcQueryInfoWhListAdapter(this, queryWareHouseInfoAdapterList)
        queryWareHouseInfoList = findViewById(R.id.gncQueryWareHouseInfoList)
        queryWareHouseInfoList.adapter = wareHouseInfoAdapter
        queryWareHouseInfoList.layoutManager = wareHouseLayoutManager

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

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

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

            } else {
                //无数据
            }
        }

    }

    private fun initData() {
        queryInfoAdapterList.add(GjcQueryInfo(1, "入库时间", "2021-04-05 15:22:33"))
        queryInfoAdapterList.add(GjcQueryInfo(1, "运单号", "A014d384"))
        queryInfoAdapterList.add(GjcQueryInfo(1, "代理人", "张三"))
        queryInfoAdapterList.add(GjcQueryInfo(1, "特码", "A06"))
        queryInfoAdapterList.add(GjcQueryInfo(1, "件数", "39件"))
        queryInfoAdapterList.add(GjcQueryInfo(1, "重量", "30kg"))
        queryInfoAdapterList.add(GjcQueryInfo(1, "品名(英)", "TF"))
        queryInfoAdapterList.add(GjcQueryInfo(1, "目的港", "白云国际机场"))
        queryInfoAdapterList.add(GjcQueryInfo(1, "航班号", "U23"))
        queryInfoAdapterList.add(GjcQueryInfo(1, "运单类型", "配载单"))
        queryInfoAdapterList.add(GjcQueryInfo(1, "承运人", "SF"))
        queryInfoAdapterList.add(GjcQueryInfo(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.gjStatus)
                        .withString("ieFlagParam", Constant.CargoTracking.exportStatus)
                        .withString("wbNoParam", viewModel.wbNo)
                        .navigation()
                }
                else -> {
                    Toast.makeText(this, "未找到对应操作", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}