package com.pictc.converts.date;

import java.time.LocalTime;

import com.pictc.converts.ConvertException;
import com.pictc.converts.TypeHandler;
import com.pictc.utils.DateUtils;

public class LocalTimeHandler implements TypeHandler<LocalTime>{
	
	private Class<?>[] converts=new Class<?>[] {LocalTime.class};

	@Override
	public boolean isType(Class<?> type) {
		return LocalTime.class.equals(type);
	}

	@Override
	public Class<?>[] getConverts() {
		return converts;
	}

	@Override
	public Object convert(LocalTime value) {
		if(value==null) {
			return null;
		}
		return DateUtils.format(value);
	}

	@Override
	public LocalTime valueToObject(Object val) {
		if(val==null) {
			return null;
		}
		Class<?> vcs=val.getClass();
		if(isType(vcs)) {
			return LocalTime.class.cast(val);
		}else {
			try {
				return DateUtils.toLocalTime(val.toString());
			} catch (Exception e) {
				throw new ConvertException(vcs,val,LocalTime.class);
			}
		}
	}

	

}
