package com.brframework.commoncms.core.layout.column;

import com.brframework.commoncms.annatotion.column.FileColumn;
import com.brframework.commoncms.annatotion.column.ImageColumn;
import com.brframework.commoncms.core.layout.AbstractPanelLayoutColumn;
import lombok.Data;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 文件
 * @author xu
 * @date 2018/9/26 14:08
 * @see com.brframework.commoncms.annatotion.column.FileColumn
 */
@Data
public class LayoutFileColumn extends AbstractPanelLayoutColumn {

    private static final String TYPE_DEF = "FILE";
    private String key;
    private String title;

    /** 文件上传数量限制 */
    private int number;

    /**
     * 文件上传文件格式限制，多个使用英文逗号(,)隔开
     * jpg,png,svg
     */
    private String format;

    /**
     * 上传文件大小限制单位字节
     */
    private long size;

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public String getType() {
        return TYPE_DEF;
    }

    @Override
    public void create(Field columnField) throws Throwable {
        init(columnField);

        FileColumn file = AnnotationUtils.findAnnotation(columnField, FileColumn.class);
        assert file != null;
        number = file.number();
        format = file.format();
        String sizeString = file.size();
        long _size = Integer.parseInt(sizeString.substring(0, sizeString.length() - 1));
        String unit = sizeString.substring(sizeString.length() - 1);
        switch (unit){
            case "K":
                size = _size * 1024;
                break;
            case "M":
                size = _size * 1024 * 1024;
                break;
            case "G":
                size = _size * 1024 * 1024 * 1024;
                break;
        }

    }

    @Override
    public List<Class<?>> supportType() {
        return null;
    }
}
