using System;
using JetBrains.Annotations;
using Reinforcement.Core;

namespace Reinforcement
{
  public class RedirectTypeAction : IAction
  {
    private readonly Type _redirectTo;
    private readonly object _token;

    public RedirectTypeAction([NotNull] Type redirectTo, [CanBeNull] object token)
    {
      if (redirectTo == null) throw new ArgumentNullException("redirectTo");
      if (redirectTo.IsGenericTypeDefinition) throw new ArgumentException("Type should not be open generic, use RedirectOpenGenericTypeAction for open generics", "redirectTo");
      _redirectTo = redirectTo;
      _token = token;
    }

    public object AssembleStage { get { return Reinforcement.AssembleStage.Redirect; } }

    public void Execute(Assembler assembler)
    {
      if (assembler.AssembleResult == null)
        assembler.AssembleResult = assembler.Assemble(new UnitInfo(_redirectTo, _token));
    }

    public void PostProcess(Assembler assembler)
    {}

    public override string ToString()
    {
      return string.Format("[{0}: _redirectTo={1}]", GetType().Name, _redirectTo);
    }
  }
}