package ru.myitschool.work.ui.employee;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import ru.myitschool.work.data.dto.EmployeeDataDto;
import ru.myitschool.work.data.repository.AdminRepositoryImpl;
import ru.myitschool.work.data.repository.InfoRepositoryImpl;
import ru.myitschool.work.domain.admin.AmIAdminUseCase;
import ru.myitschool.work.domain.info.InfoUseCase;

public class EmployeeViewModel extends ViewModel {
    private final MutableLiveData<Boolean> mutableIsSuccessfulReceiptLiveData = new MutableLiveData<>();
    public final LiveData<Boolean> isSuccessfulReceiptLiveData = mutableIsSuccessfulReceiptLiveData;

    private final MutableLiveData<EmployeeDataDto> mutableEmployeeLiveData = new MutableLiveData<>();
    public final LiveData<EmployeeDataDto> employeeLiveData = mutableEmployeeLiveData;

    private final MutableLiveData<Boolean> mutableAmIAdminLiveData = new MutableLiveData<>();
    public final LiveData<Boolean> amIAdminLiveData = mutableAmIAdminLiveData;

    private final InfoUseCase infoUseCase = new InfoUseCase(InfoRepositoryImpl.getInstance());
    private final AmIAdminUseCase amIAdminUseCase = new AmIAdminUseCase(AdminRepositoryImpl.getInstance());

    public void startQuery(String currentLogin) {
        infoUseCase.execute(currentLogin, status -> {
            if (status.getStatusCode() == 200) {
                mutableEmployeeLiveData.postValue(status.getValue());
                mutableIsSuccessfulReceiptLiveData.setValue(true);
            } else /*(status.getStatusCode() == 401) */ {
                mutableIsSuccessfulReceiptLiveData.setValue(false);
            }
        });
    }

    public void checkAmIAdmin(String auth) {
        amIAdminUseCase.execute(auth, status -> {
            if (status.getStatusCode() == 200) {
                mutableAmIAdminLiveData.setValue(true);
            } else {
                mutableIsSuccessfulReceiptLiveData.setValue(false);
            }
        });
    }
}