package cn.dankal.basiclib.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.StringRes;

import cn.dankal.basiclib.R;
import cn.dankal.basiclib.util.SizeUtils;

public class CustomDialog extends Dialog {
    private Context context;
    private int height, width;
    private boolean cancelTouchout;
    private View view;
    private int gravity;
    private boolean fullscreen;

    private CustomDialog(Builder builder) {
        super(builder.context);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
        gravity = builder.gravity;
        fullscreen = builder.fullscreen;

    }

    @Override
    protected void onStart() {
        if (fullscreen) {
            getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
            getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        }

        super.onStart();


    }

    private CustomDialog(Builder builder, int resStyle) {
        super(builder.context, resStyle);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
        gravity = builder.gravity;
        fullscreen = builder.fullscreen;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(view);

        setCanceledOnTouchOutside(cancelTouchout);
        setCancelable(cancelTouchout);
        Window win = getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();

        if (width != 0) {

            lp.gravity = Gravity.CENTER;
            if (gravity != -1) {
                lp.gravity = gravity;
            }
            if (height != 0) {
                lp.height = height;
            } else {
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            }

            lp.width = width;//宽高可设置具体大小

            win.setAttributes(lp);
        } else if (width == -1) {
            if (gravity != -1) {
                lp.gravity = gravity;
            }
            if (height != 0) {
                lp.height = height;
            } else {
                lp.height = WindowManager.LayoutParams.MATCH_PARENT;
            }

            lp.width = WindowManager.LayoutParams.MATCH_PARENT;//宽高可设置具体大小
            win.setAttributes(lp);
        }


    }


    public void addViewOnclick(@IdRes int viewRes, View.OnClickListener listener) {
        view.findViewById(viewRes).setOnClickListener(listener);

    }

    public void setTextColor(@IdRes int viewRes, @ColorInt int color) {
        ((TextView) view.findViewById(viewRes)).setTextColor(color);

    }

    public void setText(int viewRes, CharSequence message) {
        ((TextView) view.findViewById(viewRes)).setText(message);
    }


    public void setText(int viewRes, @StringRes int message) {
        ((TextView) view.findViewById(viewRes)).setText(message);
    }

    public void setTextSize(int viewRes, int message) {
        ((TextView) view.findViewById(viewRes)).setTextSize(message);
    }


    public void setImageRes(int viewRes, @DrawableRes int imgres) {
        ((ImageView) view.findViewById(viewRes)).setImageResource(imgres);
    }

    public View getView(@IdRes int viewRes) {
        return view.findViewById(viewRes);
    }

    public static final class Builder {

        private Context context;
        private int height, width;
        private boolean cancelTouchout = true;
        private View view;
        private int resStyle = R.style.Dialog;
        private int gravity;
        private int margin;
        private boolean fullscreen = false;


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

        public Builder view(int resView) {
            view = LayoutInflater.from(context).inflate(resView, null);
            return this;
        }

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


        public Builder heightpx(int val) {
            height = val;
            return this;
        }

        public Builder widthpx(int val) {
            width = val;
            return this;
        }

        public Builder heightdp(int val) {
            height = SizeUtils.dp2px(context, val);
            return this;
        }

        public Builder widthdp(int val) {
            width = SizeUtils.dp2px(context, val);
            return this;
        }

        public void setMargindp(int margin) {
            this.margin = SizeUtils.dp2px(context, margin);
        }

        public void setMarginpx(int margin) {
            this.margin = margin;
        }

        public Builder heightDimenRes(int dimenRes) {
            height = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder widthDimenRes(int dimenRes) {
            width = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder style(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        public Builder cancelTouchout(boolean val) {
            cancelTouchout = val;
            return this;
        }

        public Builder addViewOnclick(@IdRes int viewRes, View.OnClickListener listener) {
            view.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }

        public Builder setText(@IdRes int viewRes, String message) {
            if (TextUtils.isEmpty(message)) {
                view.findViewById(viewRes).setVisibility(View.GONE);
            } else {
                ((TextView) view.findViewById(viewRes)).setText(message);
            }

            return this;
        }


        public Builder setTextColor(@IdRes int viewRes, @ColorRes int color) {
            ((TextView) view.findViewById(viewRes)).setTextColor(context.getResources().getColor(color));
            return this;
        }

        public Builder setText(@IdRes int viewRes, @StringRes int message) {
            if (message < 0) {
                view.findViewById(viewRes).setVisibility(View.GONE);
            } else {
                ((TextView) view.findViewById(viewRes)).setText(message);
            }

            return this;
        }

        public boolean isFullscreen() {
            return fullscreen;
        }

        public Builder setFullscreen(boolean fullscreen) {
            this.fullscreen = fullscreen;
            return this;
        }

        public CustomDialog build() {
            if (resStyle != -1) {
                return new CustomDialog(this, resStyle);
            } else {
                return new CustomDialog(this);
            }
        }
    }
}