package com.averi.worldscribe.activities;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.averi.worldscribe.Category;
import com.averi.worldscribe.R;
import com.averi.worldscribe.utilities.AppPreferences;

public class CreateArticleActivity extends AppCompatActivity {

    public static final int PERSON_ITEM_POSITION = 0;
    public static final int GROUP_ITEM_POSITION = 1;
    public static final int PLACE_ITEM_POSITION = 2;
    public static final int ITEM_ITEM_POSITION = 3;
    public static final int CONCEPT_ITEM_POSITION = 4;

    private Spinner categorySpinner;

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

        categorySpinner = (Spinner) findViewById(R.id.categorySelection);

        populateCategorySpinner();
        selectInitialCategory();
    }

    private void populateCategorySpinner() {
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.categories_array, R.layout.spinner_item);
        categorySpinner.setAdapter(adapter);
    }

    /**
     * Select the initial Category to display in the spinner, based on the Category of the
     * previous activity.
     */
    private void selectInitialCategory() {
        Category previousCategory = (Category) getIntent().getSerializableExtra(
                AppPreferences.CATEGORY);
        switch (previousCategory) {
            case Person:
                categorySpinner.setSelection(PERSON_ITEM_POSITION);
                break;
            case Group:
                categorySpinner.setSelection(GROUP_ITEM_POSITION);
                break;
            case Place:
                categorySpinner.setSelection(PLACE_ITEM_POSITION);
                break;
            case Item:
                categorySpinner.setSelection(ITEM_ITEM_POSITION);
                break;
            case Concept:
            default:
                categorySpinner.setSelection(CONCEPT_ITEM_POSITION);
                break;
        }
    }

    public void createArticle(View view) {

    }

}

@@ -111,16 +111,79 @@
 Bundle;%0A
+import android.text.Editable;%0Aimport android.text.TextWatcher;%0A
 import a
@@ -226,19 +226,20 @@
 get.
+Array
 Adapter
-View
 ;%0Aim
@@ -254,36 +254,62 @@
 roid.widget.
-ArrayAdapter
+Button;%0Aimport android.widget.EditText
 ;%0Aimport and
@@ -789,16 +789,48 @@
 N = 4;%0A%0A
+    private EditText nameField;%0A
     priv
@@ -857,16 +857,49 @@
 Spinner;
+%0A    private Button createButton;
 %0A%0A    @O
@@ -1062,24 +1062,87 @@
 _article);%0A%0A
+        nameField = (EditText) findViewById(R.id.articleName);%0A
         cate
@@ -1202,16 +1202,75 @@
 ection);
+%0A        createButton = (Button) findViewById(R.id.create);
 %0A%0A      
@@ -1327,24 +1327,51 @@
 Category();%0A
+        addTextListener();%0A
     %7D%0A%0A    p
@@ -2554,24 +2554,1276 @@
    %7D%0A    %7D%0A%0A
+    /**%0A     * Add an EditText listener to disable the Create button when the EditText is empty.%0A     */%0A    private void addTextListener() %7B%0A        nameField.addTextChangedListener(new TextWatcher() %7B%0A            @Override%0A            public void beforeTextChanged(CharSequence s, int start, int count, int after) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void onTextChanged(CharSequence s, int start, int before, int count) %7B%0A                activateCreateButtonWhenNameIsNonempty();%0A            %7D%0A%0A            @Override%0A            public void afterTextChanged(Editable s) %7B%0A%0A            %7D%0A        %7D);%0A    %7D%0A%0A    /**%0A     * Activate the Create button if and only if the Article's name is non-empty.%0A     */%0A    private void activateCreateButtonWhenNameIsNonempty() %7B%0A        if (nameIsEmpty()) %7B%0A            createButton.setEnabled(false);%0A        %7D else %7B%0A            createButton.setEnabled(true);%0A        %7D%0A    %7D%0A%0A    /**%0A     * @return True if the Article's name is non-empty; false otherwise.%0A     */%0A    private boolean nameIsEmpty() %7B return (getArticleName().length() == 0); %7D%0A%0A    /**%0A     * @return The name entered for the Article.%0A     */%0A    private String getArticleName() %7B return nameField.getText().toString(); %7D%0A%0A
     public v

Create Article only is name is non-empty