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.viewModels
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.doverfuelingsolutions.issp.R
import com.doverfuelingsolutions.issp.databinding.FragmentHolderBinding
import com.doverfuelingsolutions.issp.utils.StringUtil

class FragmentHolder : Fragment() {

    companion object {
        const val UI_LOADING = 1
        const val UI_ACTION = 2
    }

    private lateinit var binding: FragmentHolderBinding
    private val holderViewModel: FragmentHolderViewModel by viewModels()
    private var buttonHandler: (() -> Unit)? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_holder, container, false)
        binding.lifecycleOwner = viewLifecycleOwner
        binding.viewModel = holderViewModel
        binding.actionButton.setOnClickListener { buttonHandler?.invoke() }
        return binding.root
    }

    fun action(msg: String, buttonText: String, func: (() -> Unit)? = null) {
        holderViewModel.uiType.value = UI_ACTION
        holderViewModel.actionButtonText.value = buttonText
        holderViewModel.actionText.value = msg
        buttonHandler = func
    }

    fun loading(msg: String) {
        holderViewModel.uiType.value = UI_LOADING
        holderViewModel.loadingTip.value = msg
    }

    class FragmentHolderViewModel : ViewModel() {
        val uiType = MutableLiveData(UI_LOADING)
        val loadingTip = MutableLiveData(StringUtil.get(R.string.in_connect_fusion))
        val actionText = MutableLiveData(StringUtil.get(R.string.connect_timeout))
        val actionButtonText = MutableLiveData(StringUtil.get(R.string.go_check_fusion))
    }
}