/*
   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 edu.pdx.its.portal.routelandia;

import java.util.Calendar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;


public class DatePickUp extends Activity {
    private TextView tvDisplayDay;
    private TimePicker thisTimePicker;
    private Button btnDepartureDate;
    private int hour;
    private int minute;
    private int am_pm;
    static final int TIME_DIALOG_ID = 100;
    public String dayOfWeek;
    private Spinner weekDaySpinner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_pick_up);

        setCurrentTimeOnView();
        addListenerOnButton();
        addListenerOnWeekDaySpinnerSelection();
        addListenerOnTime();
    }

    private void addListenerOnWeekDaySpinnerSelection() {
        weekDaySpinner = (Spinner) findViewById(R.id.spinner);
        weekDaySpinner.setOnItemSelectedListener(new DayPickSelectedListener());
    }

    private void addListenerOnTime(){
        thisTimePicker = (TimePicker) findViewById(R.id.timePicker);
        thisTimePicker.setOnTimeChangedListener(new TimePickSelectedListener());
    }

    //Display current time
    public void setCurrentTimeOnView() {

        tvDisplayDay = (TextView) findViewById(R.id.tvTime);
        thisTimePicker = (TimePicker) findViewById(R.id.timePicker);

        final Calendar c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);
        am_pm = c.get(Calendar.AM_PM);
        dayOfWeek = getPmAm(c.get(Calendar.AM_PM));

        tvDisplayDay.setText(
                new StringBuilder().append(hour)
                        .append(":").append(minute));

        thisTimePicker.setCurrentHour(hour);
        thisTimePicker.setCurrentMinute(minute);
    }

    public void addListenerOnButton() {

        btnDepartureDate = (Button) findViewById(R.id.btnDepartureDate);
        btnDepartureDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(
                        DatePickUp.this,
                        ListStat.class);
                startActivity(i);
            }
        });
    }

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

    private String getPmAm(int value) {
        String pmAm = "";
        switch (value) {
            case 1:
                if (am_pm == 1) {
                    pmAm = "pm";
                }
                break;
            case 2:
                if (am_pm == 0) {
                    pmAm = "am";
                }
                break;
        }
        return pmAm;
    }
}
@@ -954,16 +954,68 @@
 ker;%0D%0A%0D%0A
+import com.google.android.gms.maps.model.LatLng;%0D%0A%0D%0A
 %0D%0Apublic
@@ -1286,38 +1286,8 @@
 0;%0D%0A
-    public String dayOfWeek;%0D%0A
     
@@ -1319,16 +1319,159 @@
 inner;%0D%0A
+    public String weekDay;// = %22Sunday%22;%0D%0A    protected LatLng startPoint;%0D%0A    protected LatLng endPoint;%0D%0A    protected String departureTime;
 %0D%0A    @O
@@ -1632,24 +1632,29 @@
 _pick_up);%0D%0A
+     
 %0D%0A        se
@@ -1707,32 +1707,42 @@
 tton();%0D%0A       
+ weekDay =
  addListenerOnWe
@@ -1766,32 +1766,48 @@
 tion();%0D%0A       
+ departureTime =
  addListenerOnTi
@@ -1809,24 +1809,445 @@
 rOnTime();%0D%0A
+        %0D%0A        if(getIntent().getExtras() != null) %7B%0D%0A            startPoint = new LatLng(getIntent().getExtras().getDouble(%22lat of first point%22), %0D%0A                    getIntent().getExtras().getDouble(%22lng of first point%22));%0D%0A%0D%0A            endPoint = new LatLng(getIntent().getExtras().getDouble(%22lat of second point%22),%0D%0A                    getIntent().getExtras().getDouble(%22lng of second point%22));%0D%0A%0D%0A        %7D%0D%0A%0D%0A
     %7D%0D%0A%0D%0A   
@@ -2247,36 +2247,38 @@
 %0D%0A%0D%0A    private 
-void
+String
  addListenerOnWe
@@ -2380,54 +2380,182 @@
     
-weekDaySpinner.setOnItemSelectedListener(new D
+DayPickSelectedListener dayPickSelectedListener = new DayPickSelectedListener();%0D%0A        weekDaySpinner.setOnItemSelectedListener(dayPickSelectedListener);%0D%0A        return d
 ayPi
@@ -2564,34 +2564,44 @@
 SelectedListener
+.getWeekDay
 (
-)
 );%0D%0A    %7D%0D%0A%0D%0A   
@@ -2609,20 +2609,22 @@
 private 
-void
+String
  addList
@@ -2720,53 +2720,185 @@
     
-thisTimePicker.setOnTimeChangedListener(new T
+TimePickSelectedListener timePickSelectedListener = new TimePickSelectedListener();%0D%0A        thisTimePicker.setOnTimeChangedListener(timePickSelectedListener);%0D%0A        return t
 imeP
@@ -2916,18 +2916,34 @@
 Listener
+.getDepartureTime
 (
-)
 );%0D%0A    
@@ -3328,24 +3328,26 @@
 ar.AM_PM);%0D%0A
+//
         dayO
@@ -3906,98 +3906,208 @@
     
-Intent i = new Intent(%0D%0A                        DatePickUp.this,%0D%0A                        
+HttpAsyncTask httpAsyncTask = new HttpAsyncTask(startPoint, endPoint, departureTime, weekDay);%0D%0A                httpAsyncTask.execute();%0D%0A                Intent i = new Intent(getApplicationContext(),
 List

add field for first and last point