package com.hx.utils.application.ui.web

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.hx.utils.application.BuildConfig
import com.hx.utils.application.R
import com.hx.utils.application.base.BaseFragment
import com.hx.utils.application.base.BaseWebView
import com.hx.utils.application.databinding.FragmentWebBinding

class WebFragment : BaseFragment<FragmentWebBinding, WebViewModel>() {
    private var webView: BaseWebView? = null
    override fun setupViewModel() {
        viewModel = ViewModelProvider(this)[WebViewModel::class.java]
    }

    override fun setupView() {
        viewModel.urlFormState.observe(viewLifecycleOwner) {
            binding.apply {
                fab.show()
                loading.visibility = View.GONE
            }
            when (it.success) {
                true -> successTip()
                false -> errorTip()
                null -> {}
            }
        }
        webView?.loadUrl(BuildConfig.testUrl) ?: run {
            webView = BaseWebView(requireActivity()).apply {
                loadUrl(BuildConfig.testUrl)
            }
            binding.mainWeb.addView(
                webView, LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT
                )
            )
        }
        binding.fab.setOnClickListener {
            binding.fab.hide()
            binding.loading.visibility = View.VISIBLE
            viewModel.startListening()
        }
        (activity as? AppCompatActivity)?.supportActionBar?.title = arguments?.getString("data")
    }

    override fun setupBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentWebBinding {
        return FragmentWebBinding.inflate(inflater, container, false)
    }


    private fun successTip() {
        val dialog = AlertDialog.Builder(requireActivity())
            .setTitle(getString(R.string.webtestbot_message))
            .setMessage(
                getString(
                    R.string.url_name_first_name,
                    viewModel.urlFormState.value!!.url,
                    viewModel.urlFormState.value!!.username,
                    viewModel.urlFormState.value!!.firstName
                )
            )
            .setPositiveButton(getString(R.string.go)) { dialogInterface, _ ->
                webView?.loadUrl(viewModel.urlFormState.value!!.url)
                dialogInterface.dismiss()
            }
            .setNegativeButton(getString(R.string.cancel)) { dialogInterface, _ ->
                dialogInterface.dismiss()
            }
            .create()
        dialog.show()
    }

    private fun errorTip() {
        val dialog = AlertDialog.Builder(requireActivity())
            .setTitle(getString(R.string.error_info))
            .setMessage(
                viewModel.urlFormState.value!!.error
            )
            .setPositiveButton(getString(R.string.ok)) { dialogInterface, _ ->
                webView?.loadUrl(viewModel.urlFormState.value!!.url)
                dialogInterface.dismiss()
            }
            .create()
        dialog.show()
    }
}