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

import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.google.gson.Gson
import com.nivesh.production.niveshfd.R
import com.nivesh.production.niveshfd.databinding.FragmentNiveshfdStepFiveBinding
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.Resource

class StepFiveNiveshFDFragment : Fragment() {

    private var _binding: FragmentNiveshfdStepFiveBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        _binding = FragmentNiveshfdStepFiveBinding.inflate(inflater, container, false)
        return binding.root
    }

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

        binding.btnViewOrder.setOnClickListener {
            (activity as NiveshFdMainActivity).setResult(Activity.RESULT_OK)
            (activity as NiveshFdMainActivity).finish()
        }

        binding.tvRetry.setOnClickListener {
            (activity as NiveshFdMainActivity).retryMethod()
        }
    }

    fun getData(paymentReQueryResponse: PaymentReQueryResponse) {
        if (paymentReQueryResponse.Response.StatusCode == 200) {
            if (paymentReQueryResponse.Response.Message.isNotEmpty()) {
                val arrOfStr: List<String> =
                    paymentReQueryResponse.Response.Message.split(" ", limit = 2)
                binding.tvCongrats.text = arrOfStr[0]
                binding.tvCongrats.setTextColor(
                    ContextCompat.getColor(
                        activity as NiveshFdMainActivity,
                        R.color.green
                    )
                )
                binding.tvSuccessMessage.text = arrOfStr[1]
            }
            finalizeFDApi()

        } else {
            if (paymentReQueryResponse.Response.Message.isNotEmpty()) {
                if (paymentReQueryResponse.Response.Message.isNotEmpty()) {
                    binding.tvCongrats.text = paymentReQueryResponse.Response.Status
                    binding.tvCongrats.setTextColor(
                        ContextCompat.getColor(
                            activity as NiveshFdMainActivity,
                            R.color.red
                        )
                    )
                    binding.tvSuccessMessage.text = paymentReQueryResponse.Response.Message
                }
            }
            binding.tvRetry.visibility = View.VISIBLE
            binding.btnViewOrder.visibility = View.GONE
        }

    }

    private fun finalizeFDApi() {
        val finalizeFDRequest = FinalizeFDRequest()
        val finaliseFD = FinaliseFD()
        finaliseFD.FDProvider = getString(R.string.bajaj)
        finaliseFD.NiveshClientCode =
            (activity as NiveshFdMainActivity).getClientDetailsResponse.ObjectResponse?.clientDetails?.clientMasterMFD?.CLIENT_CODE
        finaliseFD.UniqueId = (activity as NiveshFdMainActivity).uniqueId
        finalizeFDRequest.FinaliseFD = finaliseFD

        (activity as NiveshFdMainActivity).viewModel.finaliseFD(
            finalizeFDRequest,
            PreferenceManager(activity as NiveshFdMainActivity).getToken(),
            activity as NiveshFdMainActivity
        )
        (activity as NiveshFdMainActivity).viewModel.getFinalizeFDMutableData.observe(
            viewLifecycleOwner
        ) { response ->
            when (response) {
                is Resource.Success -> {
                    Log.e("finalizeKYC ", " response-->${response.data.toString()}")
                    val finalizeFDResponse: FinalizeFDResponse =
                        Gson().fromJson(response.data?.toString(), FinalizeFDResponse::class.java)
                    finalizeFDResponse.Response.StatusCode.let { code ->
                        when (code) {
                            200 -> {
                                if ( finalizeFDResponse.Response.KYCFlag == 0){
                                    finalizeKYCApi()
                                } else {

                                }
                            }
                            650 -> ""
                            else -> {
                                if (finalizeFDResponse.Response.Errors.isNotEmpty()) {
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        finalizeFDResponse.Response.Errors[0].ErrorMessage
                                    )
                                }else{
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        "".plus(finalizeFDResponse.Response.Message)
                                    )
                                }
                            }
                        }
                    }
                }

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

                }
                is Resource.DataError -> {

                }
            }
        }
    }

    private fun finalizeKYCApi() {
        val finalizeKYCRequest = FinalizeKYCRequest()
        finalizeKYCRequest.FDProvider = getString(R.string.bajaj)
        finalizeKYCRequest.NiveshClientCode =
            (activity as NiveshFdMainActivity).getClientDetailsResponse.ObjectResponse?.clientDetails?.clientMasterMFD?.CLIENT_CODE
        finalizeKYCRequest.UniqueId = (activity as NiveshFdMainActivity).uniqueId
        (activity as NiveshFdMainActivity).viewModel.finaliseKYC(
            finalizeKYCRequest,
            PreferenceManager(activity as NiveshFdMainActivity).getToken(),
            activity as NiveshFdMainActivity
        )
        (activity as NiveshFdMainActivity).viewModel.getFinalizeKYCMutableData.observe(
            viewLifecycleOwner
        ) { response ->
            when (response) {
                is Resource.Success -> {
                    Log.e("finalizeKYC ", " response-->${response.data.toString()}")
                    val finalizeFDResponse: FinalizeFDResponse =
                        Gson().fromJson(response.data?.toString(), FinalizeFDResponse::class.java)
                    finalizeFDResponse.Response.StatusCode.let { code ->
                        when (code) {
                            200 -> {

                            }
                            650 -> ""
                            else -> {
                                if (finalizeFDResponse.Response.Errors.isNotEmpty()) {
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        finalizeFDResponse.Response.Errors[0].ErrorMessage
                                    )
                                }else{
                                    Common.showDialogValidation(
                                        activity as NiveshFdMainActivity,
                                        "".plus(finalizeFDResponse.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
    }
}