package com.lukouguoji.gjc.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.FragmentGjcQueryStorageBinding
import com.lukouguoji.gjc.holder.GjcQueryStorageViewHolder
import com.lukouguoji.gjc.viewModel.GjcQueryDetailsViewModel
import com.lukouguoji.module_base.base.CommonAdapter
import com.lukouguoji.module_base.bean.GjcStorageUse
import com.lukouguoji.module_base.http.net.NetApply

/**
 * 国际出港查询详情 - 库位信息Fragment
 */
class GjcQueryStorageFragment : Fragment() {

    private lateinit var binding: FragmentGjcQueryStorageBinding
    private lateinit var viewModel: GjcQueryDetailsViewModel
    private lateinit var adapter: CommonAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_gjc_query_storage,
            container,
            false
        )
        binding.lifecycleOwner = viewLifecycleOwner

        initRecyclerView()
        observeData()

        return binding.root
    }

    /**
     * 初始化RecyclerView
     */
    private fun initRecyclerView() {
        adapter = CommonAdapter(
            requireContext(),
            R.layout.item_gjc_query_storage,
            GjcQueryStorageViewHolder::class.java
        )

        binding.rvStorageList.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = this@GjcQueryStorageFragment.adapter
        }
    }

    /**
     * 观察数据变化
     */
    private fun observeData() {
        viewModel.storageUseList.observe(viewLifecycleOwner) { mapList ->
            if (mapList.isEmpty()) {
                // 显示空数据提示
                binding.rvStorageList.visibility = View.GONE
                binding.llEmpty.visibility = View.VISIBLE
            } else {
                // Map → GjcStorageUse Bean 转换
                val beanList = mapList.map { map ->
                    try {
                        val json = NetApply.gson.toJson(map)
                        NetApply.gson.fromJson(json, GjcStorageUse::class.java)
                    } catch (e: Exception) {
                        e.printStackTrace()
                        GjcStorageUse()  // 转换失败返回空对象
                    }
                }

                // 更新RecyclerView
                binding.rvStorageList.visibility = View.VISIBLE
                binding.llEmpty.visibility = View.GONE
                adapter.refresh(beanList)
            }
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(vm: GjcQueryDetailsViewModel) =
            GjcQueryStorageFragment().apply {
                viewModel = vm
            }
    }
}
