package com.alibaba.druid.sql.dialect.mysql.ast.statement;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr;
import com.alibaba.druid.sql.ast.expr.SQLValuableExpr;
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
import com.alibaba.druid.util.FnvHash;
import java.util.ArrayList;
import java.util.List;

public class MysqlCreateFullTextTokenizerStatement extends MySqlStatementImpl {
   private SQLName name;
   private SQLTextLiteralExpr typeName;
   private SQLTextLiteralExpr userDefinedDict;
   protected final List<SQLAssignItem> options = new ArrayList();

   public void accept0(MySqlASTVisitor visitor) {
      if (visitor.visit(this)) {
         this.acceptChild(visitor, this.name);
         this.acceptChild(visitor, this.typeName);
         this.acceptChild(visitor, this.options);
      }

      visitor.endVisit(this);
   }

   public SQLName getName() {
      return this.name;
   }

   public void setName(SQLName name) {
      if (name != null) {
         name.setParent(this);
      }

      this.name = name;
   }

   public SQLTextLiteralExpr getUserDefinedDict() {
      return this.userDefinedDict;
   }

   public void setUserDefinedDict(SQLTextLiteralExpr userDefinedDict) {
      this.userDefinedDict = userDefinedDict;
   }

   public SQLTextLiteralExpr getTypeName() {
      return this.typeName;
   }

   public void setTypeName(SQLTextLiteralExpr typeName) {
      if (this.name != null) {
         this.name.setParent(this);
      }

      this.typeName = typeName;
   }

   public List<SQLAssignItem> getOptions() {
      return this.options;
   }

   public void addOption(String name, SQLExpr value) {
      SQLAssignItem assignItem = new SQLAssignItem(new SQLIdentifierExpr(name), value);
      assignItem.setParent(this);
      this.options.add(assignItem);
   }

   public SQLExpr getOption(String name) {
      if (name == null) {
         return null;
      } else {
         long hash64 = FnvHash.hashCode64(name);

         for(SQLAssignItem item : this.options) {
            SQLExpr target = item.getTarget();
            if (target instanceof SQLIdentifierExpr && ((SQLIdentifierExpr)target).hashCode64() == hash64) {
               return item.getValue();
            }
         }

         return null;
      }
   }

   public Object getOptionValue(String name) {
      SQLExpr option = this.getOption(name);
      return option instanceof SQLValuableExpr ? ((SQLValuableExpr)option).getValue() : null;
   }
}
