package com.fusi24.rfid.ui.home;

import com.fusi24.rfid.base.BasePresenter;
import com.fusi24.rfid.data.DataManager;

import io.reactivex.Scheduler;

class HomePresenter extends BasePresenter<HomeView> {

    private DataManager manager;
    private Scheduler androidScheduler;
    private Scheduler processScheduler;

    HomePresenter(DataManager manager, Scheduler androidScheduler, Scheduler processScheduler) {
        this.manager = manager;
        this.androidScheduler = androidScheduler;
        this.processScheduler = processScheduler;
    }

    void getEntryPermit() {
        disposable.add(manager.getEntryPermit()
                .observeOn(androidScheduler)
                .subscribeOn(processScheduler)
                .subscribe(result -> {
                    if (isViewAttached()) {
                        getView().showDataPermit(result);
                    }
                }, throwable -> {
                    if (isViewAttached()) {
                        getView().showError(throwable);
                    }
                }));
    }

    void getStatusScan() {
        disposable.add(manager.getStatusScan("BeSCAN_CHECK_TYPE")
                .observeOn(androidScheduler)
                .subscribeOn(processScheduler)
                .subscribe(result -> {
                    if (isViewAttached()) {
                        getView().showDataStatusScan(result);
                    }
                }, throwable -> {
                    if (isViewAttached()) {
                        getView().showError(throwable);
                    }
                }));
    }

    void getDataDevice(String deviceId) {
        String[] expand = {"gate.sites"};
        disposable.add(manager.getDataDevice(deviceId, expand)
                .observeOn(androidScheduler)
                .subscribeOn(processScheduler)
                .subscribe(result -> {
                    if (isViewAttached()) {
                        getView().showDataDevice(result);
                    }
                }, throwable -> {
                    if (isViewAttached()) {
                        getView().showError(throwable);
                    }
                }));
    }

}
