package tools;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import dbfields.fieldDB;
import kz.istt.locust.R;
import kz.istt.locust.Tools;


public class OnClickListenerDateTime implements View.OnClickListener {

    private fieldDB edtDate=null;
    private Context context=null;

    public OnClickListenerDateTime(fieldDB edtDate, Context context){
        this.edtDate = edtDate;
        this.context = context;
    }

    @Override
    public void onClick(View view) {
        DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                int month = monthOfYear + 1;

                //Теперь выбираем время
                TimePickerDialog.OnTimeSetListener myTimeListener = new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        Date date = Tools.getDateFromStr(edtDate.getValue());
                        if(date != null) {
                            date = Tools.setHMS(date,hourOfDay,minute,0);
                            edtDate.setValue(Tools.dateToStr(date));
                        }
                    }
                };

                Date time = Tools.getDateFromStr(edtDate.getValue());
                if(time != null){

                    Calendar myCal = new GregorianCalendar();
                    myCal.setTime(time);
                    int hour = myCal.get(Calendar.HOUR_OF_DAY);
                    int minute = myCal.get(Calendar.MINUTE);
                    TimePickerDialog tpd = new TimePickerDialog(context, R.style.AlertDialogTheme, myTimeListener, hour, minute, true);
                    tpd.show();
                } else {
                    final Calendar myCal = Calendar.getInstance();
                    int hour = myCal.get(Calendar.HOUR_OF_DAY);
                    int minute = myCal.get(Calendar.MINUTE);
                    TimePickerDialog tpd = new TimePickerDialog(context, R.style.AlertDialogTheme, myTimeListener, hour, minute, true);
                    tpd.show();
                }

                time = Tools.setYMD(time,year,month,dayOfMonth);
                edtDate.setValue(Tools.dateToStr(time));
            }
        };

        Date date = Tools.getDateFromStr(edtDate.getValue());
        if(date != null){

            Calendar myCal = new GregorianCalendar();
            myCal.setTime(date);
            int day = myCal.get(Calendar.DAY_OF_MONTH);
            int month = myCal.get(Calendar.MONTH);
            int year = myCal.get(Calendar.YEAR);
            DatePickerDialog dpd = new DatePickerDialog(context, R.style.AlertDialogTheme, myDateListener, year, month, day);
            dpd.show();

        } else {
            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);

            DatePickerDialog dpd = new DatePickerDialog(context, R.style.AlertDialogTheme, myDateListener, year, month, day);
            dpd.show();
        }
    }
}
