package xyz.wbsite.dbtool.javafx.po;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import org.springframework.util.StringUtils;
import xyz.wbsite.dbtool.javafx.annotation.Property;
import xyz.wbsite.dbtool.javafx.tool.Tool;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Field extends Table {

    public Field() {
    }

    public Field(String fieldName) {
        setFieldName(fieldName);
    }

    //1字段名称
    @Property("fieldName")
    private StringProperty fieldName = new SimpleStringProperty();

    public StringProperty fieldNameProperty() {
        return fieldName;
    }

    public String getFieldName() {
        return fieldName.get();
    }

    public void setFieldName(String fieldName) {
        this.fieldName.set(fieldName);
    }

    //3字段类型
    @Property("fieldType")
    private StringProperty fieldType = new SimpleStringProperty(FieldType.String_var50.name());

    public StringProperty fieldTypeProperty() {
        return fieldType;
    }

    public String getFieldType() {
        return fieldType.get();
    }

    public void setFieldType(String fieldType) {
        this.fieldType.set(fieldType);
    }

    //4字段长度
    @Property("fieldLength")
    private IntegerProperty fieldLength = new SimpleIntegerProperty();

    public IntegerProperty fieldLengthProperty() {
        return fieldLength;
    }

    public int getFieldLength() {
        return fieldLength.get();
    }

    public void setFieldLength(int fieldLength) {
        this.fieldLength.set(fieldLength);
    }

    //5默认值
    @Property("defaultValue")
    private StringProperty defaultValue = new SimpleStringProperty();

    public StringProperty defaultValueProperty() {
        return defaultValue;
    }

    public String getDefaultValue() {
        return defaultValue.get();
    }

    public void setDefaultValue(String value) {
        this.defaultValue.set(value);
    }

    //6主键
    @Property("isPrimaryKey")
    private BooleanProperty isPrimaryKey = new SimpleBooleanProperty();

    public BooleanProperty isPrimaryKeyProperty() {
        return isPrimaryKey;
    }

    public boolean getIsPrimaryKey() {
        return isPrimaryKey.get();
    }

    public void setIsPrimaryKey(boolean value) {
        this.isPrimaryKey.set(value);
    }

    //7必填
    @Property("isMust")
    private BooleanProperty isMust = new SimpleBooleanProperty();

    public BooleanProperty isMustProperty() {
        return isMust;
    }

    public boolean getIsMust() {
        return isMust.get();
    }

    public void setIsMust(boolean value) {
        this.isMust.set(value);
    }

    //8是否唯一，新增前检查重复
    @Property("isUnique")
    private BooleanProperty isUnique = new SimpleBooleanProperty();

    public BooleanProperty isUniqueProperty() {
        return isUnique;
    }

    public boolean getIsUnique() {
        return isUnique.get();
    }

    public void setIsUnique(boolean isUnique) {
        this.isUnique.set(isUnique);
    }


    //9查询
    @Property("isQuery")
    private BooleanProperty isQuery = new SimpleBooleanProperty();

    public BooleanProperty isQueryProperty() {
        return isQuery;
    }

    public boolean getIsQuery() {
        return isQuery.get();
    }

    public void setIsQuery(boolean isQuery) {
        this.isQuery.set(isQuery);
    }

    //10模糊
    @Property("isSearch")
    private BooleanProperty isSearch = new SimpleBooleanProperty();

    public BooleanProperty isSearchProperty() {
        return isSearch;
    }

    public boolean getIsSearch() {
        return isSearch.get();
    }

    public void setIsSearch(boolean isSearch) {
        this.isSearch.set(isSearch);
    }

    //2字段注释
    @Property("fieldComment")
    private StringProperty fieldComment = new SimpleStringProperty("");

    public StringProperty fieldCommentProperty() {
        return fieldComment;
    }

    public String getFieldComment() {
        return fieldComment.get();
    }

    public void setFieldComment(String fieldComment) {
        this.fieldComment.set(fieldComment);
    }

    //11系统
    @Property("isSystem")
    private BooleanProperty isSystem = new SimpleBooleanProperty();

    public BooleanProperty isSystemProperty() {
        return isSystem;
    }

    public boolean getIsSystem() {
        return isSystem.get();
    }

    public void setIsSystem(boolean isSystem) {
        this.isSystem.set(isSystem);
    }

    private List<DictItem> dictItems = new ArrayList<>();

    public List<DictItem> getDictItems() {
        return dictItems;
    }

    public void setDictItems(List<DictItem> dictItems) {
        this.dictItems = dictItems;
    }

    private List<SelectItem> selectItems = new ArrayList<>();

    public List<SelectItem> getSelectItems() {
        return selectItems;
    }

    public void setSelectItems(List<SelectItem> selectItems) {
        this.selectItems = selectItems;
    }

    public String getTestValue() {
        String value = "";
        if (fieldType.get().matches("String_\\d+")) {
            Pattern compile = Pattern.compile("String_(\\d+)");
            Matcher matcher = compile.matcher(fieldType.get());

            if (matcher.find()) {
                int len = Integer.parseInt(matcher.group(1));

                if (StringUtils.isEmpty(getFieldComment())) {
                    StringBuilder sb = new StringBuilder("\"");
                    for (int i = 0; i < len; i++) {
                        sb.append("A");
                    }
                    sb.append("\"");
                    value = sb.toString();
                } else {
                    value = "\"" + getFieldComment().substring(0, getFieldComment().length() > getFieldLength() ? getFieldLength() : getFieldComment().length()) + "\"";
                }

            }
        } else if (fieldType.get().matches("String_var\\d+")) {
            Pattern compile = Pattern.compile("String_var(\\d+)");
            Matcher matcher = compile.matcher(fieldType.get());

            if (matcher.find()) {
                int len = Integer.parseInt(matcher.group(1));

                if (StringUtils.isEmpty(getFieldComment())) {
                    StringBuilder sb = new StringBuilder("");
                    sb.append("\"");
                    sb.append("A");
                    sb.append("\"");
                    value = sb.toString();
                } else {
                    value = "\"" + getFieldComment().substring(0, getFieldComment().length() > getFieldLength() ? getFieldLength() : getFieldComment().length()) + "\"";
                }

            }
        } else if (fieldType.get().equals("String_var")) {
            int len = getFieldLength();
            if (StringUtils.isEmpty(getFieldComment())) {
                StringBuilder sb = new StringBuilder("");
                sb.append("\"");
                sb.append("A");
                sb.append("\"");
                value = sb.toString();
            } else {
                value = "\"" + getFieldComment().substring(0, getFieldComment().length() > getFieldLength() ? getFieldLength() : getFieldComment().length()) + "\"";
            }

        } else if (fieldType.get().matches("Boolean")) {
            value = "true";
        } else if (fieldType.get().matches("Dict")) {
            value = "\"code\"";
        }else if (fieldType.get().matches("Select")) {
            value = "\"value\"";
        } else if (fieldType.get().matches("Byte")) {
            value = "(byte)1";
        } else if (fieldType.get().matches("Short")) {
            value = "(short)1";
        } else if (fieldType.get().matches("Integer")) {
            value = "1";
        } else if (fieldType.get().matches("Bytes")) {
            value = "null";
        } else if (fieldType.get().matches("Long")) {
            value = "1L";
        } else if (fieldType.get().matches("Float")) {
            value = "1.0f";
        } else if (fieldType.get().matches("Double")) {
            value = "1.0";
        } else if (fieldType.get().matches("Character")) {
            value = "'A'";
        } else if (fieldType.get().matches("Date")) {
            value = "new Date()";
        } else if (fieldType.get().matches("BigDecimal")) {
            value = "new BigDecimal()";
        } else if (fieldType.get().matches("String_super")) {
            value = "\"content\"";
        }
        return value;
    }

    public String getterName() {
        return "get" + Tool.ABB2Abb(this.fieldName.get());
    }

    public String setterName() {
        return "set" + Tool.ABB2Abb(this.fieldName.get());
    }

    public String getFName() {
        return Tool.lineToFieldName(this.fieldName.get());
    }

    public String getCName() {
        return Tool.ABB2Abb(this.fieldName.get());
    }

    public String getFieldTypeJava() {
        return FieldType.parse(getFieldType()).javaType();
    }

    public String getFieldTypeJdbc() {
        return FieldType.parse(getFieldType()).jdbcType();
    }
}
