package com.chenyang.druid.sql.dialect.kingbase.ast.expression;

import com.chenyang.druid.sql.ast.SQLExpr;
import com.chenyang.druid.sql.ast.SQLExprImpl;
import com.chenyang.druid.sql.ast.SQLReplaceable;
import com.chenyang.druid.sql.ast.expr.SQLLiteralExpr;
import com.chenyang.druid.sql.dialect.kingbase.KingbaseExpr;
import com.chenyang.druid.sql.dialect.kingbase.visitor.KingbaseASTVisitor;
import com.chenyang.druid.sql.visitor.SQLASTVisitor;
import java.util.Locale;

public class KingbaseDateTimeExpr extends SQLExprImpl implements KingbaseExpr, SQLLiteralExpr, SQLReplaceable {
   private SQLExpr precision;
   private DateTimeType timeType;
   private boolean withTimezone;
   private boolean withoutTimezone;
   private boolean local;
   private SQLExpr value;
   private SQLExpr intervalBegin;
   private SQLExpr intervalEnd;

   public boolean replace(SQLExpr expr, SQLExpr target) {
      if (expr == this.value) {
         this.value = target;
         return true;
      } else if (expr == this.precision) {
         this.precision = target;
         return true;
      } else {
         return false;
      }
   }

   protected void accept0(SQLASTVisitor v) {
      this.accept0((KingbaseASTVisitor)v);
   }

   public void accept0(KingbaseASTVisitor visitor) {
      if (visitor.visit(this)) {
         if (this.precision != null) {
            this.precision.accept(visitor);
         }

         if (this.value != null) {
            this.value.accept(visitor);
         }
      }

   }

   public SQLExpr getPrecision() {
      return this.precision;
   }

   public void setPrecision(SQLExpr precision) {
      this.precision = precision;
   }

   public DateTimeType getTimeType() {
      return this.timeType;
   }

   public void setTimeType(DateTimeType timeType) {
      this.timeType = timeType;
   }

   public boolean isWithTimezone() {
      return this.withTimezone;
   }

   public void setWithTimezone(boolean withTimezone) {
      this.withTimezone = withTimezone;
   }

   public boolean isWithoutTimezone() {
      return this.withoutTimezone;
   }

   public void setWithoutTimezone(boolean withoutTimezone) {
      this.withoutTimezone = withoutTimezone;
   }

   public SQLExpr getValue() {
      return this.value;
   }

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

   public SQLExpr getIntervalBegin() {
      return this.intervalBegin;
   }

   public void setIntervalBegin(SQLExpr intervalBegin) {
      this.intervalBegin = intervalBegin;
   }

   public SQLExpr getIntervalEnd() {
      return this.intervalEnd;
   }

   public void setIntervalEnd(SQLExpr intervalEnd) {
      this.intervalEnd = intervalEnd;
   }

   public boolean isLocal() {
      return this.local;
   }

   public void setLocal(boolean local) {
      this.local = local;
   }

   public int hashCode() {
      int prime = 31;
      int result = 1;
      result = 31 * result + (this.precision == null ? 0 : this.precision.hashCode());
      result = 31 * result + (this.timeType == null ? 0 : this.timeType.hashCode());
      result = 31 * result + (this.withTimezone ? 0 : 1);
      result = 31 * result + (this.withoutTimezone ? 0 : 10);
      result = 31 * result + (this.local ? 0 : 100);
      result = 31 * result + (this.value == null ? 0 : this.value.hashCode());
      result = 31 * result + (this.intervalBegin == null ? 0 : this.intervalBegin.hashCode());
      result = 31 * result + (this.intervalEnd == null ? 0 : this.intervalEnd.hashCode());
      return result;
   }

   public KingbaseDateTimeExpr clone() {
      KingbaseDateTimeExpr x = new KingbaseDateTimeExpr();
      if (this.precision != null) {
         x.precision = this.precision.clone();
      }

      x.timeType = this.timeType;
      x.withoutTimezone = this.withoutTimezone;
      x.withTimezone = this.withTimezone;
      x.value = this.value.clone();
      x.local = this.local;
      if (this.intervalBegin != null) {
         x.intervalBegin = this.intervalBegin;
         if (this.intervalEnd != null) {
            x.intervalEnd = this.intervalEnd;
         }
      }

      return x;
   }

   public boolean equals(Object obj) {
      if (obj == null) {
         return false;
      } else if (this.getClass() != obj.getClass()) {
         return false;
      } else {
         KingbaseDateTimeExpr other = (KingbaseDateTimeExpr)obj;
         if (this.precision == null) {
            if (other.precision != null) {
               return false;
            }
         } else if (!this.precision.equals(other.precision)) {
            return false;
         }

         if (this.timeType == null) {
            if (other.timeType != null) {
               return false;
            }
         } else if (!this.timeType.equals(other.timeType)) {
            return false;
         }

         if (this.withTimezone != other.withTimezone) {
            return false;
         } else if (this.withoutTimezone != other.withoutTimezone) {
            return false;
         } else if (this.local != other.local) {
            return false;
         } else {
            if (this.intervalBegin == null) {
               if (other.intervalBegin != null) {
                  return false;
               }
            } else if (!this.intervalBegin.equals(other.intervalBegin)) {
               return false;
            }

            if (this.intervalEnd == null) {
               if (other.intervalEnd != null) {
                  return false;
               }
            } else if (!this.intervalEnd.equals(other.intervalEnd)) {
               return false;
            }

            if (this.value == null) {
               if (other.value != null) {
                  return false;
               }
            } else if (!this.value.equals(other.value)) {
               return false;
            }

            return true;
         }
      }
   }

   public String toString() {
      return this.timeType.toString().toLowerCase(Locale.ROOT);
   }

   public static enum DateTimeType {
      TIMESTAMP("TIMESTAMP"),
      TIMESTAMPTZ("TIMESTAMPTZ"),
      DATE("DATE"),
      TIME("TIME"),
      INTERVAL("INTERVAL");

      public final String name;
      public final String name_lower;

      private DateTimeType(String name) {
         this.name = name;
         this.name_lower = name.toLowerCase();
      }
   }
}
