package rs.elfak.mosis.marko.discounthunt;

import android.content.Context;
import android.location.LocationManager;
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;
    private CurrentLocation mCurrentLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_discount);
        mCurrentLocation = new CurrentLocation((LocationManager) getSystemService(Context.LOCATION_SERVICE));
        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();
            JSONObject userJsonObject = DiscountHunt.currentSession.getJSONObject("user");
            discountJsonObject.put("user_id", userJsonObject.getInt("id"));
            discountJsonObject.put("title", mTitle.getText().toString());
            discountJsonObject.put("description", mDescription.getText().toString());
            discountJsonObject.put("price", mPrice.getText().toString());
            discountJsonObject.put("category", mCategory.getSelectedItem().toString());
            discountJsonObject.put("location_attributes", mCurrentLocation.toJSONObject());

            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();
    }
}

@@ -65,24 +65,136 @@
 nt.Context;%0A
+import android.content.Intent;%0Aimport android.graphics.Bitmap;%0Aimport android.graphics.drawable.BitmapDrawable;%0A
 import andro
@@ -218,24 +218,24 @@
 ionManager;%0A
-
 import andro
@@ -417,24 +417,59 @@
 t.EditText;%0A
+import android.widget.ImageButton;%0A
 import andro
@@ -953,16 +953,111 @@
 ocation;
+%0A    private Camera mCamera;%0A    private ImageButton mTakePhoto;%0A    private String mPhotoData;
 %0A%0A    @O
@@ -1216,32 +1216,92 @@
 eate_discount);%0A
+        initCategory();%0A        mCamera = new Camera(this);%0A
         mCurrent
@@ -1641,32 +1641,8 @@
 e);%0A
-        initCategory();%0A
     
@@ -1831,16 +1831,276 @@
     %7D);%0A
+        mTakePhoto = (ImageButton) findViewById(R.id.take_photo);%0A        mTakePhoto.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                mCamera.takePhoto();%0A            %7D%0A        %7D);%0A
     %7D%0A%0A 
@@ -3176,17 +3176,146 @@
 ect());%0A
+            if(mPhotoData != null) %7B%0A                discountJsonObject.put(%22photo_attributes%22, photoAttributes());%0A            %7D
 %0A
-
         
@@ -4182,16 +4182,16 @@
 _LONG);%0A
-
         
@@ -4209,11 +4209,735 @@
 );%0A    %7D
+%0A%0A    private JSONObject photoAttributes() %7B%0A        JSONObject jsonObject = new JSONObject();%0A        try %7B%0A            jsonObject.put(%22data%22, mPhotoData);%0A            jsonObject.put(%22file_type%22, %22jpg%22);%0A        %7Dcatch (JSONException ex) %7B%7D%0A        return jsonObject;%0A    %7D%0A%0A    @Override%0A    protected void onActivityResult(int requestCode, int resultCode, Intent data) %7B%0A        if(requestCode == Camera.CAPTURE_IMAGE_REQUEST_CODE)%7B%0A            if(resultCode == RESULT_OK)%7B%0A                Bitmap bitmap = (Bitmap) data.getExtras().get(%22data%22); ;%0A                mTakePhoto.setBackground(new BitmapDrawable(getResources(),bitmap));%0A                mPhotoData = Camera.encodeToBase64(bitmap);%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Save discount photo