package com.lukouguoji.gjj.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.gjj.R
import com.lukouguoji.gjj.adapt.GjjQueryInfoListAdapter
import com.lukouguoji.gjj.adapt.GjjQueryInfoWhListAdapter
import com.lukouguoji.gjj.model.GjjQueryInfo
import com.lukouguoji.gjj.model.GjjQueryInfoWh
import com.lukouguoji.gjj.viewModel.GjjQueryInfoViewModel
import com.lukouguoji.module_base.BaseActivity
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.router.ARouterConstants

@Deprecated("旧版国际进港查询详情，使用 IntImpQueryDetailsActivity 替代")
// @Route(path = ARouterConstants.ACTIVITY_URL_GJJ_QUERY_INFO) // 已替换为 IntImpQueryDetailsActivity
class GjjQueryInfoActivity : BaseActivity(), View.OnClickListener {

    private lateinit var viewModel: GjjQueryInfoViewModel

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

    private lateinit var submit: TextView

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

    @JvmField
    @Autowired
    var id: Int = -9999

    @JvmField
    @Autowired
    var wbNoParam: String? = null

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

        val wareHouseLayoutManager = LinearLayoutManager(this)
        wareHouseInfoAdapter = GjjQueryInfoWhListAdapter(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_gjj_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")
                val wbNo =
                    (maWbObj.getString("prefix") ?: "") + (maWbObj.getString("no") ?: "")
                adapter.append(GjjQueryInfo(1, "运单号", wbNo))
                viewModel.wbNo = wbNo
                adapter.append(GjjQueryInfo(1, "总件数", maWbObj.getIntValue("pc").toString()))
                adapter.append(
                    GjjQueryInfo(
                        1,
                        "总重量",
                        "${maWbObj.getDoubleValue("grossweight")} KG"
                    )
                )
                adapter.append(GjjQueryInfo(1, "承运人", maWbObj.getString("carrier") ?: ""))
                adapter.append(GjjQueryInfo(1, "目的港", maWbObj.getString("dest") ?: ""))
                adapter.append(GjjQueryInfo(1, "品名(英)", maWbObj.getString("goods") ?: ""))
                adapter.append(GjjQueryInfo(1, "特码", maWbObj.getString("spcode") ?: ""))
                adapter.append(GjjQueryInfo(1, "代理", maWbObj.getString("agent") ?: ""))
                adapter.append(GjjQueryInfo(1, "运单类型", maWbObj.getString("awbtype") ?: ""))
                adapter.append(GjjQueryInfo(1, "业务类型", maWbObj.getString("businesstype") ?: ""))
                adapter.append(GjjQueryInfo(1, "包装类型", maWbObj.getString("packagecode") ?: ""))
                /*adapter.append(GjjQueryInfo(1, "卸货港", maWbObj.getString("dest1") ?: ""))
                adapter.append(GjjQueryInfo(1, "危险品描述", maWbObj.getString("dgrDetail") ?: ""))
                adapter.append(GjjQueryInfo(1, "危险品库位", maWbObj.getString("dgrLocation") ?: ""))
                adapter.append(GjjQueryInfo(1, "航班", maWbObj.getString("flight") ?: ""))
                adapter.append(GjjQueryInfo(1, "入库时间", maWbObj.getString("opDate") ?: ""))
                adapter.append(GjjQueryInfo(1, "出库时间", maWbObj.getString("mclose") ?: ""))
                adapter.append(GjjQueryInfo(1, "收运人", maWbObj.getString("opId") ?: ""))*/

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

            } else {
                //无数据
            }
        }

    }

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