package rs.elfak.mosis.marko.discounthunt;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;

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

import rs.elfak.mosis.marko.discounthunt.api.endpoints.DiscountEndpoint;

public class CreateDiscountActivity extends AppCompatActivity {

    private Button mCreateButton;
    private EditText mTitle, mDescription, mPrice;
    private Spinner mCategory;

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

        mCreateButton = (Button) findViewById(R.id.create_discount);
        mTitle = (EditText) findViewById(R.id.title);
        mDescription = (EditText) findViewById(R.id.description);
        mPrice = (EditText) findViewById(R.id.price);
        initCategory();
        mCreateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createDiscount();
            }
        });
    }

    private void initCategory() {
        mCategory = (Spinner) findViewById(R.id.category);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.categories, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mCategory.setAdapter(adapter);
    }
    private void createDiscount() {
        try {
            JSONObject discountJsonObject = new JSONObject();
            discountJsonObject.put("title", mTitle.getText().toString());
            discountJsonObject.put("description", mDescription.getText().toString());
            discountJsonObject.put("price", mPrice.getText().toString());
            discountJsonObject.put("category", mCategory.getSelectedItem().toString());
            JSONObject userJsonObject = DiscountHunt.currentSession.getJSONObject("user");
            discountJsonObject.put("user_id", userJsonObject.getInt("id"));

            DiscountEndpoint discountEndpoint = new DiscountEndpoint();
            discountEndpoint.post(discountJsonObject,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            finish();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            showError();
                        }
                    }
            );
        }catch (JSONException ex) {
            showError();
        }
    }

    private void showError() {
        Toast toast = Toast.makeText(getApplicationContext(),
                R.string.error_create_discount, Toast.LENGTH_LONG);
        toast.show();
    }
}

@@ -33,24 +33,97 @@
 counthunt;%0A%0A
+import android.content.Context;%0Aimport android.location.LocationManager;%0A
 import andro
@@ -159,16 +159,16 @@
 tivity;%0A
-
 import a
@@ -760,16 +760,62 @@
 ategory;
+%0A    private CurrentLocation mCurrentLocation;
 %0A%0A    @O
@@ -982,17 +982,126 @@
 count);%0A
+        mCurrentLocation = new CurrentLocation((LocationManager) getSystemService(Context.LOCATION_SERVICE));
 %0A
-
         
@@ -1940,24 +1940,25 @@
 ter);%0A    %7D%0A
+%0A
     private 
@@ -2053,24 +2053,191 @@
 ONObject();%0A
+            JSONObject userJsonObject = DiscountHunt.currentSession.getJSONObject(%22user%22);%0A            discountJsonObject.put(%22user_id%22, userJsonObject.getInt(%22id%22));%0A
             
@@ -2562,159 +2562,84 @@
     
-JSONObject userJsonObject = DiscountHunt.currentSession.getJSONObject(%22user%22);%0A            discountJsonObject.put(%22user_id%22, userJsonObject.getInt(%22id%22
+discountJsonObject.put(%22location_attributes%22, mCurrentLocation.toJSONObject(
 ));%0A

Save current location with the discount