/*

// This is very dirty datepicker for Java/Android created just because there
// is no standard DatePicker provided for Android which could be fit into a
// single line. This implementation is just a single line TextView which  allows
// on click to show the standard DatePicker dialog.
//---------------------------------------------------------------------------
// (C) by Vladimir Kladov, 2019

CLASS {AL4_datepicker|_Java_Android}, NATIVE, UNTESTED :
      IMPORT: {Control}, {Date_time};

*/
import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;

import android.widget.*;
import android.app.DatePickerDialog;
import android.view.*;

import java.util.concurrent.*;

public class AL4_datepicker extends TextView
{
	public Calendar m_date;
	
    public AL4_datepicker() throws Exception {
    	super(AL4_form.context_activity);
    	
    	m_date = Calendar.getInstance();
    	setText(formatted_date());
    	
    	setOnClickListener(new View.OnClickListener() {
    		public void onClick(View v) {
    				AL4_system.WriteLine("CLICK ON DATEPICKER!");
    				/////////////////////////////////////////////
    			int _year = m_date.get(Calendar.YEAR);
    			int _month = m_date.get(Calendar.MONTH);
    			int _dayOfMonth = m_date.get(Calendar.DAY_OF_MONTH);
    			DatePickerDialog ddialog = new DatePickerDialog(
    				AL4_form.context_activity,
    				new DatePickerDialog.OnDateSetListener(){
    						@Override
    						public void onDateSet(DatePicker datePicker,
    							int year, int month, int day) {
    							m_date.setTime(new Date(year-1900, month, day));
    							setText(formatted_date());
    						}
    				}, _year, _month, _dayOfMonth);
    			ddialog.show();
    		}
    	});
    }
    
    public String formatted_date() {
    	return DateFormat.getDateInstance(DateFormat.MEDIUM).format(
    		m_date.getTime());
    }
    
	/*
	// Create date picker control, return descriptor
	//----------------------------------------------
	
	FUN Create_date_picker(
	{Control} D|atepicker_control) ==> INT, NATIVE :
		INT f|orm_handle = D.Form_.Handle
		NATIVE @
		"     RESULT = AL4_datepicker.Do_Create_datepicker(X_f, X_D);" .
	*/
	public static int Do_Create_datepicker(int H, Ref_obj X_D) 
                      throws Exception {
		AL4_datepicker DP = new AL4_datepicker();
		
		int Result = AL4_form.add_control_single_line(H, DP, X_D);
		AL4_form.Set_control_events(X_D, DP, false);
		return Result;
	}

	/*
	// Get date picker date
	//---------------------
	
	FUN Get_date_picker_date(
	INT D|atepicker_control) ==> {Date_time}.{date_time}
		:= Date_time(DT = do_get_date_picker_date(D)) .
		
	FUN do_get_date_picker_date(INT D|atepicker_control) ==> REAL, NATIVE : @
	"	AL4_datepicker DP = (AL4_datepicker)AL4_form.All_descr.get(X_D).Jvc;"
	"   RESULT = DP.m_date.getTimeInMillis();" .
	
	// Set date picker date
	//---------------------
	
	FUN Set_date_picker_date(
	INT D|atepicker_control, {date_time} Value|_date)
		:
		do_set_date_picker_date(D, Value.DT) .
	FUN do_set_date_picker_date(
	INT D|atepicker_control, REAL Value|_milliseconds), NATIVE : @
	"	AL4_datepicker DP = (AL4_datepicker)AL4_form.All_descr.get(X_D).Jvc;"
	"   DP.m_date.setTimeInMillis((long)X_Value);"
	"	DP.setText(DP.formatted_date());" .
	
	// Get date picker text
	//---------------------
	
	FUN Get_date_picker_text(INT D|atepicker_control) ==> STR,
		NATIVE : @
	"	AL4_datepicker DP = (AL4_datepicker)AL4_form.All_descr.get(X_D).Jvc;"
	"    RESULT = DP.getText().toString();" .
	*/	
	
	/*
	// Get date picker Ok
	//---------------------
	FUN Get_date_picker_OK(INT D|atepicker_control|dummy) ==> BOOL:
		RESULT = TRUE .
		//, NATIVE : @ " RESULT = AL4_form.do_Get_date_picker_OK(X_D);" .
		
	*/
	public static boolean do_Get_date_picker_OK(int X_D) {
        return AL4_form.get_property_boolean(new Callable<Boolean>() {
	        @Override public Boolean call() {
	     	   try {
		            AL4_datepicker D = (AL4_datepicker)AL4_form.All_descr.get(X_D).Jvc;
		            return true; // D.Ok;
	     	   }
	     	   catch (Exception ex2) { AL4_form.report(ex2); }
	      	   return false;
	        }
	    });
	}
	
}

/*
END
*/
