package cn.stylefeng.team.project.modular.filing.factory;

import cn.stylefeng.team.form.api.entity.FormItem;
import cn.stylefeng.team.form.api.entity.FormItemNumber;
import cn.stylefeng.team.form.api.enums.FormComponentEnum;
import cn.stylefeng.team.form.api.factory.FormItemFactory;
import cn.stylefeng.team.meta.api.entity.MetaField;
import cn.stylefeng.team.meta.api.entity.MetaModel;
import cn.stylefeng.team.meta.api.enums.MetaFieldTypeEnum;

public class FormDefinitionItemFactory {

    public static FormItem createFormItemByField(MetaModel thisSubModel, MetaField metaField) {
        FormItem inputItem = FormItemFactory.defaultFormItem();

        inputItem.setFieldCode(metaField.getFieldCode());
        inputItem.setItemLabelName(metaField.getDescription());

        inputItem.setItemColumnNumber(8);

        inputItem.setMetaModelCode(thisSubModel.getMetaModelCode());

        if (metaField.getFieldType().equals(MetaFieldTypeEnum.INT.getCode())) {
            inputItem.setComponentCode(FormComponentEnum.NUMBER.getComponentCode());

            FormItemNumber formItemNumber = new FormItemNumber();
            formItemNumber.setDecimalPlaces(0);
            inputItem.setFormItemNumber(formItemNumber);

        }

        else if (metaField.getFieldType().equals(MetaFieldTypeEnum.DECIMAL.getCode())) {
            inputItem.setComponentCode(FormComponentEnum.NUMBER.getComponentCode());

            FormItemNumber formItemNumber = new FormItemNumber();
            formItemNumber.setDecimalPlaces(2);
            inputItem.setFormItemNumber(formItemNumber);

        }

        else if (metaField.getFieldType().equals(MetaFieldTypeEnum.DATE_TIME.getCode())) {
            inputItem.setComponentCode(FormComponentEnum.DATETIME.getComponentCode());
        }

        else if (metaField.getFieldType().equals(MetaFieldTypeEnum.DATE.getCode())) {
            inputItem.setComponentCode(FormComponentEnum.DATE.getComponentCode());
        }

        else if (metaField.getFieldType().equals(MetaFieldTypeEnum.TIME.getCode())) {
            inputItem.setComponentCode(FormComponentEnum.TIME.getComponentCode());
        }

        else {
            inputItem.setComponentCode(FormComponentEnum.INPUT.getComponentCode());
        }

        return inputItem;
    }

}
