package com.nivesh.production.niveshfd.fd.ui.fragment

import android.os.Bundle
import android.text.Editable
import android.text.InputFilter
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.RadioButton
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import com.google.gson.Gson
import com.nivesh.production.niveshfd.R
import com.nivesh.production.niveshfd.databinding.FragmentNiveshfdStepOneBinding
import com.nivesh.production.niveshfd.fd.adapter.HorizontalRecyclerViewAdapter
import com.nivesh.production.niveshfd.fd.db.PreferenceManager
import com.nivesh.production.niveshfd.fd.model.*
import com.nivesh.production.niveshfd.fd.ui.activity.NiveshFdMainActivity
import com.nivesh.production.niveshfd.fd.util.Common
import com.nivesh.production.niveshfd.fd.util.Common.Companion.commonErrorMethod
import com.nivesh.production.niveshfd.fd.util.Common.Companion.removeError
import com.nivesh.production.niveshfd.fd.util.Resource
import com.nivesh.production.niveshfd.fd.viewModel.MyObseravble


class StepOneNiveshFDFragment : Fragment() {
    private var _binding: FragmentNiveshfdStepOneBinding? = null
    private val binding get() = _binding!!

    private val observerViewModel: MyObseravble by activityViewModels()
    private lateinit var rgMaturity: RadioButton
    private lateinit var listOfTenure: MutableList<ROIDataList>
    private lateinit var recyclerViewDropDownAdapter: HorizontalRecyclerViewAdapter
    private lateinit var listOfMinAmount: List<GetCodes>
    private lateinit var listOfMaxAmount: List<GetCodes>
    private lateinit var listOfFrequency: List<GetCodes>

    private var tenure: Int = 0
    private var interest: Double = 0.0
    private var maturityText: String = ""

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

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

        listOfTenure = ArrayList()
        listOfMinAmount = ArrayList()
        listOfMaxAmount = ArrayList()
        listOfFrequency = ArrayList()

