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 com.doverfuelingsolutions.issp.R
import com.doverfuelingsolutions.issp.databinding.FragmentReconnectBinding
import com.doverfuelingsolutions.issp.fusion.FusionManager
import com.doverfuelingsolutions.issp.view.PreferenceActivity

class FragmentReconnect private constructor(private val reconnectHandler: () -> Unit) : Fragment(), View.OnClickListener {

    companion object {

        fun build(reconnectHandler: () -> Unit): FragmentReconnect {
            return FragmentReconnect(reconnectHandler)
        }
    }

    private lateinit var binding: FragmentReconnectBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (!this::binding.isInitialized) {
            binding =
                DataBindingUtil.inflate(inflater, R.layout.fragment_reconnect, container, false)
            binding.lifecycleOwner = viewLifecycleOwner
            binding.handler = this
        }
        return binding.root
    }

    override fun onClick(v: View?) {
        when (v) {
            binding.buttonReconnect -> {
                FusionManager.restart(true)
                reconnectHandler.invoke()
            }
            binding.buttonPreference -> {
                PreferenceActivity.startForResult(requireActivity())
            }
        }
    }
}