package cc.smtweb.framework.core.mvc.controller.binder.param;

import cc.smtweb.framework.core.exception.BindParamException;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.util.Date;

public class TimeEditor extends AbstractParameterEditor {
    public TimeEditor(int header) {
        super(header);
    }

    @Override
    public Object parseText(Object value) {
        if (value == null) {
            return null;
        }

        if (value instanceof Date) {
            return new java.sql.Time(((Date) value).getTime());
        }

        String text = value.toString();
        if (StringUtils.isBlank(text)) {
            return null;
        }

        try {
            return new java.sql.Time(parseDateTime(text).getTime());
        } catch (ParseException e) {
            throw new BindParamException(e);
        }
    }
}
