package cn.stylefeng.team.meta.modular.factory;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.team.meta.api.entity.MetaDefinition;
import cn.stylefeng.team.meta.api.entity.MetaField;
import cn.stylefeng.team.meta.api.enums.DefaultFieldTypeEnum;
import cn.stylefeng.team.meta.api.enums.MetaFieldTypeEnum;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class FieldFactory {

    public static List<MetaField> createMetaField(MetaDefinition metaDefinition, List<MetaField> metaFieldRequestList) {





        BigDecimal newBaseNumber = new BigDecimal(90);

        List<MetaField> metaFields = new ArrayList<>();
        for (MetaField metaFieldRequest : metaFieldRequestList) {
            MetaField metaField = new MetaField();

            BeanUtil.copyProperties(metaFieldRequest, metaField, CopyOptions.create().ignoreNullValue());

            metaField.setFieldId(null);

            metaField.setBusinessCategoryId(metaDefinition.getBusinessCategoryId());
            metaField.setBusinessId(metaDefinition.getBusinessId());
            metaField.setMetaModelId(metaDefinition.getMetaModelId());
            metaField.setMetaDefinitionId(metaDefinition.getMetaDefinitionId());

            newBaseNumber = newBaseNumber.add(new BigDecimal(10));
            metaField.setFldSort(newBaseNumber);

            if (metaField.getFieldCode().equals("id")) {
                metaField.setNotEmptyFlag(YesOrNotEnum.Y.getCode());
                metaField.setKeyFlag(YesOrNotEnum.Y.getCode());
            }

            metaFields.add(metaField);
        }

        return metaFields;
    }

    public static List<MetaField> createDefaultMetaField(Integer defaultFieldType) {

        if (DefaultFieldTypeEnum.VERSION_FLAG.getCode().equals(defaultFieldType)) {
            return createVersionFlag();
        } else if (DefaultFieldTypeEnum.DEL_FLAG.getCode().equals(defaultFieldType)) {
            return createDelFlag();
        } else if (DefaultFieldTypeEnum.CREATE_TIME.getCode().equals(defaultFieldType)) {
            return createTime();
        } else if (DefaultFieldTypeEnum.TENANT_ID.getCode().equals(defaultFieldType)) {
            return createTenant();
        }

        return new ArrayList<>();
    }

    private static List<MetaField> createTenant() {

        ArrayList<MetaField> metaFields = new ArrayList<>();

        MetaField tenantId = new MetaField();
        tenantId.setFieldCode("tenant_id");
        tenantId.setFieldType(MetaFieldTypeEnum.BIGINT.getCode());
        tenantId.setNotEmptyFlag(YesOrNotEnum.N.getCode());
        tenantId.setKeyFlag(YesOrNotEnum.N.getCode());
        tenantId.setDescription("租户id");
        metaFields.add(tenantId);

        return metaFields;
    }

    private static List<MetaField> createDelFlag() {
        ArrayList<MetaField> metaFields = new ArrayList<>();

        MetaField versionFlag = new MetaField();
        versionFlag.setFieldCode("del_flag");
        versionFlag.setFieldType(MetaFieldTypeEnum.FIXED_STR.getCode());
        versionFlag.setNotEmptyFlag(YesOrNotEnum.N.getCode());
        versionFlag.setKeyFlag(YesOrNotEnum.N.getCode());
        versionFlag.setDescription("逻辑删除标识：Y-已删除，N-未删除");
        metaFields.add(versionFlag);

        return metaFields;
    }

    private static List<MetaField> createVersionFlag() {
        ArrayList<MetaField> metaFields = new ArrayList<>();

        MetaField versionFlag = new MetaField();
        versionFlag.setFieldCode("version_flag");
        versionFlag.setFieldType(MetaFieldTypeEnum.BIGINT.getCode());
        versionFlag.setNotEmptyFlag(YesOrNotEnum.N.getCode());
        versionFlag.setKeyFlag(YesOrNotEnum.N.getCode());
        versionFlag.setDescription("乐观锁");
        metaFields.add(versionFlag);

        return metaFields;
    }

    private static ArrayList<MetaField> createTime() {
        ArrayList<MetaField> metaFields = new ArrayList<>();

        MetaField createTime = new MetaField();
        createTime.setFieldCode("create_time");
        createTime.setFieldType(MetaFieldTypeEnum.DATE_TIME.getCode());
        createTime.setNotEmptyFlag(YesOrNotEnum.N.getCode());
        createTime.setKeyFlag(YesOrNotEnum.N.getCode());
        createTime.setDescription("创建时间");
        metaFields.add(createTime);

        MetaField createUser = new MetaField();
        createUser.setFieldCode("create_user");
        createUser.setFieldType(MetaFieldTypeEnum.BIGINT.getCode());
        createUser.setNotEmptyFlag(YesOrNotEnum.N.getCode());
        createUser.setKeyFlag(YesOrNotEnum.N.getCode());
        createUser.setDescription("创建人");
        metaFields.add(createUser);

        MetaField updateTime = new MetaField();
        updateTime.setFieldCode("update_time");
        updateTime.setFieldType(MetaFieldTypeEnum.DATE_TIME.getCode());
        updateTime.setNotEmptyFlag(YesOrNotEnum.N.getCode());
        updateTime.setKeyFlag(YesOrNotEnum.N.getCode());
        updateTime.setDescription("更新时间");
        metaFields.add(updateTime);

        MetaField updateUser = new MetaField();
        updateUser.setFieldCode("update_user");
        updateUser.setFieldType(MetaFieldTypeEnum.BIGINT.getCode());
        updateUser.setNotEmptyFlag(YesOrNotEnum.N.getCode());
        updateUser.setKeyFlag(YesOrNotEnum.N.getCode());
        updateUser.setDescription("更新人");
        metaFields.add(updateUser);

        return metaFields;
    }

    public static void simpleSetSortField(List<MetaField> metaFieldList) {





        BigDecimal newBaseNumber = new BigDecimal(90);

        for (MetaField metaField : metaFieldList) {

            newBaseNumber = newBaseNumber.add(new BigDecimal(10));
            metaField.setFldSort(newBaseNumber);
        }
    }

    public static void fillVirtualFieldInfo(List<MetaField> metaFieldRequestList) {
        BigDecimal newBaseNumber = new BigDecimal(900);
        for (MetaField metaFieldRequest : metaFieldRequestList) {

            newBaseNumber = newBaseNumber.add(new BigDecimal(10));
            metaFieldRequest.setFldSort(newBaseNumber);

            metaFieldRequest.setRealFlag(YesOrNotEnum.N.getCode());
        }
    }

}
