/*
 * Copyright 2015 Adriano Marini, Carson McLean, Conner Dunn, Daniel Haberstock, Garry Bullock
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ca.ualberta.cs.swapmyride.View;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import ca.ualberta.cs.swapmyride.Controller.LocalDataManager;
import ca.ualberta.cs.swapmyride.Model.Photo;
import ca.ualberta.cs.swapmyride.Model.User;
import ca.ualberta.cs.swapmyride.Model.Vehicle;
import ca.ualberta.cs.swapmyride.R;
import ca.ualberta.cs.swapmyride.Controller.UserController;

/**
 * ViewVehicle activity provides the functionality for the app
 * to display all relevant information about a vehicle that is
 * selected.
 */

public class ViewVehicleActivity extends AppCompatActivity {

    Toolbar toolbar;
    TextView title;
    TextView quantity;
    TextView category;
    TextView quality;
    EditText comments;
    ImageView image;
    TextView location;
    TextView lat;
    TextView longit;

    Gson gson;
    //ImageView picture;
    LinearLayout gallery;

    Button delete;
    Button editVehicle;

    int position;
    LocalDataManager dm = new LocalDataManager(ViewVehicleActivity.this);
    UserController uController;

    /**
     * Using the dataManager functionality, load details of the vehicle
     * and display all relevant information about it.
     *
     * The user can select to delete an item from this view, which they
     * would be asked to confirm.
     *
     * @param savedInstanceState
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewvehicle);
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        uController = new UserController(getApplicationContext());

        title = (TextView) findViewById(R.id.title);
        quantity = (TextView) findViewById(R.id.quality);
        category = (TextView) findViewById(R.id.category);
        quality = (TextView) findViewById(R.id.quantity);
        comments = (EditText) findViewById(R.id.commentsHead);
        //image = (ImageView) findViewById(R.id.picture);
        //picture = (ImageView) findViewById(R.id.picture);
        location = (TextView) findViewById(R.id.location);
        lat = (TextView) findViewById(R.id.Latitude);
        longit = (TextView) findViewById(R.id.Longitude);
        gallery = (LinearLayout) findViewById(R.id.viewvehiclegallery);

        delete = (Button) findViewById(R.id.delete);
        editVehicle = (Button) findViewById(R.id.edit);

        initOnClickListeners();

        Vehicle vehicle;
        position = getIntent().getIntExtra("vehiclePosition", 0);

        vehicle = uController.getUserInventoryItems().get(position);

        title.setText(vehicle.getName());
        quantity.setText(vehicle.getQuantity().toString());
        category.setText(vehicle.getCategory().getCategory());
        quality.setText(vehicle.getQuality().getQuality());
        comments.setText(vehicle.getComments());
//        try {
//            image.setBackground(new BitmapDrawable(getApplicationContext().getResources(), vehicle.getPhoto().getImage()));
//        }catch (NullPointerException e){
//            image.setBackground(new BitmapDrawable(getApplicationContext().getResources(), new Photo(getApplicationContext()).getImage()));
//        }
//        try {
//            picture.setImageBitmap(vehicle.getPhoto().getImage());
//        }catch (NullPointerException e){
//            image.setImageBitmap(new Photo(getApplicationContext()).getImage());
//        }
        gallery.removeAllViews();
        for (Photo _photo : vehicle.getPhotoArrayList()) {
            ImageView newImage = new ImageView(getApplicationContext());
            newImage.setBackground(new BitmapDrawable(getResources(), _photo.getImage()));
            gallery.addView(newImage);
        }
        try {
            location.setText(vehicle.getLocation().getPostalCode() + ",  " + vehicle.getLocation().getLocality());
            lat.setText(new java.text.DecimalFormat("0.0000").format(vehicle.getLocation().getLatitude()));
            longit.setText(new java.text.DecimalFormat("0.0000").format(vehicle.getLocation().getLongitude()));
        }catch (Exception e){
            location.setText("Location Error");
            lat.setText("0");
            longit.setText("0");
        }
    }

    public void initOnClickListeners(){
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteDialog();
            }
        });
        editVehicle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ViewVehicleActivity.this, AddInventoryActivity.class);
                intent.putExtra("vehiclePosition", position);
                startActivity(intent);
                finish();
            }
        });

    }

    /**
     * Ask the user for confirmation if they would like to delete a vehicle.
     */

    public void deleteDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(ViewVehicleActivity.this);
        builder.setMessage("Are you SURE you want to delete this vehicle? It is a permanent Action!");
        builder.setCancelable(false);
        builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                User user = uController.getCurrentUser();
                Vehicle toDelete = user.getInventory().getList().get(position);
                user.getInventory().delete(toDelete);
                dm.saveUser(user);
                finish();
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    public TextView getTheName(){
        return (TextView) findViewById(R.id.title);
    }

    public Button getEditButton(){
        return (Button) findViewById(R.id.edit);
    }
}

@@ -1184,16 +1184,73 @@
 .Gson;%0A%0A
+import ca.ualberta.cs.swapmyride.Controller.DataManager;%0A
 import c
@@ -1885,29 +1885,8 @@
 ts;%0A
-    ImageView image;%0A
     
@@ -1900,24 +1900,24 @@
 w location;%0A
+
     TextView
@@ -1948,23 +1948,8 @@
 t;%0A%0A
-    Gson gson;%0A
     
@@ -2062,21 +2062,16 @@
 on;%0A    
-Local
 DataMana
@@ -2083,21 +2083,16 @@
 m = new 
-Local
 DataMana
@@ -5991,17 +5991,16 @@
     %7D);%0A
-%0A
     %7D%0A%0A 
@@ -6087,25 +6087,24 @@
 le.%0A     */%0A
-%0A
     public v

Update ViewVehicleActivity to use new DataManager