package com.app.simplitend.welcome.welcomecg.mvvm;

import android.util.Log;

import androidx.annotation.NonNull;

import com.app.simplitend.apputils.RetrofitHelper;
import com.app.simplitend.welcome.welcomecg.WelcomeApiService;
import com.app.simplitend.welcome.welcomecg.WelcomeContracts;
import com.app.simplitend.welcome.welcomepatient.mvvm.models.CallResponse;

import java.util.Map;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CgWelcomeRepository {

    private static final String TAG = "CgWelcomeRepository";

    private static CgWelcomeRepository repository;

    private final WelcomeApiService apiService;

    private CgWelcomeRepository(){
        apiService = RetrofitHelper.getRetrofit().create(WelcomeApiService.class);
    }

    public static CgWelcomeRepository getRepository(){
        if (repository == null){
            repository = new CgWelcomeRepository();
        }

        return repository;
    }

    public void registerCareGiver(Map<String, RequestBody> body,
                                  @NonNull WelcomeContracts.RegisterCareGiverCallback registerCareGiverCallback){

        apiService.registerCareGiver(body)
                .enqueue(new Callback<CallResponse<CareGiverData>>() {
                    @Override
                    public void onResponse(Call<CallResponse<CareGiverData>> call, Response<CallResponse<CareGiverData>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200 || response.body().result == null){
                                registerCareGiverCallback.onRegisterFailed(new Exception(), response.body().message);
                                return;
                            }

                            registerCareGiverCallback.onCareGiverRegistered(response.body().result, response.body().token);
                        }else{
                            registerCareGiverCallback.onRegisterFailed(new Exception(), "Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<CareGiverData>> call, Throwable t) {
                        Log.e(TAG, "onFailure: ", t);
                        registerCareGiverCallback.onRegisterFailed(t, "Please try again later.");
                    }
                });

    }

    public void verifyCgOTP(Map<String, String> body,
                            WelcomeContracts.VerifyCgOTPCallback verifyCgOTPCallback){

        apiService.verifyCgOTP(body)
                .enqueue(new Callback<CallResponse<Object>>() {
                    @Override
                    public void onResponse(Call<CallResponse<Object>> call, Response<CallResponse<Object>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200){
                                verifyCgOTPCallback.onVerifyOTPFailed(new Exception(), response.body().message);
                                return;
                            }

                            verifyCgOTPCallback.onOTPVerifies();
                        }else{
                            verifyCgOTPCallback.onVerifyOTPFailed(new Exception(), "Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<Object>> call, Throwable t) {
                        Log.e(TAG, "onFailure: ", t);
                        verifyCgOTPCallback.onVerifyOTPFailed(t, "Please try again later.");
                    }
                });

    }

    public void loginCaregiver(Map<String, String> body,
                               WelcomeContracts.CgLoginCallback loginCallback){

        apiService.loginCaregiver(body)
                .enqueue(new Callback<CallResponse<CareGiverData>>() {
                    @Override
                    public void onResponse(Call<CallResponse<CareGiverData>> call, Response<CallResponse<CareGiverData>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200 || response.body().result == null){
                                loginCallback.onLogInFailed(new Exception(), response.body().message);
                                return;
                            }

                            loginCallback.onLoginSuccess(response.body().result, response.body().token);
                        }else{
                            loginCallback.onLogInFailed(new Exception(), "Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<CareGiverData>> call, Throwable t) {
                        loginCallback.onLogInFailed(new Exception(), "Please try again later.");
                    }
                });

    }

    public void sendForgotCodeEmail(String email,
                                    WelcomeContracts.ForgotPwdSentCodeCallback sentCodeCallback){
        apiService.sendForgotCodeEmail(email)
                .enqueue(new Callback<CallResponse<CgForgotPwdResult>>() {
                    @Override
                    public void onResponse(Call<CallResponse<CgForgotPwdResult>> call, Response<CallResponse<CgForgotPwdResult>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200 || response.body().result == null){
                                sentCodeCallback.onEmailSentFailed(new Exception(), response.body().message);
                                return;
                            }

                            sentCodeCallback.onEmailSentSuccess(response.body().result);
                        }else{
                            sentCodeCallback.onEmailSentFailed(new Exception(), "Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<CgForgotPwdResult>> call, Throwable t) {
                        sentCodeCallback.onEmailSentFailed(new Exception(), "Please try again later.");
                    }
                });
    }

    public void verifyForgotOTP(Map<String, String> body,
                                WelcomeContracts.VerifyForgotOTP forgotOTPCallback){

        apiService.verifyForgotOTP(body)
                .enqueue(new Callback<CallResponse<String>>() {
                    @Override
                    public void onResponse(Call<CallResponse<String>> call, Response<CallResponse<String>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200){
                                forgotOTPCallback.onOTPVerificationFailed(new Exception(), response.body().message);
                                return;
                            }

                            forgotOTPCallback.onOTPVerified();
                        }else{
                            forgotOTPCallback.onOTPVerificationFailed(new Exception(), "Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<String>> call, Throwable t) {
                        forgotOTPCallback.onOTPVerificationFailed(new Exception(), "Please try again later.");
                    }
                });

    }

    public void changeCgPassword(Map<String, RequestBody> body,
                                 WelcomeContracts.ChangePasswordCallback changePasswordCallback){

        apiService.changeCgPassword(body)
                .enqueue(new Callback<CallResponse<CareGiverData>>() {
                    @Override
                    public void onResponse(Call<CallResponse<CareGiverData>> call, Response<CallResponse<CareGiverData>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200 || response.body().result == null){
                                changePasswordCallback.onPasswordChangeFailed(new Exception(), response.body().message);
                                return;
                            }

                            changePasswordCallback.onPasswordChanged(response.body().result);
                        }else{
                            changePasswordCallback.onPasswordChangeFailed(new Exception(), "Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<CareGiverData>> call, Throwable t) {
                        changePasswordCallback.onPasswordChangeFailed(new Exception(), "Please try again later.");
                    }
                });

    }

    public void connectCg(Map<String, String> body,
                          WelcomeContracts.ConnectCgCallback connectCgCallback){

        apiService.connectCg(body)
                .enqueue(new Callback<CallResponse<ConnectCgResult>>() {
                    @Override
                    public void onResponse(Call<CallResponse<ConnectCgResult>> call, Response<CallResponse<ConnectCgResult>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200 || response.body().result == null){
                                connectCgCallback.onCgConnectFailed(new Exception(), response.body().message);
                                return;
                            }

                            connectCgCallback.onCgConnected(response.body().result);
                        }else{
                            connectCgCallback.onCgConnectFailed(new Exception(), "Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<ConnectCgResult>> call, Throwable t) {
                        connectCgCallback.onCgConnectFailed(new Exception(), "Please try again later.");
                    }
                });

    }

    public void resendCgCode(Map<String, String> body,
                             String token,
                             @NonNull WelcomeContracts.ResendCgCodeCallback cgCodeCallback){

        apiService.resendCgCode(body, token)
                .enqueue(new Callback<CallResponse<Object>>() {
                    @Override
                    public void onResponse(Call<CallResponse<Object>> call, Response<CallResponse<Object>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200 || response.body().result == null){
                                cgCodeCallback.onCgCodeResentFailed(new Exception(), response.body().message);
                                return;
                            }

                            cgCodeCallback.onCgCodeResent();
                        }else{
                            cgCodeCallback.onCgCodeResentFailed(new Exception(), "Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<Object>> call, Throwable t) {
                        cgCodeCallback.onCgCodeResentFailed(new Exception(), t.getMessage());
                    }
                });

    }

}
