package com.app.simplitend.patientprofile.medreminder.mvvm;

import androidx.annotation.NonNull;

import com.app.simplitend.apputils.RetrofitHelper;
import com.app.simplitend.patientprofile.PatientProfileAPIService;
import com.app.simplitend.patientprofile.ProfileContracts;
import com.app.simplitend.patientprofile.medreminder.mvvm.models.FreqNMedTypeResult;
import com.app.simplitend.patientprofile.medreminder.mvvm.models.ReminderResult;
import com.app.simplitend.welcome.welcomepatient.mvvm.models.CallResponse;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ReminderRepository {

    private static ReminderRepository reminderRepository;

    private final PatientProfileAPIService apiService;

    private ReminderRepository(){
        apiService = RetrofitHelper.getRetrofit().create(PatientProfileAPIService.class);
    }

    public static synchronized ReminderRepository getReminderRepository(){
        if (reminderRepository == null){
            reminderRepository = new ReminderRepository();
        }

        return reminderRepository;
    }

    public void getRemindersList(int patient_id, int day_of_week, String token,
                                 @NonNull ProfileContracts.GetRemindersListCallback remindersListCallback){

        apiService.getRemindersList(patient_id, day_of_week, token)
                .enqueue(new Callback<CallResponse<List<ReminderResult>>>() {
                    @Override
                    public void onResponse(Call<CallResponse<List<ReminderResult>>> call, Response<CallResponse<List<ReminderResult>>> response) {
                        if (response.isSuccessful() && response.body() != null && response.body().result != null){
                            remindersListCallback.onRemindersListFetched(response.body().result);
                        }
                        else if (response.body() != null){
                            remindersListCallback.onFetchRemindersListFailed(new Exception(response.raw().toString()), response.body().message);
                        }
                        else{
                            remindersListCallback.onFetchRemindersListFailed(new Exception(), "It's not you, it's us.\nPlease try again later");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<List<ReminderResult>>> call, Throwable t) {
                        remindersListCallback.onFetchRemindersListFailed(t, "It's not you, it's us.\nPlease try again later");
                    }
                });
    }

    public void addReminder(int patient_id,
                            ReminderResult body,
                            String token, @NonNull ProfileContracts.AddReminderCallBack addReminderCallBack){

        apiService.addReminder(patient_id, body, token)
                .enqueue(new Callback<CallResponse<ReminderResult>>() {
                    @Override
                    public void onResponse(Call<CallResponse<ReminderResult>> call, Response<CallResponse<ReminderResult>> response) {
                        if (response.isSuccessful() && response.body() != null){
                            if (response.body().status != 200){
                                addReminderCallBack.onReminderAddFailed(new Exception(), response.body().message);
                                return;
                            }

                            addReminderCallBack.onReminderAdded(response.body().result);
                        }
                        else if (response.body() != null){
                            addReminderCallBack.onReminderAddFailed(new Exception(response.raw().toString()), response.body().message);
                        }
                        else{
                            addReminderCallBack.onReminderAddFailed(new Exception(), "It's not you, it's us.\nPlease try again later");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<ReminderResult>> call, Throwable t) {
                        addReminderCallBack.onReminderAddFailed(t, "It's not you, it's us.\nPlease try again later");
                    }
                });

    }

    public void fetchFreqNMedTypes(String token,
                                   @NonNull ProfileContracts.FreqNMedTypesCallback freqNMedTypesCallback){
        apiService.getFreqNMedTypes(token)
                .enqueue(new Callback<CallResponse<FreqNMedTypeResult>>() {
                    @Override
                    public void onResponse(Call<CallResponse<FreqNMedTypeResult>> call, Response<CallResponse<FreqNMedTypeResult>> response) {
                        if (response.isSuccessful() && response.body() != null && response.body().result != null){
                            freqNMedTypesCallback.onFreqNMedTypeLoaded(response.body().result);
                        }
                        else if (response.body() != null){
                            freqNMedTypesCallback.onFreqNMedTypeLoadFailed(new Exception(response.raw().toString()), response.body().message);
                        }
                        else{
                            freqNMedTypesCallback.onFreqNMedTypeLoadFailed(new Exception(), "It's not you, it's us.\nPlease try again later");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<FreqNMedTypeResult>> call, Throwable t) {
                        freqNMedTypesCallback.onFreqNMedTypeLoadFailed(t, "It's not you, it's us.\nPlease try again later");
                    }
                });
    }

    public void deleteReminder(int patient_id,
                               int patient_reminder_id,
                               final int adapterPos,
                               String token,
                               @NonNull ProfileContracts.ReminderDeleteCallback deleteCallback){
        apiService.deleteReminder(patient_id, patient_reminder_id, token)
                .enqueue(new Callback<CallResponse<List<Void>>>() {
                    @Override
                    public void onResponse(Call<CallResponse<List<Void>>> call, Response<CallResponse<List<Void>>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200){
                                deleteCallback.onReminderDeleteFailed(new Exception(), response.body().message);
                                return;
                            }

                            deleteCallback.onReminderDeleted(adapterPos);
                        }else{
                            deleteCallback.onReminderDeleteFailed(new Exception(), "Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<List<Void>>> call, Throwable t) {
                        deleteCallback.onReminderDeleteFailed(new Exception(), "Please try again later.");
                    }
                });
    }

}
