package de.reneruck.contactor;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import de.reneruck.contactor.models.Contact;

/**
 * Created by reneruck on 19/06/15.
 */
public class EditContactActivity extends AppCompatActivity {

    private ContactsOpenHelper sqlHelper;
    private Contact currentContact;

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

        this.sqlHelper = new ContactsOpenHelper(getApplicationContext());
        this.currentContact = new Contact(getIntent().getIntExtra("contactId", -1));

        if (this.currentContact.getId() != -1) {
            loadContactData();
            populateFields();
        }
    }

    @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_edit_contact, 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);
    }

    private void populateFields() {
        ((EditText) findViewById(R.id.input_name)).setText(this.currentContact.getName());
        ((EditText) findViewById(R.id.input_email)).setText(this.currentContact.getEmail());
        ((EditText) findViewById(R.id.input_phone_private)).setText(this.currentContact.getPhonePrivate());
        ((EditText) findViewById(R.id.input_phone_work)).setText(this.currentContact.getPhoneWork());
    }

    private void loadContactData() {
        String[] contactIds = new String[]{String.valueOf(this.currentContact.getId())};
        Cursor contactResult = this.sqlHelper.getReadableDatabase().rawQuery("select * from contacts where _id = ?", contactIds);
        contactResult.moveToFirst();
        this.currentContact = new Contact(contactResult);
    }
}

@@ -25,16 +25,54 @@
 actor;%0A%0A
+import android.content.ContentValues;%0A
 import a
@@ -257,16 +257,45 @@
 ditText;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -1536,174 +1536,1609 @@
     
-//noinspection SimplifiableIfStatement%0A        if (id == R.id.action_settings) %7B%0A            return true;%0A        %7D%0A%0A        return super.onOptionsItemSelected(item);
+switch(id)%7B%0A            case R.id.action_save:%0A                if(updateContact())%7B%0A                    Toast.makeText(getApplicationContext(), %22Contact saved%22, Toast.LENGTH_LONG).show();%0A                    finish();%0A                %7D else %7B%0A                    Toast.makeText(getApplicationContext(), %22Couldn't save this contact, try again%22, Toast.LENGTH_LONG).show();%0A                %7D%0A        %7D%0A%0A        return true;%0A    %7D%0A%0A    private boolean updateContact()%7B%0A        String name = ((EditText) findViewById(R.id.input_name)).getText().toString();%0A%0A        String%5B%5D nameSplit = name.split(%22 %22);%0A%0A        String email = ((EditText) findViewById(R.id.input_email)).getText().toString();%0A        String phone_priv = ((EditText) findViewById(R.id.input_phone_private)).getText().toString();%0A        String phone_work = ((EditText) findViewById(R.id.input_phone_work)).getText().toString();%0A%0A        ContentValues values = new ContentValues();%0A        values.put(%22firstName%22, nameSplit%5B0%5D);%0A        values.put(%22lastName%22, nameSplit%5B1%5D);%0A        values.put(%22email%22, email);%0A        values.put(%22phonePrivate%22, phone_priv);%0A        values.put(%22phoneWork%22, phone_work);%0A%0A        String%5B%5D args = %7BString.valueOf(this.currentContact.getId())%7D;%0A        if(this.currentContact.getId() != -1)%7B%0A            return this.sqlHelper.getWritableDatabase().update(ContactsOpenHelper.CONTACTS_TABLE_NAME, values, %22where _id = ?%22, args) %3E 0 ? true : false;%0A        %7D else %7B%0A            return this.sqlHelper.getWritableDatabase().insert(ContactsOpenHelper.CONTACTS_TABLE_NAME, null, values) %3E 0 ? true : false;%0A        %7D
 %0A   

save or update contact entry save button click