package org.ligi.snackengage.snacks;

import android.support.annotation.IntDef;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.view.View;

import org.ligi.snackengage.SnackContext;
import org.ligi.snackengage.conditions.SnackCondition;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class BaseSnack implements Snack {

    public static final int DURATION_INDEFINITE = Snackbar.LENGTH_INDEFINITE;
    public static final int DURATION_SHORT = Snackbar.LENGTH_SHORT;
    public static final int DURATION_LONG = Snackbar.LENGTH_LONG;
    protected SnackContext snackContext;
    protected List<SnackCondition> conditionList = new ArrayList<>();
    @SnackDuration
    private int duration = DURATION_LONG;

    private String actionText;
    private String titleText;

    @Override
    public boolean opportunity(final SnackContext snackContext) {
        this.snackContext = snackContext;

        for (final SnackCondition snackCondition : conditionList) {
            if (!snackCondition.isAppropriate(snackContext, this)) {
                return false;
            }
        }

        snackContext.getStats().registerSnackShow(this);

        actionText = actionText == null ? getActionText() : actionText;
        titleText = titleText == null ? getText() : titleText;

        Snackbar.make(snackContext.getRootView(), titleText, duration).setAction(actionText, new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                snackContext.getStats().registerSnackClick(BaseSnack.this);
                engage();
            }
        }).show();
        return true;
    }

    public abstract void engage();

    public abstract String getText();

    public abstract String getActionText();

    protected String getString(@StringRes int res) {
        return snackContext.getAndroidContext().getString(res);
    }

    public Snack withDuration(@SnackDuration int duration) {
        this.duration = duration;
        return this;
    }

    public Snack withConditions(SnackCondition... conditions) {
        Collections.addAll(conditionList, conditions);
        return this;
    }

    public Snack overrideActionText(String s) {
        this.actionText = s;
        return this;
    }

    public Snack overrideTitleText(String s) {
        this.titleText = s;
        return this;
    }

    @IntDef({DURATION_INDEFINITE, DURATION_SHORT, DURATION_LONG})
    @Retention(RetentionPolicy.SOURCE)
    public @interface SnackDuration {
    }
}

@@ -2066,32 +2066,36 @@
   %7D%0A%0A    public 
+Base
 Snack withDurati
@@ -2124,24 +2124,24 @@
 duration) %7B%0A
-
         this
@@ -2197,24 +2197,28 @@
 %0A    public 
+Base
 Snack withCo
@@ -2344,32 +2344,36 @@
   %7D%0A%0A    public 
+Base
 Snack overrideAc
@@ -2442,32 +2442,32 @@
 rn this;%0A    %7D%0A%0A
-
     public Snack
@@ -2461,16 +2461,20 @@
  public 
+Base
 Snack ov

return BaseSnack instead of Snack to allow more methods