package com.doverfuelingsolutions.issp.view.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.doverfuelingsolutions.issp.R
import com.doverfuelingsolutions.issp.api.entity.PumpInfo
import com.doverfuelingsolutions.issp.databinding.FragmentNozzleBinding
import com.doverfuelingsolutions.issp.fusion.FusionManager
import com.doverfuelingsolutions.issp.view.MainActivity
import com.doverfuelingsolutions.issp.view.adapter.ChooseListAdapter
import com.scwang.smart.refresh.header.ClassicsHeader
import kotlinx.coroutines.launch

class FragmentNozzle private constructor(private val pumpList: List<PumpInfo>) : FragmentBasic() {

    companion object {

        fun build(pumpList: List<PumpInfo>) = FragmentNozzle(pumpList)
    }

    private lateinit var binding: FragmentNozzleBinding
    private val chooseList = pumpList.map {
        it.nozzleList.joinToString(", ") { nozzleInfo ->
            nozzleInfo.physicalId.toString()
        }
    }
    private val chooseListAdapter: ChooseListAdapter by lazy {
        ChooseListAdapter(
            chooseList,
            this::select
        )
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (!this::binding.isInitialized) {
            binding = DataBindingUtil.inflate(inflater, R.layout.fragment_nozzle, container, false)

            binding.nozzleList.layoutManager = GridLayoutManager(context, 4)
            binding.nozzleList.adapter = chooseListAdapter

            binding.smartRefreshLayout.setEnableLoadMore(false)
            binding.smartRefreshLayout.setRefreshHeader(ClassicsHeader(context))
            binding.smartRefreshLayout.setOnRefreshListener { handleNozzleRefresh() }

            setToolBar(binding.fragmentToolbar)
        }
        return binding.root
    }

    private fun handleNozzleRefresh() {
        lifecycleScope.launch {
            val resultNozzles = FusionManager.requestPumpInfo()
            if (resultNozzles.success && resultNozzles.data != null) {
                if (FusionManager.pumpList.isNotEmpty()) FusionManager.pumpList.clear()
                FusionManager.pumpList.addAll(resultNozzles.data)
                binding.nozzleList.adapter?.notifyDataSetChanged()
                binding.smartRefreshLayout.finishRefresh(200, true, false)
            } else {
                binding.smartRefreshLayout.finishRefresh(200, false, true)
            }
        }
    }

    private fun select(nozzleList: String) {
        pumpList.find {
            it.nozzleList.joinToString(", ") { nozzleInfo ->
                nozzleInfo.physicalId.toString()
            } == nozzleList
        }?.let {
            (activity as MainActivity).setFragment(FragmentOrderList.build(it.pumpId), true)
        }
    }
}