package com.develop.auth.ui

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import com.blankj.utilcode.util.KeyboardUtils
import com.develop.auth.R
import com.develop.auth.databinding.FragmentMemberLoginBinding
import com.develop.food.base.ui.ViewBindingFragment
import com.develop.food.base.router.Screens
import com.develop.food.base.router.navigateTo
import com.develop.food.base.utils.dimenRes

class MemberLoginFragment : ViewBindingFragment<FragmentMemberLoginBinding>(), KeyboardUtils.OnSoftInputChangedListener {

    private val viewModel by activityViewModels<MemberViewModel>()

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

    override fun onPostCreateView() {
        binding.tvLogin.setOnClickListener {
            if (!checkInputs()) {
                return@setOnClickListener
            }
            executeLogin(
                binding.etEmailId.text.toString(),
                binding.etPassword.text.toString()
            )
        }
        binding.tvRegister.setOnClickListener {
            parentFragmentManager
                .beginTransaction()
                .replace(R.id.fl_container, MemberRegisterFragment())
                .commitAllowingStateLoss()
        }
        binding.tvForget.setOnClickListener {
            parentFragmentManager
                .beginTransaction()
                .replace(R.id.fl_container, MemberForgotFragment())
                .commitAllowingStateLoss()
        }
        KeyboardUtils.registerSoftInputChangedListener(requireActivity(), this)
    }

    override fun onSoftInputChanged(height: Int) {
        val flRoot = (activity as? MemberAuthActivity)?.getRootView() ?: return
        if (height == 0) {
            flRoot.translationY = 0f
            return
        }
        val location = IntArray(2)
        binding.etPassword.getLocationOnScreen(location)
        val viewBottom = requireActivity()
            .resources.displayMetrics
            .heightPixels - location[1]
        val requireDistance = dimenRes(R.dimen.convert_60px)
        if (viewBottom - height >= requireDistance) {
            return
        }
        val deltaHeight = height - viewBottom + requireDistance
        flRoot.translationY = -deltaHeight.toFloat()
        Log.d("SoftInput", "viewBottom:${viewBottom}, height:${height}")
    }

    private fun checkInputs(): Boolean {
        val inputEmailId = binding.etEmailId.text.toString()
        val inputPassword = binding.etPassword.text.toString()
        return inputEmailId.isNotEmpty() && inputPassword.isNotEmpty()
    }

    private fun executeLogin(emailId: String, password: String) {
        showDialog()
        viewModel.login(
            emailId, password
        ).observe(this) {
            dismissDialog()
            if (!it) {
                return@observe
            }
            navigateTo(Screens.Main.ENTRANCE_CHOSEN)
            hostActivity.finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        KeyboardUtils.unregisterSoftInputChangedListener(requireActivity().window)
    }

}