// Copyright (C) 2009-2024 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.

using System;
using System.Collections.Generic;

namespace Xtensive.Sql.Dml
{
  [Serializable]
  public class SqlUpdate : SqlQueryStatement, ISqlCompileUnit
  {
    private SqlExpression where;
    private readonly Dictionary<ISqlLValue, SqlExpression> values = new Dictionary<ISqlLValue, SqlExpression>();
    private SqlTable from;
    private SqlTableRef update;
    private SqlExpression limit;

    /// <summary>
    /// Gets or sets the table.
    /// </summary>
    /// <value>The table to change.</value>
    public SqlTableRef Update
    {
      get { return update; }
      set { update = value; }
    }

    /// <summary>
    /// Gets the values.
    /// </summary>
    /// <value>The values.</value>
    public Dictionary<ISqlLValue, SqlExpression> Values {
      get {
        return values;
      }
    }

    /// <summary>
    /// Gets or sets the WHERE clause expression.
    /// </summary>
    /// <value>The WHERE clause expression.</value>
    public SqlExpression Where {
      get { return where; }
      set {
        if (value is not null && value.GetType()!=typeof(SqlCursor))
          SqlValidator.EnsureIsBooleanExpression(value);
        where = value;
      }
    }

    /// <summary>
    /// Gets or sets the FROM clause expression.
    /// </summary>
    public SqlTable From 
    {
      get { return from;}
      set { from = value; }
    }

    /// <summary>
    /// Gets or sets the LIMIT clause expression.
    /// </summary>
    public SqlExpression Limit
    {
      get { return limit; }
      set { limit = value; }
    }

    internal override object Clone(SqlNodeCloneContext context)
    {
      if (context.NodeMapping.TryGetValue(this, out var value)) {
        return value;
      }

      var clone = new SqlUpdate();
      if (update != null)
        clone.Update = (SqlTableRef) Update.Clone(context);
      if (from != null)
        clone.From = (SqlQueryRef) from.Clone(context);
      foreach (KeyValuePair<ISqlLValue, SqlExpression> p in values)
        clone.Values[(ISqlLValue) ((SqlExpression) p.Key).Clone(context)] =
          p.Value is null ? null : (SqlExpression) p.Value.Clone(context);
      if (where is not null)
        clone.Where = (SqlExpression) where.Clone(context);
      if (limit is not null)
        clone.Limit = (SqlExpression) where.Clone(context);
      if (Hints.Count > 0)
        foreach (SqlHint hint in Hints)
          clone.Hints.Add((SqlHint) hint.Clone(context));

      context.NodeMapping[this] = clone;
      return clone;
    }

    // Constructor

    internal SqlUpdate(): base(SqlNodeType.Update)
    {
    }

    internal SqlUpdate(SqlTableRef table) : this()
    {
      update = table;
    }

    public override void AcceptVisitor(ISqlVisitor visitor)
    {
      visitor.Visit(this);
    }
  }
}
