/* MIT ICT4DLab SMS Lab framework
 * 
 * Copyright (C) 2008, Massachusetts Institute of Technology
 * 
 * ICT4DLab SMS is distributed under the terms of the Apache License version 2.0
 * 
 * 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.
 * 
 * original author: Luis F. G. Sarmenta ( lfgs @ mit . edu ) 
 * v0.1.0: 20080228
 * 
 * Last modified by:
 * date:
 * comment:
 */

package org.ict4dlab.sms.adapters.simpletest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.ict4dlab.sms.core.SMSAppUtil;
import org.ict4dlab.sms.core.SMSApplication;
import org.ict4dlab.sms.core.SMSMessage;
import org.ict4dlab.sms.core.SMSSender;

public class SimpleTest implements SMSSender {

    private SMSApplication smsApp;

    public SimpleTest(SMSApplication smsApp) {
	super();
	this.smsApp = smsApp;
	this.smsApp.setSMSSender(this);
    }

    public static void usage() {
	System.out.println("Usage:  "
		+ "java org.ict4dlab.sms.adapters.simpletest.SimpleTest "
		+ "<appClassname> <optional: srcAddr destAddr text ...>");
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
	if (args.length < 1) {
	    usage();
	    System.exit(-1);
	}

	SMSApplication newSMSApp = null;
	try {
	    newSMSApp = (SMSApplication) Class.forName(args[0]).newInstance();
	} catch (Exception e) {
	    e.printStackTrace();
	}
	if (newSMSApp == null) {
	    System.exit(-2);
	}
	SimpleTest simpleTest = new SimpleTest(newSMSApp);
	if (args.length < 4) {
	    simpleTest.inputLoopTest();
	} else {
	    simpleTest.processSMS(args[1], args[2], SMSAppUtil
		    .concatFromStringArray(args, 3, args.length - 1));
	}

    }

    public void processSMS(String srcAddr, String destAddr, String inText) {
	SMSMessage inMsg = new SMSMessage(srcAddr, destAddr, inText);

	SMSMessage outMsg = this.smsApp.onSMSMessageReceived(inMsg);

	System.out.println(">>> RESPONSE from Application");
	System.out.println(outMsg);

    }

    public void inputLoopTest() {
	InputStreamReader inputStreamReader = new InputStreamReader(System.in);
	BufferedReader stdin = new BufferedReader(inputStreamReader);
	String srcAddr = null;
	String destAddr = null;
	String inText = null;
	do {
	    System.out.println("<<< ENTER new incoming SMS message");
	    System.out.print("from: ");
	    try {
		srcAddr = stdin.readLine();
		if (!"quit".equalsIgnoreCase(srcAddr)) {
		    System.out.print("to: ");
		    destAddr = stdin.readLine();
		    System.out.print("text: ");
		    inText = stdin.readLine();

		    processSMS(srcAddr, destAddr, inText);
		}
	    } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	} while (!"quit".equalsIgnoreCase(srcAddr));
    }

    public boolean sendSMSMessage(SMSMessage outgoingMsg) {

	System.out.println(">>> SENDING via SMSSender");
	System.out.println(outgoingMsg);
	return true;
    }
}
