package net.northking.cctp.element.mobile.entity;

import net.northking.cctp.element.core.exception.ParamMistakeException;
import org.springframework.util.StringUtils;

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

public class ScriptParam {
    private String name;
    private String desc;
    private Object cacheValue;
    private String type;
    private Object defaultValue;
    private List<String> quoted;
	private String value;
	private boolean required;

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Object getCacheValue() {
        return cacheValue;
    }

    public void setCacheValue(Object cacheValue) {
        this.cacheValue = cacheValue;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Object getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(Object defaultValue) {
        this.defaultValue = defaultValue;
    }

    public List<String> getQuoted() {
        return quoted;
    }

    public void setQuoted(List<String> quoted) {
        this.quoted = quoted;
    }

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    public Object paramValue() {
        Object result = new Object();
        if ("string".equals(type)) {
            if (cacheValue != null) {
                result = cacheValue;
            } else {
                if (null == defaultValue || !StringUtils.hasText((String) defaultValue)) {
                    result = "";
                }else {
                    result = defaultValue;
                }
            }
        } else if ("number".equals(type)) {
            if (cacheValue != null && !"".equals(cacheValue)) {
                try {
                    result = new BigDecimal(cacheValue.toString());
                } catch (Exception e) {
                    throw new ParamMistakeException("变量【" + this.name + "】类型为数字，值类型对应不上");
                }
            } else {
                if (null == defaultValue || "".equals(defaultValue)) {
                    result = new BigDecimal("0.0");
                }else {
                    result = new BigDecimal(defaultValue.toString());
                }
            }
        } else if ("boolean".equals(type)) {
            if (cacheValue != null && !"".equals(cacheValue)) {
                try {
                    result = Boolean.valueOf(cacheValue.toString());
                } catch (Exception e) {
                    throw new ParamMistakeException("变量【" + this.name + "】类型为布尔，值类型对应不上");
                }
            } else {
                if (null == defaultValue || "".equals(defaultValue)) {
                    result = false;
                }else {
                    result = Boolean.valueOf(defaultValue.toString());
                }
            }
        } else if ("object".equals(type)) {
            if (cacheValue != null) {
                result = cacheValue;
            } else {
                if (null != defaultValue) {
                    result = defaultValue;
                }
            }
        }
        return result;
    }
}
