public static final String EXTRA_BODY = EXTRA + ".BODY"; public static final String EXTRA_BODY_PLAIN = EXTRA + ".BODY_PLAIN"; public static final String EXTRA_BOT_API_TOKEN = EXTRA + ".BOT_API_TOKEN"; public static final String EXTRA_BOT_ICON_URL = EXTRA + ".BOT_ICON_URL"; public static final String EXTRA_BOT_ID = EXTRA + ".BOT_ID"; public static final String EXTRA_BOT_NAME = EXTRA + ".BOT_NAME"; public static final String EXTRA_CREATED_AT = EXTRA + ".CREATED_AT"; public static final String EXTRA_ID = EXTRA + ".ID"; public static final String EXTRA_IMAGE_URLS = EXTRA + ".IMAGE_URLS"; public static final String EXTRA_MENTIONS = EXTRA + ".MENTIONS"; public static final String EXTRA_MULTILINE = EXTRA + ".MULTILINE"; public static final String EXTRA_ORGANIZATION_SLUG = EXTRA + ".ORGANIZATION_SLUG"; public static final String EXTRA_ROOM_ID = EXTRA + ".ROOM_ID"; public static final String EXTRA_ROOM_NAME = EXTRA + ".ROOM_NAME"; public static final String EXTRA_SENDER_ICON_URL = EXTRA + ".SENDER_ICON_URL"; public static final String EXTRA_SENDER_ID = EXTRA + ".SENDER_ID"; public static final String EXTRA_SENDER_NAME = EXTRA + ".SENDER_NAME"; public static final String EXTRA_SENDER_TYPE = EXTRA + ".SENDER_TYPE";
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.uphyca.robota.engine.Robota.EXTRA_BODY;
import static com.uphyca.robota.engine.Robota.EXTRA_BODY_PLAIN;
import static com.uphyca.robota.engine.Robota.EXTRA_BOT_API_TOKEN;
import static com.uphyca.robota.engine.Robota.EXTRA_BOT_ICON_URL;
import static com.uphyca.robota.engine.Robota.EXTRA_BOT_ID;
import static com.uphyca.robota.engine.Robota.EXTRA_BOT_NAME;
import static com.uphyca.robota.engine.Robota.EXTRA_CREATED_AT;
import static com.uphyca.robota.engine.Robota.EXTRA_ID;
import static com.uphyca.robota.engine.Robota.EXTRA_IMAGE_URLS;
import static com.uphyca.robota.engine.Robota.EXTRA_MENTIONS;
import static com.uphyca.robota.engine.Robota.EXTRA_MULTILINE;
import static com.uphyca.robota.engine.Robota.EXTRA_ORGANIZATION_SLUG;
import static com.uphyca.robota.engine.Robota.EXTRA_ROOM_ID;
import static com.uphyca.robota.engine.Robota.EXTRA_ROOM_NAME;
import static com.uphyca.robota.engine.Robota.EXTRA_SENDER_ICON_URL;
import static com.uphyca.robota.engine.Robota.EXTRA_SENDER_ID;
import static com.uphyca.robota.engine.Robota.EXTRA_SENDER_NAME;
import static com.uphyca.robota.engine.Robota.EXTRA_SENDER_TYPE;
/*
 * Copyright (C) 2014 uPhyca Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.uphyca.robota.engine;




/**
 * @author Sosuke Masui (masui@uphyca.com)
 */
public abstract class EngineBase extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Robota.ACTION_MESSAGE_CREATED)) {
            try {
                handleMessageCreated(context, intent);
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }

    /**
     * This method is called when the BroadcastReceiver is receiving an message from Idobata.
     * 
     * @param context The Context in which the receiver is running.
     * @param bot The Bot in which the receiver is running.
     * @param textMessage The message being received.
     * @return
     */
    protected abstract String onMessageReceived(Context context, Bot bot, TextMessage textMessage);

    private void handleMessageCreated(Context context, Intent intent) {

        Bot bot = parseBot(intent);
        TextMessage textMessage = parseTextMessage(intent);

        if (getResultData() != null) {
            return;
        }

        String result = onMessageReceived(context, bot, textMessage);
        if (result != null) {
            setResultData(result);
        }
    }

    private Bot parseBot(Intent intent) {

        long id = intent.getLongExtra(EXTRA_BOT_ID, 0);
        String name = intent.getStringExtra(EXTRA_BOT_NAME);