package com.rthoni.camotion.ui;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import com.luticate.auth.dbo.LuLoginDbo;
import com.luticate.utils.business.LuPromise;
import com.rthoni.camotion.R;
import com.rthoni.camotion.business.CamotionBusiness;
import com.rthoni.camotion.dbo.CommandDbo;
import com.rthoni.camotion.dbo.LocationDbo;
import com.rthoni.camotion.ui.dialogs.CommandExecDialog;
import com.rthoni.camotion.ui.dialogs.LoginDialog;

import org.json.JSONObject;

import java.util.List;

public class CommandExecActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_command_exec);

        CommandDbo command = new CommandDbo();
        try {
            JSONObject commandJson = new JSONObject(getIntent().getStringExtra("COMMAND"));
            command.fromJson(commandJson);
        } catch (Exception e) {
            e.printStackTrace();
            setResult(RESULT_CANCELED);
            finish();
            return;
        }

        LocationDbo location = getLocation();
        if (location == null) {
            setResult(RESULT_CANCELED);
            finish();
            return;
        }

        askConfirmation(command, location);
    }

    private LocationDbo getLocation()
    {
        String locationPseudoMail = getIntent().getStringExtra("LOCATION_PSEUDO_MAIL");
        List<LocationDbo> locations = CamotionBusiness.getLocations(this);
        for (LocationDbo location : locations) {
            if (location.getPseudoMail().equals(locationPseudoMail)) {
                return location;
            }
        }
        return null;
    }

    private void askConfirmation(final CommandDbo command, final LocationDbo location)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(this.getString(R.string.command_confirm, command.getName()))
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        login(command, location);
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        setResult(RESULT_CANCELED);
                        finish();
                    }
                })
                .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        setResult(RESULT_CANCELED);
                        finish();
                    }
                })
                .show();
    }

    private void login(final CommandDbo command, final LocationDbo location)
    {
        LoginDialog dlg = new LoginDialog(this);
        dlg.login(location).then(new LuPromise.LuConsumer<LuLoginDbo>() {
            @Override
            public void execute(LuLoginDbo loginDbo) {
                CommandExecDialog.showLoadingDialog(location.getConfig(loginDbo), CommandExecActivity.this, command)
                .then(new LuPromise.LuConsumer<Boolean>() {
                    @Override
                    public void execute(Boolean data) {
                        setResult(RESULT_OK);
                        finish();
                    }
                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
                    @Override
                    public void execute(LuPromise.LuPromiseError error) {
                        setResult(RESULT_CANCELED);
                        finish();
                    }
                });
            }
        }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
            @Override
            public void execute(LuPromise.LuPromiseError error) {
                Toast.makeText(CommandExecActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                setResult(RESULT_CANCELED);
                finish();
            }
        });
    }
}
