/**
 * Copyright 2016 Keepsafe Software, Inc.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.getkeepsafe.taptargetview;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Queue;

/**
 * Displays a sequence of {@link TapTargetView}s.
 * <p>
 * Internally, a FIFO queue is held to dictate which {@link TapTarget} will be shown.
 */
public class TapTargetSequence {
  private final Activity activity;
  private final Queue<TapTarget> targets;
  private boolean active;

  @Nullable
  private TapTargetView currentView;

  Listener listener;
  boolean considerOuterCircleCanceled;
  boolean continueOnCancel;

  public interface Listener {
    /** Called when there are no more tap targets to display */
    void onSequenceFinish();

    /**
     * Called when moving onto the next tap target.
     * @param lastTarget The last displayed target
     * @param targetClicked Whether the last displayed target was clicked (this will always be true
     *                      unless you have set {@link #continueOnCancel(boolean)} and the user
     *                      clicks outside of the target
     */
    void onSequenceStep(TapTarget lastTarget, boolean targetClicked);

    /**
     * Called when the user taps outside of the current target, the target is cancelable, and
     * {@link #continueOnCancel(boolean)} is not set.
     * @param lastTarget The last displayed target
     */
    void onSequenceCanceled(TapTarget lastTarget);
  }

  public TapTargetSequence(Activity activity) {
    if (activity == null) throw new IllegalArgumentException("Activity is null");
    this.activity = activity;
    this.targets = new LinkedList<>();
  }

  /** Adds the given targets, in order, to the pending queue of {@link TapTarget}s */
  public TapTargetSequence targets(List<TapTarget> targets) {
    this.targets.addAll(targets);
    return this;
  }

  /** Adds the given targets, in order, to the pending queue of {@link TapTarget}s */
  public TapTargetSequence targets(TapTarget... targets) {
    Collections.addAll(this.targets, targets);
    return this;
  }

  /** Adds the given target to the pending queue of {@link TapTarget}s */
  public TapTargetSequence target(TapTarget target) {
    this.targets.add(target);
    return this;
  }

  /** Whether or not to continue the sequence when a {@link TapTarget} is canceled **/
  public TapTargetSequence continueOnCancel(boolean status) {
    this.continueOnCancel = status;
    return this;
  }

  /** Whether or not to consider taps on the outer circle as a cancellation **/
  public TapTargetSequence considerOuterCircleCanceled(boolean status) {
    this.considerOuterCircleCanceled = status;
    return this;
  }

  /** Specify the listener for this sequence **/
  public TapTargetSequence listener(Listener listener) {
    this.listener = listener;
    return this;
  }

  /** Immediately starts the sequence and displays the first target from the queue **/
  @UiThread
  public void start() {
    if (targets.isEmpty() || active) {
      return;
    }

    active = true;
    showNext();
  }

  /**
   * Cancels the sequence, if the current target is cancelable.
   * When the sequence is canceled, the current target is dismissed and the remaining targets are
   * removed from the sequence.
   * @return whether the sequence was canceled or not
   */
  @UiThread
  public boolean cancel() {
    if (targets.isEmpty() || !active) {
      return false;
    }
    if (currentView == null || !currentView.cancelable) {
      return false;
    }
    currentView.dismiss(false);
    active = false;
    targets.clear();
    if (listener != null) {
      listener.onSequenceCanceled(currentView.target);
    }
    return true;
  }

  void showNext() {
    try {
      currentView = TapTargetView.showFor(activity, targets.remove(), tapTargetListener);
    } catch (NoSuchElementException e) {
      // No more targets
      if (listener != null) {
        listener.onSequenceFinish();
      }
    }
  }

  private final TapTargetView.Listener tapTargetListener = new TapTargetView.Listener() {
    @Override
    public void onTargetClick(TapTargetView view) {
      super.onTargetClick(view);
      if (listener != null) {
        listener.onSequenceStep(view.target, true);
      }
      showNext();
    }

    @Override
    public void onOuterCircleClick(TapTargetView view) {
      if (considerOuterCircleCanceled) {
        onTargetCancel(view);
      }
    }

    @Override
    public void onTargetCancel(TapTargetView view) {
      super.onTargetCancel(view);
      if (continueOnCancel) {
        if (listener != null) {
          listener.onSequenceStep(view.target, false);
        }
        showNext();
      } else {
        if (listener != null) {
          listener.onSequenceCanceled(view.target);
        }
      }
    }
  };
}

@@ -678,16 +678,43 @@
 tivity;%0A
+import android.app.Dialog;%0A
 import a
@@ -749,16 +749,16 @@
 llable;%0A
-
 import a
@@ -1142,16 +1142,26 @@
 e final 
+@Nullable 
 Activity
@@ -1171,16 +1171,57 @@
 tivity;%0A
+  private final @Nullable Dialog dialog;%0A
   privat
@@ -2418,16 +2418,261 @@
 tivity;%0A
+    this.dialog = null;%0A    this.targets = new LinkedList%3C%3E();%0A  %7D%0A%0A  public TapTargetSequence(Dialog dialog) %7B%0A    if (dialog == null) throw new IllegalArgumentException(%22Given null Dialog%22);%0A    this.dialog = dialog;%0A    this.activity = null;%0A
     this
@@ -4779,16 +4779,94 @@
   try %7B%0A
+      TapTarget tapTarget = targets.remove();%0A      if (activity != null) %7B%0A  
       cu
@@ -4911,30 +4911,121 @@
 vity, ta
+pTa
 rget
-s.remove()
+, tapTargetListener);%0A      %7D else %7B%0A        currentView = TapTargetView.showFor(dialog, tapTarget
 , tapTar
@@ -5034,24 +5034,32 @@
 tListener);%0A
+      %7D%0A
     %7D catch 

Add support for TapTargetSequences to act on Dialogs (#198)