package com.nivesh.production.niveshfd.partnerOnBoarding.ui.fragments1

import android.os.Bundle
import android.text.Editable
import android.text.InputFilter
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.Fragment
import com.nivesh.production.niveshfd.R
import com.nivesh.production.niveshfd.databinding.FragmentOtpBinding
import com.nivesh.production.niveshfd.fd.ui.activity.BaseActivity
import com.nivesh.production.niveshfd.fd.util.Common

class OTPFragment : BaseFragment() {
    private var _binding: FragmentOtpBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        _binding = FragmentOtpBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        init()
    }

    private fun init() {

        binding.edtOTP1.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(1))
        binding.edtOTP2.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(1))
        binding.edtOTP3.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(1))
        binding.edtOTP4.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(1))
        binding.edtOTP5.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(1))
        binding.edtOTP6.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(1))

        binding.edtOTP1.addTextChangedListener(GenericTextWatcher(binding.edtOTP1, binding.edtOTP2))
        binding.edtOTP2.addTextChangedListener(GenericTextWatcher(binding.edtOTP2, binding.edtOTP3))
        binding.edtOTP3.addTextChangedListener(GenericTextWatcher(binding.edtOTP3, binding.edtOTP4))
        binding.edtOTP4.addTextChangedListener(GenericTextWatcher(binding.edtOTP4, binding.edtOTP5))
        binding.edtOTP5.addTextChangedListener(GenericTextWatcher(binding.edtOTP5, binding.edtOTP6))
        binding.edtOTP6.addTextChangedListener(GenericTextWatcher(binding.edtOTP6, null))

        binding.edtOTP1.setOnKeyListener(GenericKeyEvent(binding.edtOTP1, null))
        binding.edtOTP2.setOnKeyListener(GenericKeyEvent(binding.edtOTP2, binding.edtOTP1))
        binding.edtOTP3.setOnKeyListener(GenericKeyEvent(binding.edtOTP3, binding.edtOTP2))
        binding.edtOTP4.setOnKeyListener(GenericKeyEvent(binding.edtOTP4, binding.edtOTP3))
        binding.edtOTP5.setOnKeyListener(GenericKeyEvent(binding.edtOTP5, binding.edtOTP4))
        binding.edtOTP6.setOnKeyListener(GenericKeyEvent(binding.edtOTP6, binding.edtOTP5))

        binding.txtDigit.text = getString(R.string.sixDigitOTP).plus(" ").plus("+91.00000.00000")


        binding.txtResendOTP.setOnClickListener{

        }

        binding.btnSubmit.setOnClickListener {
            if (validate()) {
                replaceFragment(activity as BaseActivity, R.id.signUpContainer,GetStartedFragment(),"GET STARTED", true)
            }
        }
    }

    private fun validate(): Boolean {
        return if (binding.edtOTP1.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtOTP1,
                binding.tlOTP1,
                ""
            )
        } else if (binding.edtOTP2.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtOTP2,
                binding.tlOTP2,
                ""
            )
        } else if (binding.edtOTP3.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtOTP3,
                binding.tlOTP3,
                ""
            )
        } else if (binding.edtOTP4.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtOTP4,
                binding.tlOTP4,
                ""
            )
        } else if (binding.edtOTP5.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtOTP5,
                binding.tlOTP5,
                ""
            )
        } else if (binding.edtOTP6.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtOTP6,
                binding.tlOTP6,
                ""
            )
        } else {
            return true
        }
    }

    class GenericKeyEvent internal constructor(
        private val currentView: EditText,
        private val previousView: EditText?
    ) : View.OnKeyListener {
        override fun onKey(p0: View?, keyCode: Int, event: KeyEvent?): Boolean {
            if (event!!.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DEL && currentView.id != R.id.edtOTP1 && currentView.text.isEmpty()) {
                //If current is empty then previous EditText's number will also be deleted
                previousView!!.text = null
                previousView.requestFocus()
                return true
            }
            return false
        }
    }

    class GenericTextWatcher internal constructor(
        private val currentView: View,
        private val nextView: View?
    ) : TextWatcher {
        override fun afterTextChanged(editable: Editable) { // TODO Auto-generated method stub
            val text = editable.toString()
            when (currentView.id) {
                R.id.edtOTP1 -> if (text.length == 1) nextView!!.requestFocus()
                R.id.edtOTP2 -> if (text.length == 1) nextView!!.requestFocus()
                R.id.edtOTP3 -> if (text.length == 1) nextView!!.requestFocus()
                R.id.edtOTP4 -> if (text.length == 1) nextView!!.requestFocus()
                R.id.edtOTP5 -> if (text.length == 1) nextView!!.requestFocus()
                //You can use EditText4 same as above to hide the keyboard
            }
        }

        override fun beforeTextChanged(
            arg0: CharSequence,
            arg1: Int,
            arg2: Int,
            arg3: Int
        ) {
        }

        override fun onTextChanged(
            arg0: CharSequence,
            arg1: Int,
            arg2: Int,
            arg3: Int
        ) {
        }

    }



}