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.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.nivesh.production.niveshfd.databinding.FragmentSignupBinding
import com.nivesh.production.niveshfd.fd.ui.activity.BaseActivity
import com.nivesh.production.niveshfd.fd.util.Common


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


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

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

    private fun init() {
        binding.edtMobileNumber.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(10))
        binding.edtMobileNumber.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {}
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                Common.removeError(binding.tlMobileNumber)
            }
        })

        binding.btnSignUp.setOnClickListener {
            if (validate()) {
                replaceFragment(
                    activity as BaseActivity,
                    com.nivesh.production.niveshfd.R.id.signUpContainer,
                    OTPFragment(),
                    "OTP",
                    true
                )
            }
        }
    }


    private fun validate(): Boolean {
        return if (binding.edtMobileNumber.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtMobileNumber,
                binding.tlMobileNumber,
                getString(com.nivesh.production.niveshfd.R.string.emptyMobileNumber)
            )
        } else if (binding.edtMobileNumber.text?.length != 10) { // EditText
            Common.commonErrorMethod(
                binding.edtMobileNumber,
                binding.tlMobileNumber,
                getString(com.nivesh.production.niveshfd.R.string.inValidMobileNumber)
            )
        } else if (!Common.isIndianMobileNo(binding.edtMobileNumber.text.toString())) { // EditText
            Common.commonErrorMethod(
                binding.edtMobileNumber,
                binding.tlMobileNumber,
                getString(com.nivesh.production.niveshfd.R.string.inValidIndianMobileNumber)
            )
        } else {
            return true
        }
    }


}