package com.naz.sdkdemo.picselect;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.naz.sdkdemo.R;


/**
 * @Description:
 * @Date: 2020/9/4 上午12:33
 */
public class SDialogHelper extends Dialog{
    public static final int DIALOG_STYLE_NONE = 0;
    public static final int DIALOG_STYLE_CENTER = 1;
    public static final int DIALOG_STYLE_BOTTOM = 2;

    private onDialogListener dialogListener;

    private SDialogHelper(Context context, int themeResId){
        super(context, themeResId);
    }

    public <T extends View> T safeRequestViewById(int id){
        if (Build.VERSION.SDK_INT >= 28) {
            return (T)requireViewById(id);
        } else {
            View view = findViewById(id);
            if (view == null) {
                throw new
                        IllegalArgumentException("ID does not reference a View inside this Dialog");
            }
            return (T)view;
        }
    }

    public int getColor(int id){
        if (null == getContext()){
            return 0;
        }
        return getContext().getResources().getColor(id);
    }

    public int getDimensionPixelSize(int id){
        if (null == getContext()){
            return 0;
        }
        return getContext().getResources().getDimensionPixelSize(id);
    }

    public void setTextView(int viewId, int resId){
        View child = findViewById(viewId);
        if (child instanceof TextView){
            ((TextView) child).setText(resId);
        }
    }

    public void setTextView(int viewId, String text){
        View child = findViewById(viewId);
        if (child instanceof TextView){
            ((TextView) child).setText(text);
        }
    }

    public void setImageView(int viewId, int resId){
        View child = findViewById(viewId);
        if (child instanceof ImageView){
            ((ImageView) child).setImageResource(resId);
        }
    }

    public void setImageView(int viewId, Bitmap bitmap){
        View child = findViewById(viewId);
        if (child instanceof ImageView){
            ((ImageView) child).setImageBitmap(bitmap);
        }
    }

    public void startActivity(Intent intent){
        if (null != getContext()){
            getContext().startActivity(intent);
        }
    }

    public void startActivityForResult(Intent intent, int requestCode){
        if (getContext() instanceof Activity){
            ((Activity)getContext()).startActivityForResult(intent, requestCode);
        }
    }

    public static void safeShow(Dialog dialog){
        safeShow(dialog, true);
    }

    public static void safeShow(Dialog dialog, boolean finishExit){
        if (null == dialog){
            return;
        }
        if (dialog.getContext() == null) {
            return;
        }
        if (dialog.getContext() instanceof Activity && ((Activity)dialog.getContext()).isFinishing()) {
            return;
        }
        if (dialog.isShowing()){
            if (!finishExit){
                return;
            }
            dialog.dismiss();
        }
        dialog.show();
    }

    public static Builder newBuilder(){
        return new Builder();
    }

    public static class Builder{
        private int layoutId;
        private View rootView;
        private boolean cancelable;
        private int themeResId;
        private int dialogStyle = DIALOG_STYLE_NONE; //1--中间弹窗、2--底部弹窗
        private Context context;
        private int gravity = Gravity.BOTTOM;
        private int windowAnimations;
        private int width;
        private int height;
        private onDialogListener dialogListener;
        private OnDismissListener dismissListener;
        private OnCancelListener cancelListener;
        private OnShowListener showListener;

        private Builder(){

        }

        public Builder setLayoutId(int layoutId) {
            this.layoutId = layoutId;
            return this;
        }

        public Builder setRootView(View rootView) {
            this.rootView = rootView;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public Builder setThemeResId(int themeResId) {
            this.themeResId = themeResId;
            return this;
        }

        public Builder setDialogStyle(int dialogStyle) {
            this.dialogStyle = dialogStyle;
            return this;
        }

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public Builder setGravity(int gravity) {
            this.gravity = gravity;
            return this;
        }

        public Builder setWindowAnimations(int windowAnimations) {
            this.windowAnimations = windowAnimations;
            return this;
        }

        public Builder setWidth(int width) {
            this.width = width;
            return this;
        }

        public Builder setHeight(int height) {
            this.height = height;
            return this;
        }

        public Builder setDialogListener(onDialogListener dialogListener) {
            this.dialogListener = dialogListener;
            return this;
        }

        public Builder setOnDismissListener(OnDismissListener dismissListener) {
            this.dismissListener = dismissListener;
            return this;
        }

        public Builder setOnCancelListener(OnCancelListener cancelListener) {
            this.cancelListener = cancelListener;
            return this;
        }

        public Builder setOnShowListener(OnShowListener showListener) {
            this.showListener = showListener;
            return this;
        }

        public SDialogHelper builder(){
            if (context == null){
                throw new IllegalArgumentException("context is null");
            }
            if (dialogStyle == DIALOG_STYLE_BOTTOM){ //底部弹窗
                themeResId = R.style.Dialog_Style;
                gravity = Gravity.BOTTOM;
                windowAnimations = R.style.BottomDialog_Anim;
            }else if (dialogStyle == DIALOG_STYLE_CENTER){ //中间弹窗
                themeResId = R.style.Dialog_Style;
                gravity = Gravity.CENTER;
                windowAnimations = R.style.CenterDialog_Anim;
            }
            SDialogHelper helper = new SDialogHelper(context, themeResId);
            helper.setCancelable(cancelable);
            helper.setCanceledOnTouchOutside(cancelable);
            if (rootView != null){
                helper.setContentView(rootView);
            }else {
                helper.setContentView(layoutId);
            }
            Window window = helper.getWindow();
            if (null != window){
                int finalWidth = width > 0 ? width : ViewGroup.LayoutParams.MATCH_PARENT;
                int finalHeight = height > 0 ? height : ViewGroup.LayoutParams.WRAP_CONTENT;
                window.setLayout(finalWidth, finalHeight);
                window.setGravity(gravity);
                window.setWindowAnimations(windowAnimations);
            }
            if (null != dismissListener){
                helper.setOnDismissListener(dismissListener);
            }
            if (null != cancelListener){
                helper.setOnCancelListener(cancelListener);
            }
            if (null != showListener){
                helper.setOnShowListener(showListener);
            }
            if (null != dialogListener){
                dialogListener.onDialogInit(helper);
            }
            helper.dialogListener = dialogListener;
            return helper;
        }
    }

    public interface onDialogListener{
        void onDialogInit(Dialog dialog);
    }
}
