package es.craftsmanship.toledo.katangapp.activities;

import es.craftsmanship.toledo.katangapp.db.FavoriteDAO;
import es.craftsmanship.toledo.katangapp.db.model.Favorite;
import es.craftsmanship.toledo.katangapp.models.BusStop;

import android.content.Intent;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.view.View;

/**
 * @author Manuel de la Peña
 */
public class BusStopActivity extends AppCompatActivity {

    private BusStop busStop;
    private boolean isFavorite;
    private FavoriteDAO favoriteDAO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_bus_stop);

        Intent intent = getIntent();

        if (intent.hasExtra("busStop") && (intent.getSerializableExtra("busStop") != null)) {
            busStop = (BusStop) intent.getSerializableExtra("busStop");

            favoriteDAO = new FavoriteDAO(this);

            favoriteDAO.open();

            String title = busStop.getId() + " (" + busStop.getAddress() + ")";

            this.setTitle(title);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

            setSupportActionBar(toolbar);

            Favorite favorite = favoriteDAO.getFavorite(busStop.getId());

            isFavorite = (favorite != null);

            final FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(
                R.id.fab);

            floatingActionButton.setImageDrawable(getFavoritedDrawable(isFavorite));

            floatingActionButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    String message = "Parada AÑADIDA a favoritas con éxito";

                    if (!isFavorite) {
                        favoriteDAO.createFavorite(busStop.getId());
                    }
                    else {
                        message = "Parada ELIMINADA de favoritas con éxito";

                        Favorite favorite = new Favorite();

                        favorite.setBusStopId(busStop.getId());

                        favoriteDAO.deleteFavorite(favorite);
                    }

                    isFavorite = !isFavorite;

                    floatingActionButton.setImageDrawable(getFavoritedDrawable(isFavorite));

                    Snackbar.make(
                        view, message, Snackbar.LENGTH_LONG).setAction("Action", null).show();
                }

            });

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    private Drawable getFavoritedDrawable(boolean favorited) {
        if (favorited) {
            return ContextCompat.getDrawable(BusStopActivity.this, android.R.drawable.ic_delete);
        }

        return ContextCompat.getDrawable(BusStopActivity.this, R.drawable.ic_star_favorite_24dp);
    }

}
@@ -759,45 +759,8 @@
 ite;
-%0A    private FavoriteDAO favoriteDAO;
 %0A%0A  
@@ -1135,91 +1135,8 @@
 );%0A%0A
-            favoriteDAO = new FavoriteDAO(this);%0A%0A            favoriteDAO.open();%0A%0A
     
@@ -1351,32 +1351,141 @@
 nBar(toolbar);%0A%0A
+            try (FavoriteDAO favoriteDAO = new FavoriteDAO(this)) %7B%0A                favoriteDAO.open();%0A%0A    
             Favo
@@ -1547,32 +1547,36 @@
 );%0A%0A            
+   
+ 
 isFavorite = (fa
@@ -1591,16 +1591,30 @@
 = null);
+%0A            %7D
 %0A%0A      
@@ -1668,16 +1668,32 @@
 Button =
+%0A               
  (Floati
@@ -1721,33 +1721,16 @@
 iewById(
-%0A                
 R.id.fab
@@ -2046,32 +2046,173 @@
 as con %C3%A9xito%22;%0A%0A
+                    try (FavoriteDAO favoriteDAO = new FavoriteDAO(BusStopActivity.this)) %7B%0A                        favoriteDAO.open();%0A%0A    
                 
@@ -2230,24 +2230,28 @@
 Favorite) %7B%0A
+    
             
@@ -2328,40 +2328,24 @@
     
-   %7D%0A
        
-            
+%7D
  else %7B%0A
     
@@ -2340,16 +2340,20 @@
  else %7B%0A
+    
         
@@ -2438,32 +2438,36 @@
                 
+    
 Favorite favorit
@@ -2503,32 +2503,36 @@
                 
+   
+ 
 favorite.setBusS
@@ -2548,32 +2548,36 @@
 Stop.getId());%0A%0A
+    
                 
@@ -2634,35 +2634,42 @@
                 
-%7D%0A%0A
+    %7D%0A%0A   
                 
@@ -2664,32 +2664,33 @@
                 
+ 
 isFavorite = !is
@@ -2696,24 +2696,28 @@
 sFavorite;%0A%0A
+    
             
@@ -2818,16 +2818,20 @@
         
+    
 Snackbar
@@ -2861,16 +2861,20 @@
         
+   
+ 
 view, me
@@ -2936,16 +2936,38 @@
 show();%0A
+                    %7D%0A
         

Apply try-with-resources for DAO