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.EmployeeDataDto;
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.info.InfoRepository;

public class InfoRepositoryImpl implements InfoRepository {
    private static InfoRepositoryImpl INSTANCE;

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

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


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

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