package com.fusi24.entryPermitScanner.base;

import androidx.appcompat.app.AppCompatActivity;

import com.fusi24.entryPermitScanner.config.Constant;
import com.fusi24.entryPermitScanner.data.DataManager;
import com.fusi24.entryPermitScanner.data.SessionManager;
import com.fusi24.entryPermitScanner.data.api.RestService;
import com.fusi24.entryPermitScanner.data.api.RestServiceFactory;
import com.fusi24.entryPermitScanner.util.Helper;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public abstract class BaseActivity extends AppCompatActivity {

    private RestService restService;
    private SessionManager sessionManager;
    private DataManager manager;

    public RestService getRestService() {
        if (restService == null) {
            restService = RestServiceFactory.create(getJwtToken(), getSessionManager().getUserSid(), this);
        }
        return restService;
    }

    public SessionManager getSessionManager() {
        if (sessionManager == null) {
            sessionManager = new SessionManager(Helper.getDefaultPreferences(this));
        }
        return sessionManager;
    }

    public DataManager getManager() {
        if (manager == null) {
            manager = new DataManager(getRestService());
        }
        return manager;
    }

    protected Scheduler getAndroidScheduler() {
        return AndroidSchedulers.mainThread();
    }

    protected Scheduler getProcessScheduler() {
        return Schedulers.io();
    }

    private String getJwtToken() {
        return Constant.JWT_TOKEN;
    }

}
