package com.chenyang.druid.sql.ast.statement;

import com.chenyang.druid.sql.ast.SQLExpr;
import com.chenyang.druid.sql.ast.SQLObjectImpl;
import com.chenyang.druid.sql.ast.SQLReplaceable;
import com.chenyang.druid.sql.ast.SQLStatement;
import com.chenyang.druid.sql.ast.SQLStatementImpl;
import com.chenyang.druid.sql.visitor.SQLASTVisitor;
import java.util.ArrayList;
import java.util.List;

public class SQLIfStatement extends SQLStatementImpl implements SQLReplaceable {
   private SQLExpr condition;
   private List<SQLStatement> statements = new ArrayList();
   private List<ElseIf> elseIfList = new ArrayList();
   private Else elseItem;

   public SQLIfStatement clone() {
      SQLIfStatement x = new SQLIfStatement();

      for(SQLStatement stmt : this.statements) {
         SQLStatement stmt2 = stmt.clone();
         stmt2.setParent(x);
         x.statements.add(stmt2);
      }

      for(ElseIf o : this.elseIfList) {
         ElseIf o2 = o.clone();
         o2.setParent(x);
         x.elseIfList.add(o2);
      }

      if (this.elseItem != null) {
         x.setElseItem(this.elseItem.clone());
      }

      return x;
   }

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

         for(int i = 0; i < this.statements.size(); ++i) {
            ((SQLStatement)this.statements.get(i)).accept(visitor);
         }

         for(int i = 0; i < this.elseIfList.size(); ++i) {
            ((ElseIf)this.elseIfList.get(i)).accept(visitor);
         }

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

      visitor.endVisit(this);
   }

   public SQLExpr getCondition() {
      return this.condition;
   }

   public void setCondition(SQLExpr condition) {
      if (condition != null) {
         condition.setParent(this);
      }

      this.condition = condition;
   }

   public List<SQLStatement> getStatements() {
      return this.statements;
   }

   public void addStatement(SQLStatement statement) {
      if (statement != null) {
         statement.setParent(this);
      }

      this.statements.add(statement);
   }

   public List<ElseIf> getElseIfList() {
      return this.elseIfList;
   }

   public Else getElseItem() {
      return this.elseItem;
   }

   public void setElseItem(Else elseItem) {
      if (elseItem != null) {
         elseItem.setParent(this);
      }

      this.elseItem = elseItem;
   }

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

   public static class ElseIf extends SQLObjectImpl implements SQLReplaceable {
      private SQLExpr condition;
      private List<SQLStatement> statements = new ArrayList();

      public void accept0(SQLASTVisitor visitor) {
         if (visitor.visit(this)) {
            this.acceptChild(visitor, this.condition);
            this.acceptChild(visitor, this.statements);
         }

         visitor.endVisit(this);
      }

      public List<SQLStatement> getStatements() {
         return this.statements;
      }

      public void setStatements(List<SQLStatement> statements) {
         this.statements = statements;
      }

      public SQLExpr getCondition() {
         return this.condition;
      }

      public void setCondition(SQLExpr condition) {
         if (condition != null) {
            condition.setParent(this);
         }

         this.condition = condition;
      }

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

      public ElseIf clone() {
         ElseIf x = new ElseIf();
         if (this.condition != null) {
            x.setCondition(this.condition.clone());
         }

         for(SQLStatement stmt : this.statements) {
            SQLStatement stmt2 = stmt.clone();
            stmt2.setParent(x);
            x.statements.add(stmt2);
         }

         return x;
      }
   }

   public static class Else extends SQLObjectImpl {
      private List<SQLStatement> statements = new ArrayList();

      public void accept0(SQLASTVisitor visitor) {
         if (visitor.visit(this)) {
            this.acceptChild(visitor, this.statements);
         }

         visitor.endVisit(this);
      }

      public List<SQLStatement> getStatements() {
         return this.statements;
      }

      public void setStatements(List<SQLStatement> statements) {
         this.statements = statements;
      }

      public Else clone() {
         Else x = new Else();

         for(SQLStatement stmt : this.statements) {
            SQLStatement stmt2 = stmt.clone();
            stmt2.setParent(x);
            x.statements.add(stmt2);
         }

         return x;
      }
   }
}
