package com.rthoni.camotion.ui.dialogs;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;

import com.luticate.utils.business.LuPromise;
import com.luticate.utils.dbo.LuDataAccessConfigDbo;
import com.luticate.utils.dbo.LuVoidDbo;
import com.rthoni.camotion.R;
import com.rthoni.camotion.business.CommandsBusiness;
import com.rthoni.camotion.dbo.CommandDbo;

/**
 *
 * Created by robin on 11/28/15.
 */
public abstract class CommandExecDialog {

    public static void showDialog(final LuDataAccessConfigDbo config, final Context context, final CommandDbo command)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(context.getString(R.string.command_confirm, command.getName()))
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showLoadingDialog(config, context, command);
                    }
                })
                .setNegativeButton(android.R.string.no, null)
                .show();
    }

    public static LuPromise<Boolean> showLoadingDialog(final LuDataAccessConfigDbo config, final Context context, final CommandDbo command)
    {
        final LuPromise<Boolean> promise = new LuPromise<>();
        final ProgressDialog dlg = new ProgressDialog(context);
        dlg.setIndeterminate(true);
        dlg.setTitle(R.string.loading);
        dlg.setMessage(context.getString(R.string.command_loading, command.getName()));
        dlg.setCancelable(false);
        dlg.show();
        CommandsBusiness.exec(config, command).then(new LuPromise.LuConsumer<LuVoidDbo>() {
            @Override
            public void execute(LuVoidDbo data) {
                dlg.hide();
                showEndDialog(context, context.getString(R.string.command_success, command.getName()))
                    .then(new LuPromise.LuConsumer<Boolean>() {
                        @Override
                        public void execute(Boolean data) {
                            promise.resolve(null);
                        }
                    });
            }
        }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
            @Override
            public void execute(final LuPromise.LuPromiseError error) {
                dlg.hide();
                showEndDialog(context, context.getString(R.string.error, error.toString()))
                        .then(new LuPromise.LuConsumer<Boolean>() {
                            @Override
                            public void execute(Boolean data) {
                                promise.reject(error);
                            }
                        });
            }
        });
        return promise;
    }

    protected static LuPromise<Boolean> showEndDialog(Context context, String text)
    {
        final LuPromise<Boolean> promise = new LuPromise<>();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(text)
                .setCancelable(true)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        promise.resolve(null);
                    }
                })
                .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        promise.resolve(null);
                    }
                })
                .show();
        return promise;
    }
}
