package com.xunao.effectdemo.view;

import com.xuexiang.xui.widget.guidview.DismissListener;
import com.xuexiang.xui.widget.guidview.GuideCaseQueue;
import com.xuexiang.xui.widget.guidview.GuideCaseView;
import com.xuexiang.xui.widget.guidview.OnCompleteListener;

import java.util.LinkedList;
import java.util.Queue;

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/8/316:36
 * desc :
 * version: 1.0
 */
public class MyGuideCaseQueue implements DismissListener {

	private Queue<MyGuideCaseView> mQueue;
	private DismissListener mCurrentOriginalDismissListener;
	private MyGuideCaseView mCurrent;
	private OnCompleteListener mCompleteListener;

	/**
	 * Constructor
	 */
	public MyGuideCaseQueue() {
		mQueue = new LinkedList<>();
		mCurrentOriginalDismissListener = null;
	}

	/**
	 * Adds a GuideCaseView to the queue
	 *
	 * @param showCaseView the view that should be added to the queue
	 * @return Builder
	 */
	public MyGuideCaseQueue add(MyGuideCaseView showCaseView) {
		mQueue.add(showCaseView);
		return this;
	}

	/**
	 * Starts displaying all views in order of their insertion in the queue, one after another
	 */
	public void show() {
		if (!mQueue.isEmpty()) {
			mCurrent = mQueue.poll();
			mCurrentOriginalDismissListener = mCurrent.getDismissListener();
			mCurrent.setDismissListener(this);
			mCurrent.show();
		} else if (mCompleteListener != null) {
			mCompleteListener.onComplete();
		}
	}

	/**
	 * Cancels the queue
	 * @param hideCurrent hides current GuideCaseView
	 */
	public void cancel(boolean hideCurrent) {
		if (hideCurrent && mCurrent != null) {
			mCurrent.hide();
		}
		if (!mQueue.isEmpty()) {
			mQueue.clear();
		}
	}

	@Override
	public void onDismiss(String id) {
		if (mCurrentOriginalDismissListener != null) {
			mCurrentOriginalDismissListener.onDismiss(id);
		}
		show();
	}

	@Override
	public void onSkipped(String id) {
		if (mCurrentOriginalDismissListener != null) {
			mCurrentOriginalDismissListener.onSkipped(id);
		}
		show();
	}

	public MyGuideCaseQueue setCompleteListener(OnCompleteListener completeListener) {
		mCompleteListener = completeListener;
		return this;
	}
}
