package com.pictc.converts.date;

import java.time.LocalDateTime;

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

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

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

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

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

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

	

}
