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.Fragment
import androidx.fragment.app.activityViewModels
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.DataStore
import com.doverfuelingsolutions.issp.databinding.FragmentSearchTypeBinding
import com.doverfuelingsolutions.issp.utils.log.DFSLog
import com.doverfuelingsolutions.issp.view.MainActivity

class FragmentSearchType : Fragment(), View.OnClickListener, View.OnLongClickListener {

    private val globalViewModel: MainActivity.GlobalViewModel by activityViewModels()
    private val viewModel: SearchTypeViewModel by viewModels()

    private lateinit var binding: FragmentSearchTypeBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_search_type, container, false)
        binding.lifecycleOwner = this
        binding.viewModel = viewModel
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        binding.chooseOil.setOnClickListener(this)
        binding.chooseNozzle.setOnClickListener(this)
        binding.versionNum.setOnLongClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v) {
            binding.chooseNozzle -> DFSLog.i("chooseNozzle")
            binding.chooseOil -> DFSLog.i("chooseOil")
        }
    }

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

    class SearchTypeViewModel : ViewModel() {
        val deviceNum = MutableLiveData(DataStore.sn)
        val version: MutableLiveData<String> by lazy {
            val pm = DFSApplication.instance.applicationContext.packageManager
            val info = pm.getPackageInfo(DFSApplication.instance.applicationContext.packageName, android.content.pm.PackageManager.GET_ACTIVITIES)
            MutableLiveData(info.versionName)
        }
    }
}