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 com.doverfuelingsolutions.issp.R
import com.doverfuelingsolutions.issp.databinding.FragmentReconnectBinding
import com.doverfuelingsolutions.issp.fusion.FusionManager
import com.doverfuelingsolutions.issp.utils.DFSToastUtil
import com.doverfuelingsolutions.issp.view.MainActivity
import com.doverfuelingsolutions.issp.view.PreferenceActivity
import com.wayne.www.waynelib.fdc.FdcClient

class FragmentReconnect private constructor() : FragmentBasic(), View.OnClickListener {

    companion object {

        fun build(): FragmentReconnect {
            return FragmentReconnect()
        }
    }

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

    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

            if (FusionManager.stateFusion == FdcClient.FdcClientState.Stopped || FusionManager.stateFusion == FdcClient.FdcClientState.MyAddReConnect) {
                binding.buttonReconnect.visibility = View.VISIBLE
                binding.buttonFetchInfo.visibility = View.GONE
            } else {
                binding.buttonReconnect.visibility = View.GONE
                binding.buttonFetchInfo.visibility = View.VISIBLE
            }
        }
        return binding.root
    }

    override fun onClick(v: View?) {
        when (v) {
            binding.buttonReconnect -> {
                FusionManager.restart(true)
                (requireActivity() as MainActivity).setFusionLinkingLoading()
            }
            binding.buttonFetchInfo -> {
                lifecycleScope.launchWhenStarted {
                    val dialog = loading(R.string.in_get_fuel)
                    if (checkFusion()) {
                        DFSToastUtil.success(R.string.get_fuel_success)
                        (requireActivity() as MainActivity).initFragmentEntry()
                    } else {
                        DFSToastUtil.fail(R.string.get_fuel_failed)
                    }
                    dialog.dismiss()
                }
            }
            binding.buttonPreference -> {
                PreferenceActivity.startForResult(requireActivity())
            }
        }
    }

    private suspend fun checkFusion(): Boolean {
        if (!FusionManager.isLogin) {
            val dialog = loading(R.string.in_login_fusion)
            val success = FusionManager.loginToFusion()
            dialog.dismiss()
            if (!success) {
                DFSToastUtil.fail(R.string.fusion_login_failed)
                return false
            }
        }

        if (!FusionManager.isFetchPumpInfo) {
            val dialog = loading(R.string.in_get_fuel)
            val result = FusionManager.getPumpInfo()
            dialog.dismiss()
            if (!result.success) {
                DFSToastUtil.fail(R.string.get_fuel_failed)
                return false
            }
        }

        return true
    }
}