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.fragment.app.viewModels
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.doverfuelingsolutions.issp.DFSApplication
import com.doverfuelingsolutions.issp.R
import com.doverfuelingsolutions.issp.data.GlobalData
import com.doverfuelingsolutions.issp.databinding.FragmentSearchTypeBinding
import com.doverfuelingsolutions.issp.fusion.FusionManager
import com.doverfuelingsolutions.issp.utils.AppUtil
import com.doverfuelingsolutions.issp.view.MainActivity
import com.doverfuelingsolutions.issp.view.PreferenceActivity

class FragmentSelect : FragmentBasic(),
    View.OnLongClickListener,
    View.OnClickListener {

    override val useActionBar: Boolean = false
    override val countdownTime: Int = -1

    private lateinit var binding: FragmentSearchTypeBinding
    private val viewModel: SearchTypeViewModel by viewModels()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        if (!this::binding.isInitialized) {
            binding = DataBindingUtil.inflate(inflater, R.layout.fragment_search_type, container, false)
            binding.lifecycleOwner = this
            binding.viewModel = viewModel
            binding.handler = this
            binding.deviceNum.setOnLongClickListener(this)
        }

        return binding.root
    }

    override fun onClick(v: View?) {
        when (v) {
            binding.chooseNozzle -> {
                (activity as MainActivity).fragmentRouter.push(FragmentNozzle.build(FusionManager.pumpList))
            }
            binding.chooseOil -> {
                (activity as MainActivity).fragmentRouter.push(FragmentOrderList.build(-1))
            }
        }
    }

    override fun onLongClick(v: View?): Boolean {
        return when (v) {
            binding.deviceNum -> {
                PreferenceActivity.startForResult(requireActivity())
                true
            }
            else -> false
        }
    }

    class SearchTypeViewModel : ViewModel() {
        val deviceNum = MutableLiveData(GlobalData.serialNumber.get())
        val version = MutableLiveData(AppUtil.getVersionName())
    }
}