package com.pictc.converts.date;

import java.time.LocalDate;

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

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

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

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

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

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

	

}
