package me.ykrank.s1next.view.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.support.v4.app.DialogFragment;
import android.view.View;

import com.google.common.base.Optional;

import java.lang.ref.WeakReference;

import javax.inject.Inject;

import me.ykrank.s1next.App;
import me.ykrank.s1next.R;
import me.ykrank.s1next.view.internal.CoordinatorLayoutAnchorDelegate;
import me.ykrank.s1next.widget.track.DataTrackAgent;
import me.ykrank.s1next.widget.track.event.page.FragmentEndEvent;
import me.ykrank.s1next.widget.track.event.page.FragmentStartEvent;

/**
 * Created by ykrank on 2016/12/28.
 */

public abstract class BaseDialogFragment extends DialogFragment {
    @Inject
    DataTrackAgent trackAgent;

    protected CoordinatorLayoutAnchorDelegate mCoordinatorLayoutAnchorDelegate;
    @Nullable
    protected WeakReference<Snackbar> mRetrySnackbar;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        mCoordinatorLayoutAnchorDelegate = (CoordinatorLayoutAnchorDelegate) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        App.getAppComponent().inject(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        trackAgent.post(new FragmentStartEvent(this));
    }

    @Override
    public void onPause() {
        trackAgent.post(new FragmentEndEvent(this));
        super.onPause();
    }

    public void showRetrySnackbar(CharSequence text, View.OnClickListener onClickListener) {
        Optional<Snackbar> snackbar = mCoordinatorLayoutAnchorDelegate.showLongSnackbarIfVisible(
                text, R.string.snackbar_action_retry, onClickListener);
        if (snackbar.isPresent()) {
            mRetrySnackbar = new WeakReference<>(snackbar.get());
        }
    }

    protected void showShortSnackbar(CharSequence text) {
        mCoordinatorLayoutAnchorDelegate.showShortSnackbar(text);
    }

    protected void showShortSnackbar(@StringRes int resId) {
        mCoordinatorLayoutAnchorDelegate.showShortSnackbar(resId);
    }

    protected void showShortText(@StringRes int resId) {
        mCoordinatorLayoutAnchorDelegate.showShortText(getString(resId));
    }

    protected void showLongSnackbar(@StringRes int resId) {
        mCoordinatorLayoutAnchorDelegate.showLongSnackbar(resId);
    }

    protected void dismissRetrySnackbarIfExist() {
        if (mRetrySnackbar != null) {
            Snackbar snackbar = mRetrySnackbar.get();
            if (snackbar != null && snackbar.isShownOrQueued()) {
                snackbar.dismiss();
            }
            mRetrySnackbar = null;
        }
    }
}

@@ -36,40 +36,8 @@
 g;%0A%0A
-import android.content.Context;%0A
 impo
@@ -54,24 +54,24 @@
 .os.Bundle;%0A
+
 import andro
@@ -106,53 +106,8 @@
 le;%0A
-import android.support.annotation.StringRes;%0A
 impo
@@ -198,75 +198,8 @@
 ent;
-%0Aimport android.view.View;%0A%0Aimport com.google.common.base.Optional;
 %0A%0Aim
@@ -295,106 +295,8 @@
 pp;%0A
-import me.ykrank.s1next.R;%0Aimport me.ykrank.s1next.view.internal.CoordinatorLayoutAnchorDelegate;%0A
 impo
@@ -642,336 +642,71 @@
     
-protected CoordinatorLayoutAnchorDelegate mCoordinatorLayoutAnchorDelegate;%0A    @Nullable%0A    protected WeakReference%3CSnackbar%3E mRetrySnackbar;%0A%0A    @Override%0A    public void onAttach(Context context) %7B%0A        super.onAttach(context);%0A%0A        mCoordinatorLayoutAnchorDelegate = (CoordinatorLayoutAnchorDelegate) context;%0A    %7D
+@Nullable%0A    protected WeakReference%3CSnackbar%3E mRetrySnackbar;
 %0A%0A  
@@ -1098,24 +1098,24 @@
 ent(this));%0A
+
         supe
@@ -1136,1237 +1136,7 @@
    %7D
-%0A%0A    public void showRetrySnackbar(CharSequence text, View.OnClickListener onClickListener) %7B%0A        Optional%3CSnackbar%3E snackbar = mCoordinatorLayoutAnchorDelegate.showLongSnackbarIfVisible(%0A                text, R.string.snackbar_action_retry, onClickListener);%0A        if (snackbar.isPresent()) %7B%0A            mRetrySnackbar = new WeakReference%3C%3E(snackbar.get());%0A        %7D%0A    %7D%0A%0A    protected void showShortSnackbar(CharSequence text) %7B%0A        mCoordinatorLayoutAnchorDelegate.showShortSnackbar(text);%0A    %7D%0A%0A    protected void showShortSnackbar(@StringRes int resId) %7B%0A        mCoordinatorLayoutAnchorDelegate.showShortSnackbar(resId);%0A    %7D%0A%0A    protected void showShortText(@StringRes int resId) %7B%0A        mCoordinatorLayoutAnchorDelegate.showShortText(getString(resId));%0A    %7D%0A%0A    protected void showLongSnackbar(@StringRes int resId) %7B%0A        mCoordinatorLayoutAnchorDelegate.showLongSnackbar(resId);%0A    %7D%0A%0A    protected void dismissRetrySnackbarIfExist() %7B%0A        if (mRetrySnackbar != null) %7B%0A            Snackbar snackbar = mRetrySnackbar.get();%0A            if (snackbar != null && snackbar.isShownOrQueued()) %7B%0A                snackbar.dismiss();%0A            %7D%0A            mRetrySnackbar = null;%0A        %7D%0A    %7D
 %0A%7D%0A

Fix one error