package com.arrebol.taxiservicios.beans.spa;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
/**
 *
 * @author Crov
 */
@FacesConverter("duracionConverter")
public class DuracionConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.isEmpty()) {
            return null;
        }
        
        double duracion = Double.parseDouble(value.replace(" h:min", "").trim());
        return duracion;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        }
        
        double duracion = (Double) value;
        int horas = (int) duracion;
        int minutos = (int) ((duracion - horas) * 60);

        return String.format("%d:%02d h:min", horas, minutos);
    }
}
