package ru.myitschool.work.data.repository;

import androidx.annotation.NonNull;

import java.util.List;
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.dto.EntryDto;
import ru.myitschool.work.data.network.RetrofitFactory;
import ru.myitschool.work.data.network.api.AdminApi;
import ru.myitschool.work.domain.admin.AdminRepository;
import ru.myitschool.work.domain.entities.Status;

public class AdminRepositoryImpl implements AdminRepository {
    private static AdminRepositoryImpl INSTANCE;

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

    private AdminApi adminApi = RetrofitFactory.getInstance().getAdminApi();

    @Override
    public void amIAdmin(@NonNull String auth, @NonNull Consumer<Status<Void>> callback) {
        adminApi.amIAdmin(auth).enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {
                callback.accept(new Status<>(response.code(), response.body(), null));
            }

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

    @Override
    public void getEmployeeInfo(@NonNull String auth, @NonNull String login, @NonNull Consumer<Status<EmployeeDataDto>> callback) {
        adminApi.getEmployeeInfo(auth, 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));
            }
        });
    }

    @Override
    public void setBlockCondition(@NonNull String auth, @NonNull String login, @NonNull Boolean condition, @NonNull Consumer<Status<Void>> callback) {
        adminApi.setBlockCondition(auth, login, condition).enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {
                callback.accept(new Status<>(response.code(), response.body(), null));
            }

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

    @Override
    public void getEmployeeEntryList(@NonNull String auth, @NonNull String login, @NonNull Consumer<Status<List<EntryDto>>> callback) {
        adminApi.getEmployeeEntry(auth, login).enqueue(new Callback<List<EntryDto>>() {
            @Override
            public void onResponse(Call<List<EntryDto>> call, Response<List<EntryDto>> response) {
                callback.accept(new Status<>(response.code(), response.body(), null));
            }

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

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

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