package com.nivesh.production.partnerOnBoarding.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.nivesh.production.partnerOnBoarding.R
import com.nivesh.production.partnerOnBoarding.adapters.NewDistributorSignStepOneAdapter
import com.nivesh.production.partnerOnBoarding.api.ApiClient
import com.nivesh.production.partnerOnBoarding.databinding.NewDistributorSignStepOneBinding
import com.nivesh.production.partnerOnBoarding.model.response.GetAllProductResponse
import com.nivesh.production.partnerOnBoarding.providerfactory.OnBoardingModelProviderFactory
import com.nivesh.production.partnerOnBoarding.repositories.MainRepository
import com.nivesh.production.partnerOnBoarding.ui.activities.BaseActivity
import com.nivesh.production.partnerOnBoarding.ui.activities.SignUpActivity
import com.nivesh.production.partnerOnBoarding.util.Constants
import com.nivesh.production.partnerOnBoarding.viewModels.OnBoardingViewModel

class NewDistributorSignStepOne : BaseFragment() {

    private var _binding: NewDistributorSignStepOneBinding? = null
    private val binding get() = _binding!!
    private lateinit var viewModel: OnBoardingViewModel

    companion object {
        var listMapData: MutableMap<Int, String>? = null
    }

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

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

    private fun init() {
        viewModel = ViewModelProvider(
            (activity as SignUpActivity),
            OnBoardingModelProviderFactory(MainRepository(ApiClient.getApiClientOne))
        )[OnBoardingViewModel::class.java]

        (activity as SignUpActivity).viewModelOne.getAllProduct(
//            Constants.BASE_URL_COMMON + "User/GetAllProduct",
            (activity as SignUpActivity)
        )

        (activity as SignUpActivity).viewModelOne.getAllProductMutableData.observe(
            viewLifecycleOwner
        ) { response ->

            val res: GetAllProductResponse =
                Gson().fromJson(
                    response?.data.toString(),
                    GetAllProductResponse::class.java
                )

            if ((res.Status == getString(R.string.successText))) {
                binding.rvList.layoutManager = LinearLayoutManager(activity as SignUpActivity)
                val adapter = NewDistributorSignStepOneAdapter(res.Result.Data)
                binding.rvList.adapter = adapter
            }
        }


        binding.btnNext.setOnClickListener {
            if (listMapData?.isNotEmpty() == true) {
                val bundle = Bundle()
                bundle.putString("mobile", arguments?.getString("mobile"))
                bundle.putString("arnType", "Associate")
                val fragment = GetStartedFragment()
                fragment.arguments = bundle

                replaceFragment(
                    activity as BaseActivity,
                    R.id.signUpContainer,
                    fragment,
                    "GET STARTED",
                    true
                )
            } else {
                Toast.makeText(
                    (activity as SignUpActivity),
                    "Choose the business you are interested",
                    Toast.LENGTH_SHORT
                )
                    .show()
            }
        }
    }
}