package cz.gdgjihlava.parkovani.parkovani_v_jihlave.sms;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseListAdapter;
import com.google.firebase.database.*;
import cz.gdgjihlava.parkovani.parkovani_v_jihlave.R;


public class ParkingLotSelector {

    private static final String TAG = "ParkingLotSelector";
    private Spinner mSpinner;
    private ArrayAdapter<ParkingLot> parkingLots;

    public ParkingLotSelector(Spinner spinner, final Activity activity) {
        mSpinner = spinner;


        final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

        databaseReference.child("parking_lots").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {


                parkingLots = new ArrayAdapter<>(activity.getApplicationContext(), android.R.layout.simple_spinner_item);

                for (DataSnapshot parkingLotSnapshot: dataSnapshot.getChildren()) {
                    final String name = parkingLotSnapshot.child("name").getValue(String.class);
                    String zoneKey = parkingLotSnapshot.child("zone").getValue(String.class);

                    databaseReference.child("zones/" + zoneKey).addListenerForSingleValueEvent(new ValueEventListener
                        () {

                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            int zoneCode = dataSnapshot.child("code").getValue(Integer.class);
                            Log.d(TAG, Integer.toString(zoneCode));
                            int ticketDurationInMinutes = dataSnapshot.child("ticketDurationInMinutes")
                                .getValue(Integer.class);
                            int ticketPriceinCZK = dataSnapshot.child("ticketPriceInCZK").getValue(Integer.class);

                            Zone zone = new Zone(zoneCode, ticketDurationInMinutes, ticketPriceinCZK);
                            parkingLots.add(new ParkingLot(name, zone));
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });



                }

                parkingLots.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                mSpinner.setAdapter(parkingLots);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    public ParkingLot getSelectedParking() {
        ParkingLot parkingLotForReturn = (ParkingLot) mSpinner.getSelectedItem();
        Log.d(TAG, parkingLotForReturn.getName() + " - " + parkingLotForReturn.getZone().getCode());
        return parkingLotForReturn;
    }
}

@@ -563,35 +563,466 @@
 ing 
-TAG = %22ParkingLotSelector%22;
+ZONE_CODE_KEY = %22code%22;%0A    private static final String TAG = %22ParkingLotSelector%22;%0A    private static final String TICKET_DURATION_KEY = %22ticketDurationInMinutes%22;%0A    private static final String TICKET_PRICE_KEY = %22ticketPriceInCZK%22;%0A    private static final String PARKING_LOTS_KEY = %22parking_lots%22;%0A    private static final String NAME_KEY = %22name%22;%0A    private static final String ZONE_KEY = %22zone%22;%0A    private static final String ZONES_KEY = %22zones%22;%0A
 %0A   
@@ -1201,16 +1201,92 @@
 pinner;%0A
+        final Context applicationContext = activity.getApplicationContext();
 %0A%0A      
@@ -1322,22 +1322,16 @@
 abaseRef
-erence
  = Fireb
@@ -1396,35 +1396,31 @@
 eRef
-erence
 .child(
-%22parking_lots%22
+PARKING_LOTS_KEY
 ).ad
@@ -1606,28 +1606,16 @@
 pter%3C%3E(a
-ctivity.getA
 pplicati
@@ -1623,18 +1623,16 @@
 nContext
-()
 , androi
@@ -1717,16 +1717,17 @@
 Snapshot
+ 
 : dataSn
@@ -1815,22 +1815,24 @@
 t.child(
-%22name%22
+NAME_KEY
 ).getVal
@@ -1880,19 +1880,18 @@
 ing zone
-Key
+ID
  = parki
@@ -1910,22 +1910,24 @@
 t.child(
-%22zone%22
+ZONE_KEY
 ).getVal
@@ -1980,27 +1980,28 @@
 eRef
-erence
 .child(
-%22zones
+ZONES_KEY + %22
 /%22 +
@@ -2009,11 +2009,10 @@
 zone
-Key
+ID
 ).ad
@@ -2267,22 +2267,29 @@
 t.child(
-%22code%22
+ZONE_CODE_KEY
 ).getVal
@@ -2339,184 +2339,77 @@
     
-Log.d(TAG, Integer.toString(zoneCode));%0A                            int ticketDurationInMinutes = dataSnapshot.child(%22ticketDurationInMinutes%22)%0A                                
+int ticketDurationInMinutes = dataSnapshot.child(TICKET_DURATION_KEY)
 .get
@@ -2469,25 +2469,25 @@
  ticketPrice
-i
+I
 nCZK = dataS
@@ -2500,34 +2500,32 @@
 t.child(
-%22ticketPriceInCZK%22
+TICKET_PRICE_KEY
 ).getVal
@@ -2639,17 +2639,17 @@
 ketPrice
-i
+I
 nCZK);%0A 
@@ -2911,19 +2911,16 @@
     %7D);%0A
-%0A%0A%0A
         

Refactor ParkingLotSelector.java