package jp.ito.camera;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class XmppSetting extends Activity {
	private static final String TAG = XmppSetting.class.getSimpleName();
	static final String USER = "Xmpp.user";
	static final String PASSWORD = "Xmpp.PassWord";
	static final String HOST = "Xmpp.Host";
	static final String PORT = "Xmpp.Port";
	static final String SERVICE = "Xmpp.Service";
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.xmppsetting);
        setTitle("見守り君 - XMPP設定");

        SharedPreferences pref = getSharedPreferences("jp.ito.mimamori",MODE_PRIVATE);
		String strAddress = pref.getString(USER, "boo");
		EditText editAddress = (EditText)findViewById( R.id.editXmppUser );
		editAddress.setText(strAddress);
        
		String strPassWord = pref.getString(PASSWORD, "");
		EditText editPassWord = (EditText)findViewById( R.id.editXmppPassWord );
		editPassWord.setText(strPassWord);
        
		String strHost = pref.getString(HOST, "talk.google.com");
		EditText editHost = (EditText)findViewById( R.id.editXmppHost );
		editHost.setText(strHost);
        
		String strPort = pref.getString(PORT, "5222");
		EditText editPort = (EditText)findViewById( R.id.editXmppPort );
		editPort.setText(strPort);
        
		String strService = pref.getString(SERVICE, "gmail.com");
		EditText editService = (EditText)findViewById( R.id.editXmppService );
		editService.setText(strService);
		Button btn  = (Button) findViewById(R.id.button);
        btn.setOnClickListener(btnListener);	//リスナの登録
    }
    private OnClickListener btnListener = new OnClickListener() {
        public void onClick(View v) {
            switch(v.getId()){
            case R.id.button:
                SharedPreferences pref = getSharedPreferences("jp.ito.mimamori",MODE_PRIVATE);
        		Editor e = pref.edit();
        		e.putString(USER, ((EditText)findViewById( R.id.editXmppUser )).getText().toString());
        		e.putString(PASSWORD, ((EditText)findViewById( R.id.editXmppPassWord )).getText().toString());
        		e.putString(HOST, ((EditText)findViewById( R.id.editXmppHost )).getText().toString());
        		e.putString(PORT, ((EditText)findViewById( R.id.editXmppPort )).getText().toString());
        		e.putString(SERVICE, ((EditText)findViewById( R.id.editXmppService )).getText().toString());
        		e.commit();
        		Intent i = new Intent();
        		setResult(RESULT_OK, i);
        		finish();
                break;
            }
        }
    };
    @Override
    protected void onStart() {
        super.onStart();
		Log.d(TAG, "onStart()");
    }
    //アプリの開始
    @Override
    protected void onResume() {
        super.onResume();
		Log.d(TAG, "onResume()");
    }

    @Override
    protected void onPause(){
		super.onPause();
		Log.d(TAG, "onPause()");
	}

    //アプリの停止
    @Override
    protected void onStop() {
        super.onStop();
		Log.d(TAG, "onStop()");
    }    

    @Override
    protected void onDestroy(){
		super.onDestroy();
		Log.d(TAG, "onDestroy()");
	}
}
