import { findComponentsByKeyValue, getComponentViewBindingId } from "./helpers"


export const generateDatePickerDialogInteractions = (fragment) => {
    if (findComponentsByKeyValue(fragment, "name", "DatePickerDialog").length == 0) return "";
    let dialogs = findComponentsByKeyValue(fragment, "name", "DatePickerDialog");

    return dialogs.map((view) => ` 
    binding.${getComponentViewBindingId(view)}.setOnClickListener {
        val c = Calendar.getInstance()
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH)
        val day = c.get(Calendar.DAY_OF_MONTH)
        val dpd = DatePickerDialog(requireContext(), DatePickerDialog.OnDateSetListener { 
            view, 
            year, 
            monthOfYear, 
            dayOfMonth ->
            // Display Selected date in textbox
            //lblDate.setText("" + dayOfMonth + " " + MONTHS[monthOfYear] + ", " + year)
        }, year, month, day)
            dpd.show()
    }
    `).join("\n");
}

export const generateDatePickerDialogImports = (fragment) => {
    if (findComponentsByKeyValue(fragment, "name", "DatePickerDialog").length > 0) return `import android.app.DatePickerDialog`
    else return ''
}

export const generateTimePickerDialogInteractions = (fragment) => {
    if (findComponentsByKeyValue(fragment, "name", "TimePickerDialog")?.length == 0) return "";
    let dialogs = findComponentsByKeyValue(fragment, "name", "TimePickerDialog");


    return dialogs.map((view) => ` 
    binding.${getComponentViewBindingId(view)}.setOnClickListener {
    val cal = Calendar.getInstance()
    val timeSetListener = TimePickerDialog.OnTimeSetListener { timePicker, hour, minute ->
        cal.set(Calendar.HOUR_OF_DAY, hour)
        cal.set(Calendar.MINUTE, minute)
    }
    TimePickerDialog(requireContext(), timeSetListener, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true).show()
    }
    `).join("\n");
}

export const generateTimePickerDialogImports = (fragment) => {
    if (findComponentsByKeyValue(fragment, "name", "TimePickerDialog")?.length > 0) return `import android.app.TimePickerDialog`
    else return ''
} 