package eic.beike.projectx.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import eic.beike.projectx.R;
import eic.beike.projectx.util.Constants;


/**
 * @author Adam Ingmansson
 */
public class NameSplashActivity extends Activity {

    public static final int MENU_ACTIVITY = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_init);

        SharedPreferences settings = getSharedPreferences(Constants.SETTINGS_FILE,0);

        // Used to fetch the phone id
        final TelephonyManager tm = (TelephonyManager) getBaseContext()
                .getSystemService(Context.TELEPHONY_SERVICE);

        final String androidId;
        androidId = tm.getDeviceId();

        // Save it
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(Constants.ID_FIELD, androidId);

        editor.apply();
    }

    @Override
    public void onBackPressed() {
        // Do Nothing
    }

    /**
     * Handles clicks form the button in the view
     * @param view Unused (Triggering view)
     */
    public void onClick(View view) {
        EditText editBox = (EditText) findViewById(R.id.nameInputField);
        String text = editBox.getText().toString();

        // Lets save the name in permanent storage
        SharedPreferences settings = getSharedPreferences(Constants.SETTINGS_FILE, 0);
        SharedPreferences.Editor editor = settings.edit();

        editor.putString(Constants.NAME_FIELD,text);

        editor.apply();


        // Start menu activity
        Intent intent = new Intent(this, MenuActivity.class);

        // Set so there is no history for back button
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent, NameSplashActivity.MENU_ACTIVITY);
    }

    /**
     * Handles the result from any activity started by this.
     * Used for stopping this when user exits.
     *
     * @param requestCode The code identifying which request
     * @param resultCode The result code of that request
     * @param data The intent
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MENU_ACTIVITY) {
            if (resultCode == NameSplashActivity.RESULT_CANCELED) {
                // Bubble the "exit" to next activity (Main)
                setResult(NameSplashActivity.RESULT_CANCELED);
                finish();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_name_splash, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -185,22 +185,76 @@
 roid.os.
-Bundle
+AsyncTask;%0Aimport android.os.Bundle;%0Aimport android.os.Debug
 ;%0Aimport
@@ -290,16 +290,41 @@
 anager;%0A
+import android.util.Log;%0A
 import a
@@ -459,16 +459,135 @@
 ectx.R;%0A
+import eic.beike.projectx.network.projectXServer.Database;%0Aimport eic.beike.projectx.network.projectXServer.IDatabase;%0A
 import e
@@ -757,16 +757,96 @@
 Y = 0;%0A%0A
+    //TODO: Make database work like%0A    private IDatabase db = new Database();%0A%0A
     @Ove
@@ -2093,24 +2093,24 @@
 ELD,text);%0A%0A
-
         edit
@@ -2122,16 +2122,1133 @@
 ply();%0A%0A
+        // Register the username on the server in a background thread.%0A        new AsyncTask%3CVoid, Void, Void%3E()%7B%0A            @Override%0A            protected Void doInBackground(Void... voids) %7B%0A%0A                //Get name and id from settings file.%0A                SharedPreferences settings = getSharedPreferences(Constants.SETTINGS_FILE, 0);%0A                String name = settings.getString(Constants.NAME_FIELD, %22%22);%0A                String id = settings.getString(Constants.ID_FIELD,%22%22);%0A%0A                //Register to server.%0A                try %7B%0A                   boolean failure = db.register(id, name);%0A                   if(failure)%7B%0A                       Log.d(this.getClass().getSimpleName(),%22Failed to register nickname!%22);%0A                   %7D%0A                %7D catch (Exception e)%7B%0A                    //Log any errors%0A                    Log.e(this.getClass().getSimpleName(),e.getMessage()+%22%22);%0A%0A                    //TODO: Show dialog as well.%0A                %7D%0A%0A                // Need to return something, this is androids fault.%0A                return null;%0A            %7D%0A        %7D.execute();%0A%0A
 %0A       

Make nameplash register user.