package com.qmth.teachcloud.common.util.excel.convertor.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;

import com.qmth.teachcloud.common.util.excel.convertor.ValueConvertor;
import com.qmth.teachcloud.common.util.excel.model.FieldParam;

public class LocalDateTimeConvertor implements ValueConvertor<LocalDateTime> {

    public static final LocalDateTimeConvertor instance = new LocalDateTimeConvertor();
    String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
    @Override
    public LocalDateTime parse(String value, FieldParam param) throws Exception {
        String pattern = param.getAnnotation().pattern();
        if (StringUtils.isBlank(pattern)) {
            pattern = DEFAULT_DATE_PATTERN;
        }
        return LocalDateTime.parse(value, DateTimeFormatter.ofPattern(pattern));
    }

    @Override
    public void format(LocalDateTime object, FieldParam param, Cell cell) throws Exception {
        String pattern = param.getAnnotation().pattern();
        if (StringUtils.isBlank(pattern)) {
            pattern = DEFAULT_DATE_PATTERN;
        }
        cell.setCellValue(object.format(DateTimeFormatter.ofPattern(pattern)));
    }
}
