package com.example.bb;

import java.util.Calendar;
import java.util.Comparator;

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.DatePicker;

public class Reservation2 extends Activity {
    private static String TAG = "B&B Reservation";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_two);

        final DatePicker checkin =
                (DatePicker) findViewById(R.id.checkin);
        final DatePicker checkout =
                (DatePicker) findViewById(R.id.checkout);

        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        checkin.init(year, month, day,
                new DatePicker.OnDateChangedListener() {
                    @Override
                    public void
                            onDateChanged(DatePicker view, int year,
                                    int monthOfYear, int dayOfMonth) {
                        DatePickerCompare dpc =
                                new DatePickerCompare();

                        // if check in is >= to checkout
                        if (dpc.compare(checkin, checkout) >= 0) {
                            checkout.updateDate(year, monthOfYear,
                                    dayOfMonth);
                        }
                    }
                });

        Button next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {

                Log.v(TAG,
                        "Checkin: " + checkin.getMonth() + " "
                                + checkin.getDayOfMonth() + ", "
                                + checkin.getYear());
                Log.v(TAG,
                        "Checkout: " + checkout.getMonth() + " "
                                + checkout.getDayOfMonth() + ", "
                                + checkout.getYear());

                Intent i = new Intent();
                i.setClass(Reservation2.this, Reservation3.class);
                startActivity(i);
            }
        });
    }

    class DatePickerCompare implements Comparator<DatePicker> {
        @Override
        public int compare(DatePicker dp1, DatePicker dp2) {
            if (dp1 == dp2) {
                return 0;
            }

            if (dp1.getYear() < dp2.getYear()) {
                return -1;
            }
            if ((dp1.getYear() == dp2.getYear())
                    && (dp1.getMonth() < dp2.getMonth())) {
                return -1;
            }
            if ((dp1.getYear() == dp2.getYear())
                    && (dp1.getMonth() == dp2.getMonth())
                    && (dp1.getDayOfMonth() < dp2.getDayOfMonth())) {
                return -1;
            }

            if (dp1.getYear() > dp2.getYear()) {
                return 1;
            }
            if ((dp1.getYear() == dp2.getYear())
                    && (dp1.getMonth() > dp2.getMonth())) {
                return 1;
            }
            if ((dp1.getYear() == dp2.getYear())
                    && (dp1.getMonth() == dp2.getMonth())
                    && (dp1.getDayOfMonth() > dp2.getDayOfMonth())) {
                return 1;
            }
            return 0;
        }
    }
}