package com.develop.auth.ui

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.KeyboardUtils
import com.develop.auth.R
import com.develop.auth.databinding.FragmentMemberLoginBinding
import com.develop.auth.viewmodel.FragmentTag
import com.develop.auth.viewmodel.MemberViewModel
import com.develop.base.ext.navigateTo
import com.develop.base.ext.resId2Dimension
import com.develop.base.mvvm.BaseBVMFragment
import com.develop.common.router.Screens
import com.develop.common.ui.CommonBVMFragment

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

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

    override fun onPostCreateView() {
        super.onPostCreateView()
        binding.tvLogin.setOnClickListener {
            if (!checkInputs()) {
                return@setOnClickListener
            }
            executeLogin(
                binding.etEmailId.text.toString(), binding.etPassword.text.toString()
            )
        }
        binding.tvRegister.setOnClickListener {
            viewModel.fragmentChangeLiveData.postValue(FragmentTag.MEMBER_REGISTER)
        }
        binding.tvForget.setOnClickListener {
            viewModel.fragmentChangeLiveData.postValue(FragmentTag.MEMBER_FORGER)
        }
        KeyboardUtils.registerSoftInputChangedListener(requireActivity(), this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.loginLiveData.observe(this) {
            dismissPlainDialog()
            it.msg?.apply {
                if("账号或密码错误".equals(this)){
                    showToast(getString(com.develop.common.R.string.account_or_pwd_error))
                }

            }
            if (it.success) {
                navigateTo(Screens.Main.ENTRANCE_CHOSEN)
                hostActivity.finish()
            }
        }
    }

    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 = dp60
        if (viewBottom - height >= requireDistance) {
            return
        }
        val deltaHeight = height - viewBottom + requireDistance
        flRoot.translationY = -deltaHeight
        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) {
        showPlainDialog()
        viewModel.login(
            emailId, password
        )
    }

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

    override fun createViewModel(): MemberViewModel {
        return getViewModelOfActivity(MemberViewModel::class.java)
    }

    companion object {
        var dp60 = (com.develop.base.R.dimen.convert_60px).resId2Dimension()
    }

}