package com.nivesh.production.partnerOnBoarding.viewModels

import android.app.Activity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.JsonObject
import com.nivesh.production.partnerOnBoarding.model.request.PartnerRequest
import com.nivesh.production.partnerOnBoarding.model.request.SendOTPRequest
import com.nivesh.production.partnerOnBoarding.model.request.ValidatePartnerRequest
import com.nivesh.production.partnerOnBoarding.repositories.MainRepository
import com.nivesh.production.partnerOnBoarding.util.Common
import com.nivesh.production.partnerOnBoarding.util.Common.Companion.handleError
import com.nivesh.production.partnerOnBoarding.util.Common.Companion.handleResponse
import com.nivesh.production.partnerOnBoarding.util.Resource
import kotlinx.coroutines.launch
import okhttp3.MultipartBody
import okhttp3.RequestBody

open class OnBoardingViewModel(private val mainRepository: MainRepository) : ViewModel() {

    val getPartnerCreationMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getPartnerCreateData(
        requestBody: PartnerRequest,
        token: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getPartnerCreationResponse(requestBody, token)
            getPartnerCreationMutableData.postValue(handleResponse(response))
        }
    }

    val getPartnerCreationSaveMutableData: MutableLiveData<Resource<JsonObject>?> =
        MutableLiveData()

    fun getPartnerCreateSaveData(
        requestBody: PartnerRequest,
        token: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getPartnerCreationResponse(requestBody, token)
            getPartnerCreationSaveMutableData.postValue(handleResponse(response))
        }
    }

    val getPartnerCreationContinueMutableData: MutableLiveData<Resource<JsonObject>?> =
        MutableLiveData()

    fun getPartnerCreateContinueData(
        requestBody: PartnerRequest,
        token: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getPartnerCreationResponse(requestBody, token)
            getPartnerCreationContinueMutableData.postValue(handleResponse(response))
        }
    }

    val getPartnerDetailMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getPartnerDetailData(
        partnerCode: String,
        token: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getPartnerDetailResponse(partnerCode, token)
            getPartnerDetailMutableData.postValue(handleResponse(response))
        }
    }

    val getValidatePartnerMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getPartnerValidate(
        validatePartnerRequest: ValidatePartnerRequest,
        token: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getValidatePartnerResponse(validatePartnerRequest)
            getValidatePartnerMutableData.postValue(handleResponse(response))
        }
    }

    val getSendOTPMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getSendOTP(
        sendOTPRequest: SendOTPRequest,
        token: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getSendOTPResponse(sendOTPRequest)
            getSendOTPMutableData.postValue(handleResponse(response))
        }
    }

    val getSendOTPSaveEAgreementMutableData: MutableLiveData<Resource<JsonObject>?> =
        MutableLiveData()

    fun getSaveEAgreementSendOTP(
        sendOTPRequest: SendOTPRequest,
        token: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getSendSaveEAgreementOTPResponse(sendOTPRequest)
            getSendOTPSaveEAgreementMutableData.postValue(handleResponse(response))
        }
    }

    val getVerifyOTPMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getVerifyOTP(
        requestBody: RequestBody,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getVerifyOTP(requestBody)
            getVerifyOTPMutableData.postValue(handleResponse(response))
        }
    }

    val getAMFIDetailMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getAMFIDetail(
        requestBody: RequestBody,
        token: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getAMFIDetailResponse(requestBody)
            getAMFIDetailMutableData.postValue(handleResponse(response))
        }
    }

    val getPANDetailMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getPANDetail(
        requestBody: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getPANDetailResponse(requestBody)
            getPANDetailMutableData.postValue(handleResponse(response))
        }
    }

    val getDocumentTypeMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getDocumentType(
        value: String,
        type: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getDocumentTypeResponse(value, type)
            getDocumentTypeMutableData.postValue(handleResponse(response))
        }
    }

    val getDocumentTypeFullMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getDocumentTypeFull(
        value: String,
        type: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getDocumentTypeResponse(value, type)
            getDocumentTypeFullMutableData.postValue(handleResponse(response))
        }
    }

    val getDocumentUploadMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getDocumentUpload(
//        multipartBody: List<MultipartBody.Part>,
        multipartBody: MultipartBody.Part,
        pc: Int,
        pr: Int,
        di: Int,
        fileId: Int,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getDocumentUploadResponse(multipartBody, pc, pr, di,fileId)
            getDocumentUploadMutableData.postValue(handleResponse(response))
        }
    }

    val getSaveEAgreementMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getSaveEAgreement(
        requestBody: RequestBody,
        token: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getSaveEAgreement(requestBody, token)
            getSaveEAgreementMutableData.postValue(handleResponse(response))
        }
    }

    val getAllProductMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getAllProduct(
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getAllProduct()
            getAllProductMutableData.postValue(handleResponse(response))
        }
    }

    val getAreaDetailByPinMutableData: MutableLiveData<Resource<JsonObject>?> = MutableLiveData()
    fun getAreaDetailByPin(
        pin: String,
        activity: Activity
    ) = viewModelScope.launch(handleError(activity)) {
        if (Common.isNetworkAvailable(activity)) {
            val response = mainRepository.getAreaDetailByPinCode(pin)
            getAreaDetailByPinMutableData.postValue(handleResponse(response))
        }
    }
}