package sqlancer.databend.ast;

import sqlancer.common.ast.newast.Node;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

public class DatabendConstant implements Node<DatabendExpression> {

    private DatabendConstant() {
    }

    public static class DatabendNullConstant extends DatabendConstant {

        @Override
        public String toString() {
            return "NULL";
        }

    }

    public static class DatabendIntConstant extends DatabendConstant {

        private final long value;

        public DatabendIntConstant(long value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return String.valueOf(value);
        }

        public long getValue() {
            return value;
        }

    }

    public static class DatabendDoubleConstant extends DatabendConstant {

        private final double value;

        public DatabendDoubleConstant(double value) {
            this.value = value;
        }

        public double getValue() {
            return value;
        }

        @Override
        public String toString() {
            if (value == Double.POSITIVE_INFINITY) {
                return "'+Inf'";
            } else if (value == Double.NEGATIVE_INFINITY) {
                return "'-Inf'";
            }


//            DecimalFormat doubleFormat = new DecimalFormat();
//            doubleFormat.setMaximumFractionDigits(2);  //小数点后2位
//            System.out.println(String.valueOf(doubleFormat.format(value)));
            //TODO value先返回0.3来取消指数形式表达，等待Databend支持指数形式表达
            return String.valueOf(0.3);
        }

    }

    public static class DatabendTextConstant extends DatabendConstant {

        private final String value;

        public DatabendTextConstant(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        @Override
        public String toString() {
            return "'" + value.replace("'", "''") + "'";
        }

    }

    public static class DatabendBitConstant extends DatabendConstant {

        private final String value;

        public DatabendBitConstant(long value) {
            this.value = Long.toBinaryString(value);
        }

        public String getValue() {
            return value;
        }

        @Override
        public String toString() {
            return "B'" + value + "'";
        }

    }

    public static class DatabendDateConstant extends DatabendConstant {

        public String textRepr;

        public DatabendDateConstant(long val) {
            Timestamp timestamp = new Timestamp(val);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            textRepr = dateFormat.format(timestamp);
        }

        public String getValue() {
            return textRepr;
        }

        @Override
        public String toString() {
            return String.format("DATE '%s'", textRepr);
        }

    }

    public static class DatabendTimestampConstant extends DatabendConstant {

        public String textRepr;

        public DatabendTimestampConstant(long val) {
            Timestamp timestamp = new Timestamp(val);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            textRepr = dateFormat.format(timestamp);
        }

        public String getValue() {
            return textRepr;
        }

        @Override
        public String toString() {
            return String.format("TIMESTAMP '%s'", textRepr);
        }

    }

    public static class DatabendBooleanConstant extends DatabendConstant {

        private final boolean value;

        public DatabendBooleanConstant(boolean value) {
            this.value = value;
        }

        public boolean getValue() {
            return value;
        }

        @Override
        public String toString() {
            return String.valueOf(value);
        }

    }

    public static Node<DatabendExpression> createStringConstant(String text) {
        return new DatabendTextConstant(text);
    }

    public static Node<DatabendExpression> createFloatConstant(double val) {
        return new DatabendDoubleConstant(val);
    }

    public static Node<DatabendExpression> createIntConstant(long val) {
        return new DatabendIntConstant(val);
    }

    public static Node<DatabendExpression> createNullConstant() {
        return new DatabendNullConstant();
    }

    public static Node<DatabendExpression> createBooleanConstant(boolean val) {
        return new DatabendBooleanConstant(val);
    }

    public static Node<DatabendExpression> createDateConstant(long integer) {
        return new DatabendDateConstant(integer);
    }

    public static Node<DatabendExpression> createTimestampConstant(long integer) {
        return new DatabendTimestampConstant(integer);
    }

}

@@ -1320,18 +1320,16 @@
     %7D%0A%0A%0A
-//
         
@@ -1336,300 +1336,81 @@
     
-DecimalFormat doubleFormat = new DecimalFormat();%0A//            doubleFormat.setMaximumFractionDigits(2);  //%E5%B0%8F%E6%95%B0%E7%82%B9%E5%90%8E2%E4%BD%8D%0A//            System.out.println(String.valueOf(doubleFormat.format(value)));%0A           
+return String.valueOf(value).toLowerCase();
  //
+ 
 TODO 
-value%E5%85%88%E8%BF%94%E5%9B%9E0.3%E6%9D%A5%E5%8F%96%E6%B6%88%E6%8C%87%E6%95%B0%E5%BD%A2%E5%BC%8F%E8%A1%A8%E8%BE%BE%EF%BC%8C%E7%AD%89%E5%BE%85Databend%E6%94%AF%E6%8C%81%E6%8C%87%E6%95%B0%E5%BD%A2%E5%BC%8F%E8%A1%A8%E8%BE%BE%0A            return String.valueOf(0.3);
+%E5%BE%85databend%E6%94%AF%E6%8C%81%E5%A4%A7%E5%86%99E%E5%86%8D%E5%8E%BB%E6%8E%89%E5%B0%8F%E5%86%99%E8%BD%AC%E6%8D%A2
 %0A   

update double constant