package com.gamedog.vididin.main.fragments



import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.fragment.app.viewModels
import com.ama.core.architecture.appBase.AppViewsFragment
import com.ama.core.architecture.appBase.OnFragmentBackgroundListener
import com.ama.core.architecture.util.AndroidUtil
import com.ama.core.architecture.util.setOnClickBatch
import com.ama.core.architecture.util.setStatusBarDarkFont
import com.ama.core.common.util.dp
import com.gamedog.vididin.core.login.login.AccountManager
import com.gamedog.vididin.router.Router
import dagger.hilt.android.AndroidEntryPoint
import kotlin.getValue
import com.viddin.videos.free.databinding.VididinappFeatureMineFragmentMineBinding as ViewBinding
import com.gamedog.vididin.main.fragments.mine.MineUiState as UiState
import com.gamedog.vididin.main.fragments.mine.MineViewModel as ViewModel




@AndroidEntryPoint
class MineFragment : AppViewsFragment<ViewBinding, UiState, ViewModel>(),
    OnFragmentBackgroundListener {
    override val mViewModel: ViewModel by viewModels()
    override var isBackgroundBright: Boolean = true
    private var isStatusBarDarkFont = false

    override fun inflateViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ) = ViewBinding.inflate(inflater, container, false)

    override fun ViewBinding.initWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(topBackground) { v, insets ->
            val systemBars =
                insets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
            v.updatePadding(top = systemBars.top + 20.dp)
            insets
        }
    }

    override fun ViewBinding.initViews() {
        setOnClickBatch(rlPrivacy, rlVersion, rlFeedback, ivEditName, ivSaveName) {
            when (this) {
                rlPrivacy -> {
                    Router.Privacy.startActivity(requireActivity())
                }
                rlVersion -> {
                    Router.Version.startActivity(requireActivity())
                }
                rlFeedback -> {
                    Router.Feedback.startActivity(requireActivity())
                }

                ivEditName -> {
                    switchNameEditState(true)
                }

                ivSaveName -> {
                    switchNameEditState(false)
                    AndroidUtil.hideKeyboard(ivSaveName)
                }
            }
        }

        nameEditRoot.isVisible = false
        nameNormalRoot.isVisible = true

        var name = AccountManager.getAccount().userName
        if (name.isEmpty()) {
            name = "Miguel"
            AccountManager.updateUserName(name)
        }
        tvAccountName.text = name

        etAccountName.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                tvNameCharactCount.setText("(${s?.length}/15)")
            }

            override fun beforeTextChanged(
                s: CharSequence?,
                start: Int,
                count: Int,
                after: Int
            ) {
            }

            override fun onTextChanged(
                s: CharSequence?,
                start: Int,
                before: Int,
                count: Int
            ) {
            }
        })
    }

    private fun switchNameEditState(isEnterEdit: Boolean) {
        binding?.let {
            with(it) {
                nameEditRoot.isVisible = isEnterEdit
                nameNormalRoot.isVisible = !isEnterEdit
                if (isEnterEdit) {
                    etAccountName.setText(tvAccountName.text.toString().trim())
                } else {
                    val newName = etAccountName.text.toString().trim()
                    if (newName.isNotEmpty()) {
                        tvAccountName.setText(newName)
                        AccountManager.updateUserName(newName)
                    }
                }
            }
        }
    }

    override fun ViewBinding.initListeners() {
        nestedScrollView.setOnScrollChangeListener { _, _, scrollY, _, _ ->
            isStatusBarDarkFont = scrollY > topBackground.height
            setStatusBarDarkFont(isStatusBarDarkFont)
        }
    }

    override fun ViewBinding.initObservers() {
    }

    override fun ViewBinding.onUiStateCollect(uiState: UiState) {
        //dynamicColorsSwitch.isChecked = uiState.useDynamicColor
    }

    override fun onResume() {
        super.onResume()
        setStatusBarDarkFont(isDarkFont = isStatusBarDarkFont)
    }

    companion object {
        internal fun newInstance() = MineFragment()
    }
}