package com.pinnackl.shoppinglist.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.pinnackl.shoppinglist.HttpRequest;
import com.pinnackl.shoppinglist.IHttpRequestListener;
import com.pinnackl.shoppinglist.R;
import com.pinnackl.shoppinglist.list.List;
import com.pinnackl.shoppinglist.list.ListFactory;
import com.pinnackl.shoppinglist.list.ListUtil;
import com.pinnackl.shoppinglist.request.Request;
import com.pinnackl.shoppinglist.request.RequestFactory;
import com.pinnackl.shoppinglist.user.UserUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class AddListActivity extends AppCompatActivity {
    private EditText mListNameView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_list);
        mListNameView = (EditText) findViewById(R.id.listName);


        Button createListButton = (Button) findViewById(R.id.button);
        createListButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createList();
            }
        });
    }

    public boolean createList() {
        String listName = mListNameView.getText().toString();
        View focusView = null;
        boolean cancel = false;

        if (TextUtils.isEmpty(listName)) {
            mListNameView.setError(getString(R.string.error_field_required));
            focusView = mListNameView;
            cancel = true;
        }

        if (cancel) {
            focusView.requestFocus();
        } else {
            Context context = getApplicationContext();

            HttpRequest request = new HttpRequest(context);
            request.setListener(new IHttpRequestListener() {
                @Override
                public void onFailure(String errorMessage) {
                    Toast toast = Toast.makeText(AddListActivity.this, "Error", Toast.LENGTH_SHORT);
                    toast.show();
                }

                @Override
                public void onSuccess(JSONObject result) {
                    try {
                        JSONObject oList = result.getJSONObject("result");
                        ListFactory factory = new ListFactory();
                        List list = factory.createList();
                        ListUtil util = new ListUtil();
                        util.save(oList, list);

                        Toast toast = Toast.makeText(AddListActivity.this, "List created", Toast.LENGTH_SHORT);
                        toast.show();
                        startActivity(new Intent(AddListActivity.this, ProductActivity.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            // get token in shared preferences
            UserUtil userUtil = new UserUtil();
            String token = userUtil.getToken(context);

            RequestFactory requestFactory = new RequestFactory();
            Request requestObject = requestFactory.createRequest();
            requestObject.setParameters("name", listName);
            requestObject.setParameters("token", token);
            requestObject.setEndpoint("/shopping_list/create.php");

            request.execute(requestObject);
        }

        return true;
    }
}

@@ -129,16 +129,59 @@
 Bundle;%0A
+import android.support.annotation.NonNull;%0A
 import a
@@ -252,16 +252,46 @@
 tUtils;%0A
+import android.view.MenuItem;%0A
 import a
@@ -1153,24 +1153,89 @@
 _add_list);%0A
+%0A        getSupportActionBar().setDisplayHomeAsUpEnabled(true);%0A%0A
         mLis
@@ -1287,17 +1287,16 @@
 Name);%0A%0A
-%0A
         
@@ -3773,11 +3773,253 @@
 e;%0A    %7D
+%0A%0A    @Override%0A    public boolean onOptionsItemSelected(@NonNull MenuItem item) %7B%0A        Intent intent = new Intent(AddListActivity.this, ProductActivity.class);%0A        AddListActivity.this.startActivity(intent);%0A        return true;%0A    %7D
 %0A%7D%0A

add back button to addlistactivity