package epson.print.widgets;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import epson.print.R;

public class CustomTitleAlertDialogBuilder extends AlertDialog.Builder {
    private static final int TITLES_COLOR = 2131034138;
    private static final int TITLES_LINES = 5;
    ViewGroup titleView = null;

    public CustomTitleAlertDialogBuilder(Context context) {
        super(context);
        setUpCustomTitle();
    }

    public CustomTitleAlertDialogBuilder(Context context, int i) {
        super(context, i);
        setUpCustomTitle();
    }

    @NonNull
    public AlertDialog.Builder setTitle(CharSequence charSequence) {
        AlertDialog.Builder title = super.setTitle(charSequence);
        setCustomTitle(charSequence);
        return title;
    }

    @NonNull
    public AlertDialog.Builder setTitle(int i) {
        AlertDialog.Builder title = super.setTitle(i);
        setCustomTitle(getContext().getText(i));
        return title;
    }

    @NonNull
    public AlertDialog.Builder setIcon(Drawable drawable) {
        AlertDialog.Builder icon = super.setIcon(drawable);
        setCustomTitleIcon(drawable);
        return icon;
    }

    @NonNull
    public AlertDialog.Builder setIcon(int i) {
        AlertDialog.Builder icon = super.setIcon(i);
        setCustomTitleIcon(getContext().getResources().getDrawable(i));
        return icon;
    }

    private void setUpCustomTitle() {
        this.titleView = (ViewGroup) ((LayoutInflater) getContext().getSystemService("layout_inflater")).inflate(R.layout.alert_dialog_title, (ViewGroup) null);
        ViewGroup viewGroup = this.titleView;
        if (viewGroup != null) {
            setCustomTitle(viewGroup);
            TextView textView = (TextView) this.titleView.findViewById(R.id.alertTitle);
            if (textView != null) {
                textView.setMaxLines(5);
                textView.setTextColor(getContext().getResources().getColor(R.color.all_black));
            }
        }
    }

    public void setCustomTitle(CharSequence charSequence) {
        TextView textView;
        ViewGroup viewGroup = this.titleView;
        if (viewGroup != null && (textView = (TextView) viewGroup.findViewById(R.id.alertTitle)) != null) {
            textView.setText(charSequence);
        }
    }

    public void setCustomTitleIcon(Drawable drawable) {
        ImageView imageView;
        ViewGroup viewGroup = this.titleView;
        if (viewGroup != null && (imageView = (ImageView) viewGroup.findViewById(R.id.icon)) != null) {
            imageView.setImageDrawable(drawable);
        }
    }
}
