package com.rthoni.camotion.ui.dialogs;

import android.app.ProgressDialog;
import android.content.Context;

import com.luticate.auth.business.LuticatePermissionsBusiness;
import com.luticate.auth.business.LuticateUsersBusiness;
import com.luticate.auth.dbo.LuFullLoginDbo;
import com.luticate.auth.dbo.LuLoginDbo;
import com.luticate.auth.dbo.LuPermissionDbo;
import com.luticate.utils.business.LuPromise;
import com.rthoni.camotion.R;
import com.rthoni.camotion.dbo.LocationDbo;

/**
 *
 * Created by robin on 11/28/15.
 */
public class LoginDialog extends ProgressDialog {
    public LoginDialog(Context context) {
        super(context);
        setTitle(R.string.loading);
        setIndeterminate(true);
        setMessage(context.getString(R.string.login_progress));
        setCancelable(false);
    }

    public LuPromise<LuLoginDbo> login(LocationDbo location)
    {
        final LuPromise<LuLoginDbo> promise = new LuPromise<>();
        show();
        LuticateUsersBusiness.login(location.getConfig(null), location.getUsername(), location.getPassword())
                .then(new LuPromise.LuConsumer<LuLoginDbo>() {
                    @Override
                    public void execute(LuLoginDbo data) {
                        hide();
                        promise.resolve(data);
                    }
                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
                    @Override
                    public void execute(LuPromise.LuPromiseError data) {
                        hide();
                        promise.reject(data);
                    }
                });
        return promise;
    }

    public LuPromise<LuFullLoginDbo> loginFull(final LocationDbo location)
    {
        final LuPromise<LuFullLoginDbo> promise = new LuPromise<>();
        show();
        LuticateUsersBusiness.login(location.getConfig(null), location.getUsername(), location.getPassword())
                .then(new LuPromise.LuConsumer<LuLoginDbo>() {
                    @Override
                    public void execute(final LuLoginDbo loginDbo) {

                        LuticatePermissionsBusiness.getAllEffectiveMe(location.getConfig(loginDbo))
                                .then(new LuPromise.LuConsumer<LuPermissionDbo.LuMultiplePermissionDbo>() {
                                    @Override
                                    public void execute(LuPermissionDbo.LuMultiplePermissionDbo permissions) {
                                        LuFullLoginDbo dbo = new LuFullLoginDbo();
                                        dbo.setPermissions(permissions.getData());
                                        dbo.setLoginDbo(loginDbo);
                                        hide();
                                        promise.resolve(dbo);
                                    }
                                });
                    }
                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
                    @Override
                    public void execute(LuPromise.LuPromiseError data) {
                        hide();
                        promise.reject(data);
                    }
                });
        return promise;
    }
}
