package credit.android.dg;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class ConnectScreen extends Activity implements Constants{	
	private TextView t;
	private String DeviceID;
	private int Message;
	private EditText et;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.connect_screen);
		resolveExtras(savedInstanceState);
		showDialog(Message);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		LayoutInflater factory = LayoutInflater.from(this);
		final View textEntryView;
		
		switch (id){
		case FIRST_TIME_DIALOG:
            textEntryView = factory.inflate(R.layout.first_time, null);
            /*((EditText)findViewById(R.id.username_edit)).setSingleLine();*/
            /*m_UserName = (EditText)findViewById(R.id.username_edit);
            m_UserName.setSingleLine();*/
            return new AlertDialog.Builder(ConnectScreen.this)
            	.setView(textEntryView)
            	.setPositiveButton(R.string.OK,new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
    
                        /* User clicked OK so do some stuff 
                         * Register new DeviceID */
                    	doEnterGame();
                    }
                })
                .create();
		case LOGIN_DIALOG:
            textEntryView = factory.inflate(R.layout.welcome, null);
            return new AlertDialog.Builder(ConnectScreen.this)
            	.setTitle(R.string.welcome)
            	.setView(textEntryView)
            	.setPositiveButton(R.string.OK,new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
    
                        /* User clicked OK so do some stuff 
                         * Connect to DataBase */
                    	doEnterGame();
                    }
                })
                .create();
		}
		return null;
	}
	
	public void doEnterGame(){
		Intent i = new Intent(this, BasicScreen.class);
    	i.putExtra(DIALOG_STRING, FIRST_TIME_DIALOG);
    	i.putExtra(DEVICE_ID_STRING, DeviceID);
    	startActivity(i);
	}
	
	public void resolveExtras(Bundle bundle){
		Bundle extras = getIntent().getExtras();
		Message = bundle != null ? bundle.getInt(DIALOG_STRING) : 0;
		DeviceID = bundle != null ? bundle.getString(DEVICE_ID_STRING) : null;
		if (Message == 0){			
			Message = extras != null ? extras.getInt(DIALOG_STRING) : 0;
		}
		if (DeviceID == null){
			DeviceID = extras != null ? extras.getString(DEVICE_ID_STRING) : null;
		}
	}
}
