package com.rthoni.camotion.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.luticate.auth.dbo.LuFullLoginDbo;
import com.luticate.utils.business.LuPromise;
import com.luticate.utils.business.LuRequest;
import com.rthoni.camotion.R;
import com.rthoni.camotion.dbo.CommandDbo;
import com.rthoni.camotion.dbo.LocationDbo;
import com.rthoni.camotion.ui.dialogs.LocationPickerDialog;
import com.rthoni.camotion.ui.dialogs.LoginDialog;
import com.rthoni.camotion.ui.fragments.AbstractCommandsFragment;

public class AddCommandShortcutActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LuRequest.init(this);
        setContentView(R.layout.content_main);
        selectLocation();
    }

    private void selectLocation()
    {
        LocationPickerDialog.getLocation(this)
                .then(new LuPromise.LuConsumer<LocationDbo>() {
                    @Override
                    public void execute(LocationDbo location) {
                        login(location);
                    }
                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
                    @Override
                    public void execute(LuPromise.LuPromiseError data) {
                        setResult(RESULT_CANCELED);
                        finish();
                    }
                });
    }

    private void login(final LocationDbo location)
    {
        LoginDialog dlg = new LoginDialog(this);
        dlg.loginFull(location).then(new LuPromise.LuConsumer<LuFullLoginDbo>() {
            @Override
            public void execute(LuFullLoginDbo loginDbo) {
                showCommands(loginDbo, location);
            }
        }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
            @Override
            public void execute(LuPromise.LuPromiseError error) {
                selectLocation();
                Toast.makeText(AddCommandShortcutActivity.this, error.toString(), Toast.LENGTH_LONG).show();
            }
        });
    }

    private void showCommands(final LuFullLoginDbo loginDbo, final LocationDbo location)
    {
        AbstractCommandsFragment fragment = new AbstractCommandsFragment() {
            @Override
            protected void onCommandClicked(CommandDbo command) {
                addShortcut(location, command);
            }
        };
        fragment.setCamotionDbo(loginDbo, location);
        getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
    }

    private void addShortcut(LocationDbo location, CommandDbo command)
    {
        Intent shortcutIntent = new Intent(this, CommandExecActivity.class);
        shortcutIntent.putExtra("LOCATION_PSEUDO_MAIL", location.getPseudoMail());
        shortcutIntent.putExtra("COMMAND", command.toString());
        Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_command_bg);

        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, command.getName());
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        setResult(RESULT_OK, intent);
        finish();
    }
}
