package com.sunzn.action.box.library;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by sunzn on 2017/2/16.
 */

public abstract class ActionBox {

    private int mResource;
    private View mRootView;
    private Context mContext;
    private Dialog mActionBox;
    private boolean mCancelable = true;
    private boolean mCanceledOnTouch = true;

    public ActionBox(Context context, int resource) {
        this.mContext = context;
        this.mResource = resource;
    }

    public ActionBox create() {
        mRootView = LayoutInflater.from(mContext).inflate(mResource, null);
        mActionBox = new Dialog(mContext, R.style.Theme_ActionBox);
        mActionBox.setCanceledOnTouchOutside(getCanceledOnTouch());
        mActionBox.setCancelable(getCancelable());
        mActionBox.setContentView(mRootView);
        Window window = mActionBox.getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams params = window.getAttributes();
            params.x = 0;
            params.y = 0;
            params.width = ScreenUtils.getScreenWidth(mContext);
            window.setAttributes(params);
        }
        onActionBoxCreated();
        return this;
    }

    public void onShow() {
        // TODO
    }

    public void onFade() {
        // TODO
    }

    public void show() {
        if (mActionBox != null && !mActionBox.isShowing()) {
            mActionBox.show();
            onShow();
        }
    }

    public void fade() {
        if (mActionBox != null && mActionBox.isShowing()) {
            mActionBox.dismiss();
            onFade();
        }
    }

    public void postDelayShow(long millis) {
        if (mActionBox != null && !mActionBox.isShowing() && mRootView != null) {
            mRootView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    show();
                }
            }, millis);
        }
    }

    public void postDelayFade(long millis) {
        if (mActionBox != null && mActionBox.isShowing() && mRootView != null) {
            mRootView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    fade();
                }
            }, millis);
        }
    }

    public abstract void onActionBoxCreated();

    private boolean getCancelable() {
        return this.mCancelable;
    }

    private boolean getCanceledOnTouch() {
        return this.mCanceledOnTouch;
    }

    public ActionBox setCancelable(boolean cancel) {
        this.mCancelable = cancel;
        return this;
    }

    public ActionBox setCanceledOnTouch(boolean cancel) {
        this.mCanceledOnTouch = cancel;
        return this;
    }

    public Context getContext() {
        return mActionBox.getContext();
    }

    protected View findViewById(int id) {
        return mRootView == null ? null : mRootView.findViewById(id);
    }

}

@@ -320,19 +320,22 @@
 ctionBox
+%3CT%3E
  %7B%0A
-
 %0A    pri
@@ -1515,32 +1515,138 @@
  // TODO%0A    %7D%0A%0A
+    public void onShow(T t) %7B%0A        // TODO%0A    %7D%0A%0A    public void onFade(T t) %7B%0A        // TODO%0A    %7D%0A%0A
     public void 
@@ -1935,32 +1935,354 @@
        %7D%0A    %7D%0A%0A
+    public void show(T t) %7B%0A        if (mActionBox != null && !mActionBox.isShowing()) %7B%0A            mActionBox.show();%0A            onShow(t);%0A        %7D%0A    %7D%0A%0A    public void fade(T t) %7B%0A        if (mActionBox != null && mActionBox.isShowing()) %7B%0A            mActionBox.dismiss();%0A            onFade(t);%0A        %7D%0A    %7D%0A%0A
     public void 
@@ -2823,32 +2823,32 @@
 ic void run() %7B%0A
-
                 
@@ -2910,32 +2910,709 @@
        %7D%0A    %7D%0A%0A
+    public void postDelayShow(long millis, final T t) %7B%0A        if (mActionBox != null && !mActionBox.isShowing() && mRootView != null) %7B%0A            mRootView.postDelayed(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    show(t);%0A                %7D%0A            %7D, millis);%0A        %7D%0A    %7D%0A%0A    public void postDelayFade(long millis, final T t) %7B%0A        if (mActionBox != null && mActionBox.isShowing() && mRootView != null) %7B%0A            mRootView.postDelayed(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    fade(t);%0A                %7D%0A            %7D, millis);%0A        %7D%0A    %7D%0A%0A
     public abstr

Add Generic