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

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

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

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

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

    private fun init() {
        binding.edtDay.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.tlDay)
            }
        })
        binding.edtMonth.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.tlMonth)
            }
        })
        binding.edtYear.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.tlYear)
            }
        })

        binding.edtFullName.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.tlFullName)
            }
        })

        binding.edtEmail.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.tlEmail)
            }
        })

        binding.edtAddressLine1.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.tlAddressLine1)
            }
        })

        binding.edtPinCode.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.tlPinCode)
            }
        })

        binding.spCity.setOnClickListener {

        }

        binding.spState.setOnClickListener {

        }
        binding.cbCheck.setOnClickListener {

        }

        binding.tvTermsAndCondition.setOnClickListener{

        }
        binding.tvPrivacyPolicy.setOnClickListener{

        }
        binding.tvAgreement.setOnClickListener{

        }

        binding.btnBack.setOnClickListener {
            requireActivity().onBackPressedDispatcher.onBackPressed()
        }

        binding.btnNext.setOnClickListener {
            if (validate()) {
                // submit
            }
        }


    }

    private fun validate(): Boolean {
        return if (binding.edtDay.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtDay,
                binding.tlDay,
                ""
            )
        } else if (binding.edtMonth.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtMonth,
                binding.tlMonth,
                ""
            )
        } else if (binding.edtYear.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtYear,
                binding.tlYear,
                ""
            )
        } else if (binding.edtFullName.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtFullName,
                binding.tlFullName,
                ""
            )
        } else if (binding.edtEmail.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtFullName,
                binding.tlFullName,
                ""
            )
        } else if (!Common.isValidEmail(binding.edtEmail.text.toString())) { // EditText
            Common.commonErrorMethod(
                binding.edtFullName,
                binding.tlFullName,
                ""
            )
        } else if (binding.edtAddressLine1.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtAddressLine1,
                binding.tlAddressLine1,
                ""
            )
        } else if (binding.edtPinCode.text.toString().isEmpty()) { // EditText
            Common.commonErrorMethod(
                binding.edtPinCode,
                binding.tlPinCode,
                ""
            )
        } else if (binding.edtPinCode.text.toString().length != 6) { // EditText
            Common.commonErrorMethod(
                binding.edtPinCode,
                binding.tlPinCode,
                ""
            )
        } else if (binding.spCity.text.toString().isEmpty()) { // EditText
            Common.commonSpinnerErrorMethod(
                binding.spCity,
                binding.tlCity,
                ""
            )
        } else if (binding.spState.text.toString().isEmpty()) { // EditText
            Common.commonSpinnerErrorMethod(
                binding.spState,
                binding.tlState,
                ""
            )
        }else if (!binding.cbCheck.isChecked) { // EditText
           return false
        } else {
            return true
        }
    }

}