        rgMaturity = RadioButton(activity as NiveshFdMainActivity)
        binding.edtAmount.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(8)) // upto 1 Cr
        // Amount
        binding.edtAmount.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) {
                removeError(binding.tlDepositAmount)
                if (s.toString().trim().length >= 4) {
                    maturityAmountApi(tenure, interest)
                }
            }
        })

        // Frequency
        binding.spInterestPayout.setOnItemClickListener { _, _, position, _ ->
            removeError(binding.tlInterestPayout)
            if (listOfFrequency.isNotEmpty()) {
                binding.tvFrequency.text = listOfFrequency[position].Value
                if (!binding.tvFrequency.text.equals(getString(R.string.cumulativeText))) {
                    binding.txtCumulativeNon.text = getString(R.string.nonCumulativeROI)
                } else {
                    binding.txtCumulativeNon.text = getString(R.string.cumulativeROI)
                }
                if (binding.edtAmount.text.toString().trim().isNotEmpty()) {
                    maturityAmountApi(tenure, interest)
                }
            }
        }

        // Tenure
        binding.spTenure.setOnItemClickListener { _, _, position, _ ->
            removeError(binding.tlInterestTenure)
            if (listOfTenure.isNotEmpty()) {
                tenure = listOfTenure[position].Tenure.toInt()
                interest = listOfTenure[position].ROI
                binding.tvROI.text = interest.toString().plus(" %")
                maturityAmountApi(tenure, interest)
                recyclerViewDropDownAdapter.refresh()
            }
        }

        // Senior / Non Senior Citizen
        binding.swSeniorCitizen.setOnCheckedChangeListener { _, _ ->
            getRatesApi()
        }

        // Maturity Options
        maturityText = getString(R.string.totalDeduction)
        rgMaturity.text = getString(R.string.additionalDetailOne)
        binding.radioGroup.setOnCheckedChangeListener { group, checkedId ->
            rgMaturity = group.findViewById(checkedId)
            Log.e("Maturity", "-->" + rgMaturity.text)
            maturityText = if (rgMaturity.text.contains("credit")) {
                getString(R.string.totalDeduction)
            } else {
                getString(R.string.principalDeduction)
            }
        }

        // Next Button
        binding.btnNext.setOnClickListener {
            if (validation()) {

                (activity as NiveshFdMainActivity).fdInvestmentDetails.FDAmount =
                    binding.edtAmount.text.toString().toDouble()
                observerViewModel.data.value = binding.edtAmount.text.toString()
                (activity as NiveshFdMainActivity).fdInvestmentDetails.Frequency =
                    binding.spInterestPayout.text.toString()
                (activity as NiveshFdMainActivity).fdInvestmentDetails.Tenure = tenure
                (activity as NiveshFdMainActivity).fdInvestmentDetails.Interest = interest
                (activity as NiveshFdMainActivity).fdInvestmentDetails.NiveshClientCode =
                    (activity as NiveshFdMainActivity).getClientDetailsResponse.ObjectResponse?.clientDetails?.clientMasterMFD?.CLIENT_CODE
                (activity as NiveshFdMainActivity).fdInvestmentDetails.Provider =
                    getString(R.string.bajaj)
                (activity as NiveshFdMainActivity).fdInvestmentDetails.IPAddress = "192.168.1.23"
                (activity as NiveshFdMainActivity).fdInvestmentDetails.Device =
                    getString(R.string.app)
                (activity as NiveshFdMainActivity).fdInvestmentDetails.Source =
                    getString(R.string.source)
                if (binding.swSeniorCitizen.isChecked) {
                    (activity as NiveshFdMainActivity).fdInvestmentDetails.CitizenType =
                        getString(R.string.seniorCitizen)
                } else {
                    (activity as NiveshFdMainActivity).fdInvestmentDetails.CitizenType =
                        getString(R.string.nonSeniorCitizen)
                }
                (activity as NiveshFdMainActivity).fdInvestmentDetails.CustomerType = ""
                (activity as NiveshFdMainActivity).fdInvestmentDetails.CKYCNumber = ""
                (activity as NiveshFdMainActivity).fdInvestmentDetails.UniqueId =
                    (activity as NiveshFdMainActivity).uniqueId
                (activity as NiveshFdMainActivity).fdInvestmentDetails.RenewOption = maturityText

                (activity as NiveshFdMainActivity).createFDApplicantRequest.FDInvestmentDetails =
                    (activity as NiveshFdMainActivity).fdInvestmentDetails
                Log.e(
                    "StepOneData",
                    "-->" + Gson().toJson((activity as NiveshFdMainActivity).fdInvestmentDetails)
                )
                (activity as NiveshFdMainActivity).stepOneApi()
            }
        }

        minAmountApi()
        interestPayoutApi()
    }

    private fun interestPayoutApi() {
        val getCodeRequest = GetCodeRequest()
        getCodeRequest.ProductName = getString(R.string.bajajFD)
        getCodeRequest.Category = getString(R.string.niveshCategory)
        getCodeRequest.Language = getString(R.string.language)
        getCodeRequest.InputValue = ""
        (activity as NiveshFdMainActivity).viewModel.getCode(
            getCodeRequest,
            PreferenceManager(activity as NiveshFdMainActivity).getToken(),
            activity as NiveshFdMainActivity
        )
        (activity as NiveshFdMainActivity).viewModel.getCodeMutableData.observe(viewLifecycleOwner) { response ->
            when (response) {
                is Resource.Success -> {
                    Log.e("interestPayoutApi", " response -->${response.data.toString()}")
                    val getCodeResponse: GetCodeResponse =
                        Gson().fromJson(response.data?.toString(), GetCodeResponse::class.java)
                    getCodeResponse.Response.StatusCode.let { code ->
                        when (code) {
                            200 -> {
                                listOfFrequency = getCodeResponse.Response.GetCodesList
                                if (listOfFrequency.isNotEmpty()) {
                                    val adapter = ArrayAdapter(
                                        activity as NiveshFdMainActivity,
                                        R.layout.spinner_dropdown,
                                        listOfFrequency
                                    )
                                    binding.spInterestPayout.setAdapter(adapter)
                                    binding.spInterestPayout.setText(
                                        adapter.getItem(listOfFrequency.size - 1)?.Value,
                                        false
                                    )
                                    binding.tvFrequency.text =
                                        adapter.getItem(listOfFrequency.size - 1)?.Value
                                    getRatesApi()
                                } else {
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        "Interest Payout Frequency Data Is Missing."
                                    )
                                }
                            }
                            650 -> ""
                            else -> {
                                if (getCodeResponse.Response.Errors.isNotEmpty()) {
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        getCodeResponse.Response.Errors[0].ErrorMessage
                                    )
                                }else{
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        "".plus(getCodeResponse.Response.Message)
                                    )
                                }
                            }
                        }
                    }
                }

                is Resource.Error -> {
                    response.message?.let { message ->
                        Common.showDialogValidation(activity as NiveshFdMainActivity, message)
                    }
                }
                is Resource.Loading -> {

                }
                is Resource.DataError -> {

                }
            }
        }
    }

    private fun minAmountApi() {
        val getCodeRequest = GetCodeRequest()
        getCodeRequest.ProductName = getString(R.string.bajajFD)
        getCodeRequest.Category = getString(R.string.minAmountCategory)
        getCodeRequest.Language = getString(R.string.language)
        getCodeRequest.InputValue = ""
        (activity as NiveshFdMainActivity).viewModel.getMinAmount(
            getCodeRequest,
            PreferenceManager(activity as NiveshFdMainActivity).getToken(),
            activity as NiveshFdMainActivity
        )
        (activity as NiveshFdMainActivity).viewModel.getMinAmountMutableData.observe(
            viewLifecycleOwner
        ) { response ->
            when (response) {
                is Resource.Success -> {
                    Log.e("minAmountApi ", " response-->${response.data.toString()}")
                    val getCodeResponse: GetCodeResponse =
                        Gson().fromJson(response.data?.toString(), GetCodeResponse::class.java)
                    getCodeResponse.Response.StatusCode.let { code ->
                        when (code) {
                            200 -> {
                                listOfMinAmount = getCodeResponse.Response.GetCodesList
//                                if (listOfMinAmount.isNotEmpty()) {
//                                    binding.txtMinAmount.text = getString(R.string.minAmount).plus(listOfMinAmount[0].Value)
//                                }
                                maxAmountApi()
                            }
                            650 -> ""
                            else -> {
                                if (getCodeResponse.Response.Errors.isNotEmpty()) {
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        getCodeResponse.Response.Errors[0].ErrorMessage
                                    )
                                }else{
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        "".plus(getCodeResponse.Response.Message)
                                    )
                                }
                            }
                        }
                    }
                }

                is Resource.Error -> {
                    response.message?.let { message ->
                        Common.showDialogValidation(activity as NiveshFdMainActivity, message)
                    }
                }
                is Resource.Loading -> {

                }
                is Resource.DataError -> {

                }
            }
        }
    }

    private fun maxAmountApi() {
        val getCodeRequest = GetCodeRequest()
        getCodeRequest.ProductName = getString(R.string.bajajFD)
        getCodeRequest.Category = getString(R.string.MaxAmountCategory)
        getCodeRequest.Language = getString(R.string.language)
        getCodeRequest.InputValue = ""
        (activity as NiveshFdMainActivity).viewModel.getMaxAmount(
            getCodeRequest,
            PreferenceManager(activity as NiveshFdMainActivity).getToken(),
            activity as NiveshFdMainActivity
        )
        (activity as NiveshFdMainActivity).viewModel.getMaxAmountMutableData.observe(
            viewLifecycleOwner
        ) { response ->
            when (response) {
                is Resource.Success -> {
                    Log.e("maxAmountApi ", " response-->${response.data.toString()}")
                    val getCodeResponse: GetCodeResponse =
                        Gson().fromJson(response.data?.toString(), GetCodeResponse::class.java)
                    getCodeResponse.Response.StatusCode.let { code ->
                        when (code) {
                            200 -> {
                                listOfMaxAmount = getCodeResponse.Response.GetCodesList
                            }
                            650 -> ""
                            else -> {
                                if (getCodeResponse.Response.Errors.isNotEmpty()) {
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        getCodeResponse.Response.Errors[0].ErrorMessage
                                    )
                                }else{
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        "".plus(getCodeResponse.Response.Message)
                                    )
                                }
                            }
                        }
                    }
                }

                is Resource.Error -> {
                    response.message?.let { message ->
                        Common.showDialogValidation(activity as NiveshFdMainActivity, message)
                    }
                }
                is Resource.Loading -> {

                }
                is Resource.DataError -> {

                }
            }
        }
    }

    private fun setUpRecyclerView() {

        recyclerViewDropDownAdapter = HorizontalRecyclerViewAdapter(
            activity as NiveshFdMainActivity,
            listOfTenure
        ) { position ->
            tenure = listOfTenure[position].Tenure.toInt()
            interest = listOfTenure[position].ROI
            binding.tvROI.text = interest.toString().plus(" %")
            maturityAmountApi(tenure, interest)
        }
        val mLayoutManager: LayoutManager =
            LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, true)
        binding.rvTenure.layoutManager = mLayoutManager
        binding.rvTenure.setHasFixedSize(true)
        binding.rvTenure.itemAnimator = DefaultItemAnimator()
        binding.rvTenure.adapter = recyclerViewDropDownAdapter
    }

    private fun maturityAmountApi(tenure: Int, interest: Double) {
        if (binding.edtAmount.text.toString().length >= 4 && interest != 0.0 && tenure != 0 && binding.spInterestPayout.text.toString()
                .isNotEmpty()
        ) {
            val maturityAmountRequest = GetMaturityAmountRequest()
            maturityAmountRequest.FDProvider = getString(R.string.bajaj)
            maturityAmountRequest.FDAmount = binding.edtAmount.text.toString().toInt()
            maturityAmountRequest.Frequency = binding.spInterestPayout.text.toString()
            maturityAmountRequest.Tenure = tenure
            maturityAmountRequest.Interest = interest
            (activity as NiveshFdMainActivity).viewModel.getMaturityAmount(
                maturityAmountRequest,
                activity as NiveshFdMainActivity,
                PreferenceManager(activity as NiveshFdMainActivity).getToken()
            )
            (activity as NiveshFdMainActivity).viewModel.getMaturityAmountMutableData.observe(
                viewLifecycleOwner
            ) { response ->
                when (response) {
                    is Resource.Success -> {
                        Log.e("maturityAmountApi ", " response-->${response.data.toString()}")
                        val getMaturityAmountResponse =
                            Gson().fromJson(
                                response.data?.toString(),
                                GetCalculateMaturityAmountResponse::class.java
                            )
                        getMaturityAmountResponse.Response.StatusCode.let { code ->
                            when (code) {
                                200 -> {
                                    binding.tvMaturityAmount.text =
                                        getString(R.string.rs).plus(" ").plus(
                                            getMaturityAmountResponse.Response.MaturityAmount.toString()
                                        )
                                }
                                //   650 -> refreshToken()
                                else -> {
                                    if (getMaturityAmountResponse.Response.Errors.isNotEmpty()) {
                                        Common.showDialogValidation(
                                            activity as NiveshFdMainActivity,
                                            getMaturityAmountResponse.Response.Errors[0].ErrorMessage
                                        )
                                    }else{
                                        Common.showDialogValidation(
                                            activity as NiveshFdMainActivity,
                                            "".plus(getMaturityAmountResponse.Response.Message)
                                        )
                                    }
                                }
                            }
                        }
                    }

                    is Resource.Error -> {
                        response.message?.let { message ->
                            Common.showDialogValidation(
                                activity as NiveshFdMainActivity,
                                message
                            )
                        }
                    }
                    is Resource.Loading -> {

                    }
                    is Resource.DataError -> {

                    }
                }
            }
        }
    }

    private fun validation(): Boolean {
        return if (binding.edtAmount.text.toString().isEmpty()) {
            commonErrorMethod(
                binding.edtAmount,
                binding.tlDepositAmount,
                getString(R.string.emptyAmount)
            )
        } else if (binding.edtAmount.text.toString()
                .toDouble() < listOfMinAmount[0].Value.toDouble()
        ) {
            commonErrorMethod(
                binding.edtAmount,
                binding.tlDepositAmount,
                getString(R.string.validMinAmount)
            )
        } else if (binding.edtAmount.text.toString().toInt() % 1000 != 0) {
            commonErrorMethod(
                binding.edtAmount,
                binding.tlDepositAmount,
                getString(R.string.validMultipleAmount)
            )
        } else if (binding.edtAmount.text.toString()
                .toDouble() > listOfMaxAmount[0].Value.toDouble()
        ) {
            commonErrorMethod(
                binding.edtAmount,
                binding.tlDepositAmount,
                getString(R.string.validMaxAmount)
            )
        } else if (binding.spInterestPayout.text.isEmpty()) {
            Common.commonSpinnerErrorMethod(
                binding.spInterestPayout,
                binding.tlInterestPayout,
                getString(R.string.emptyInterestPayout)
            )
        } else if (binding.spTenure.text.isEmpty()) {
            Common.commonSpinnerErrorMethod(
                binding.spTenure,
                binding.tlInterestTenure,
                getString(R.string.emptyInterestTenure)
            )
        } else {
            true
        }
    }

    private fun getRatesApi() {
        val getRatesRequest = GetRatesRequest()
        getRatesRequest.fdProvider = getString(R.string.bajaj)
        getRatesRequest.frequency = binding.spInterestPayout.text.toString()
        if (binding.swSeniorCitizen.isChecked) {
            getRatesRequest.type = getString(R.string.seniorCitizen)
        } else {
            getRatesRequest.type = getString(R.string.nonSeniorCitizen)
        }
        (activity as NiveshFdMainActivity).viewModel.getRates(
            getRatesRequest,
            PreferenceManager(activity as NiveshFdMainActivity).getToken(),
            activity as NiveshFdMainActivity
        )
        (activity as NiveshFdMainActivity).viewModel.getRatesMutableData.observe(viewLifecycleOwner) { response ->
            when (response) {
                is Resource.Success -> {
                    val getRatesResponse: GetRatesResponse =
                        Gson().fromJson(response.data?.toString(), GetRatesResponse::class.java)
                    getRatesResponse.Response.StatusCode.let { code ->
                        when (code) {
                            200 -> {
                                if (listOfTenure.isNotEmpty()) {
                                    listOfTenure.clear()
                                }
                                listOfTenure = getRatesResponse.Response.ROIDatalist
                                // Tenure
                                if (listOfTenure.isNotEmpty()) {
                                    listOfTenure.sortWith { lhs: ROIDataList, rhs: ROIDataList ->
                                        rhs.Tenure.compareTo(
                                            lhs.Tenure
                                        )
                                    }
                                    binding.ORLayout.visibility = View.VISIBLE
                                    val tenureAdapter =
                                        ArrayAdapter(
                                            activity as NiveshFdMainActivity,
                                            R.layout.spinner_dropdown,
                                            listOfTenure
                                        )
                                    binding.spTenure.setAdapter(tenureAdapter)

                                    binding.spTenure.setText(
                                        tenureAdapter.getItem(0)?.Tenure.plus(
                                            " Months  |  "
                                        ).plus(tenureAdapter.getItem(0)?.ROI).plus(" %"), false
                                    )
                                    tenure = tenureAdapter.getItem(0)?.Tenure.toString().toInt()
                                    interest = tenureAdapter.getItem(0)?.ROI ?: 0.0
                                    binding.tvROI.text =
                                        tenureAdapter.getItem(0)?.ROI.toString().plus(" %")
                                    setUpRecyclerView()
                                } else {
                                    binding.ORLayout.visibility = View.GONE
                                }
                            }
                            //   650 -> refreshToken()
                            else -> {
                                if (getRatesResponse.Response.Errors.isNotEmpty()) {
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        getRatesResponse.Response.Errors[0].ErrorMessage
                                    )
                                }else{
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        "".plus(getRatesResponse.Response.Message)
                                    )
                                }
                            }
                        }
                    }
                }

                is Resource.Error -> {
                    response.message?.let { message ->
                        Common.showDialogValidation(activity as NiveshFdMainActivity, message)
                    }
                }
                is Resource.Loading -> {

                }
                is Resource.DataError -> {

                }
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}