package com.app.simplitend.patientprofile.setuproutine.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.welcome.welcomepatient.mvvm.models.CallResponse;

import java.util.List;

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

public class RoutinesRepository {

    private static RoutinesRepository routineRepository;

    private final PatientProfileAPIService apiService;

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

    public static synchronized RoutinesRepository getRoutineRepository(){
        if (routineRepository == null){
            routineRepository = new RoutinesRepository();
        }

        return routineRepository;
    }

    public void getRoutines(int patient_id,
                            String token,
                            int week_day,
                            @NonNull ProfileContracts.GetRoutinesCallback routinesCallback){

        apiService.getRoutines(patient_id, week_day, token)
                .enqueue(new Callback<CallResponse<List<RoutineDetails>>>() {
                    @Override
                    public void onResponse(Call<CallResponse<List<RoutineDetails>>> call, Response<CallResponse<List<RoutineDetails>>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200 || response.body().result == null){
                                routinesCallback.onRoutinesFetchedFailed(new Exception(), response.body().message);
                                return;
                            }

                            routinesCallback.onRoutinesFetched(response.body().result);
                        }else{
                            routinesCallback.onRoutinesFetchedFailed(new Exception(), "Please try again later.");
                        }
                    }

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

    }

    public void addNUpdateRoutine(int patient_id,
                                  RoutineDetails routineDetails,
                                  String token,
                                  @NonNull ProfileContracts.AddNUpdateRoutineCallback routineCallback){

        apiService.addNUpdateRoutine(patient_id, routineDetails, token)
                .enqueue(new Callback<CallResponse<RoutineDetails>>() {
                    @Override
                    public void onResponse(Call<CallResponse<RoutineDetails>> call, Response<CallResponse<RoutineDetails>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200 || response.body().result == null){
                                routineCallback.onRoutineAddFailed(new Exception(), response.body().message);
                                return;
                            }

                            routineCallback.onRoutineAdded(response.body().result);
                        }else{
                            routineCallback.onRoutineAddFailed(new Exception(), "Please try again later.");
                        }
                    }

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

    }

    public void deleteRoutine(int patient_id,
                              int patient_routine_id,
                              int adapterPosition,
                              String token,
                              @NonNull ProfileContracts.RoutineDeleteCallback deleteCallback){

        apiService.deleteRoutine(patient_id, patient_routine_id, 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){
                                deleteCallback.onRoutineDeleteFailed(new Exception(), response.body().message);
                                return;
                            }

                            deleteCallback.onRoutineDeleted(adapterPosition);
                        }else{
                            deleteCallback.onRoutineDeleteFailed(new Exception(), "Please try again later.");
                        }
                    }

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

    }

}
