package ru.myitschool.work.data.repository;

import androidx.annotation.NonNull;

import java.util.function.Consumer;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import ru.myitschool.work.data.dto.Code;
import ru.myitschool.work.data.network.RetrofitFactory;
import ru.myitschool.work.data.network.api.EmployeeApi;
import ru.myitschool.work.domain.entities.Status;
import ru.myitschool.work.domain.open.OpenRepository;

public class OpenRepositoryImpl implements OpenRepository {

    private static OpenRepositoryImpl INSTANCE;

    public static OpenRepositoryImpl getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new OpenRepositoryImpl();
        }
        return INSTANCE;
    }

    private EmployeeApi employeeApi = RetrofitFactory.getInstance().getEmployeeApi();

    @Override
    public void open(@NonNull String login, @NonNull Code code, @NonNull Consumer<Status<Void>> callback) {
        employeeApi.open(login, code).enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {
                callback.accept(new Status<>(response.code(), null, null));
            }

            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                callback.accept(new Status<>(-1, null, t));
            }
        });
    }
}
