package com.handset.serialportsensor.utils;

import android.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.handset.serialportsensor.R;
import com.handset.serialportsensor.activity.BaseActivity;

public class ToastHelper {
    public interface OnDialogClickListener {
        void onOKClickListener(View view);

        void onCancelClickListener(View view);
    }

    public interface OnDialogSubmitClickListener {
        void onOKClickListener(View view);
    }

    public static AlertDialog showSubmitDialog(final String title, int icon, String content, int btnBgColor, String ok, BaseActivity context, final OnDialogSubmitClickListener listener) {
        AlertDialog dialog = showSubmitDialogNoShow(title, icon, content, btnBgColor, ok, context, listener);
        context.showDialog(dialog);
        return dialog;
    }

    private static AlertDialog showSubmitDialogNoShow(final String title, int icon, String content, int btnBgColor, String ok, BaseActivity context, final OnDialogSubmitClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.dialog_btl_recognition_error, (null));
        final AlertDialog dialog = builder.create();
        dialog.setView(view);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        tvTitle.setText(title);
        ImageView ivImg = view.findViewById(R.id.iv_img);
        ivImg.setImageResource(icon);
        TextView tvContent = view.findViewById(R.id.tv_content);
        tvContent.setText(content);
        Button btnConfirm = view.findViewById(R.id.btn_confirm);
        btnConfirm.setBackgroundColor(btnBgColor);
        if (!TextUtils.isEmpty(ok)) {
            btnConfirm.setText(ok);
        }
        btnConfirm.setOnClickListener((View view2) -> {
            dialog.dismiss();
            listener.onOKClickListener(view2);
        });
        return dialog;
    }

    public static AlertDialog showChooseDialogNoShow(final String title, int icon, String content, int btnBgColor, String confirm, String cancel, BaseActivity context, final OnDialogClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.dialog_stop_test, (null));
        final AlertDialog dialog = builder.create();
        dialog.setView(view);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        tvTitle.setText(title);
        ImageView ivImg = view.findViewById(R.id.iv_img);
        ivImg.setImageResource(icon);
        TextView tvContent = view.findViewById(R.id.tv_content);
        tvContent.setText(content);
        Button btnConfirm = view.findViewById(R.id.btn_confirm);
        btnConfirm.setBackgroundColor(btnBgColor);
        if (!TextUtils.isEmpty(confirm)) {
            btnConfirm.setText(confirm);
        }
        btnConfirm.setOnClickListener((View view2) -> {
            dialog.dismiss();
            listener.onOKClickListener(view2);
        });
        Button btnCancel = view.findViewById(R.id.btn_cancel);
        if (!TextUtils.isEmpty(cancel)) {
            btnCancel.setText(cancel);
        }
        btnCancel.setOnClickListener((View view3) -> {
            dialog.dismiss();
            listener.onCancelClickListener(view3);
        });
        return dialog;
    }

    public static AlertDialog showTipDialog(BaseActivity context, int imgRes, int tips) {
        return showTipDialog(context, imgRes, tips, (false));
    }

    public static AlertDialog showTipDialogAnimation(BaseActivity context, int imgRes, int tips) {
        return showTipDialog(context, imgRes, tips, (true));
    }

    public static AlertDialog showTipDialogNoShow(BaseActivity context, int imgRes, int tips) {
        return showTipDialogNoShow(context, imgRes, tips, (false));
    }

    private static AlertDialog showTipDialog(BaseActivity context, int imgRes, int tips, boolean showAnimation) {
        AlertDialog dialog = showTipDialogNoShow(context, imgRes, tips, showAnimation);
        context.showDialog(dialog);
        return dialog;
    }

    private static AlertDialog showTipDialogNoShow(BaseActivity context, int imgRes, int tips, boolean showAnimation) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.dialog_tips_layout, (null));
        final AlertDialog dialog = builder.create();
        if (dialog.getWindow() != null) {
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
        dialog.setView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        ImageView ivImg = view.findViewById(R.id.iv_loading);
        if (imgRes != 0) {
            ivImg.setVisibility(View.VISIBLE);
            ivImg.setImageResource(imgRes);
        } else {
            ivImg.setVisibility(View.GONE);
        }
        if (showAnimation) {
            Animation animation = AnimationUtils.loadAnimation(context, R.anim.rotate_anim);
            animation.setInterpolator(new LinearInterpolator());
            ivImg.startAnimation(animation);
        }
        TextView tvTip = view.findViewById(R.id.tv_loading);
        tvTip.setText(tips);
        return dialog;
    }

    public static void showResetSensorDialog(BaseActivity context, final OnDialogClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.dialog_chuchang, (null));
        final AlertDialog dialog = builder.create();
        dialog.setView(view);
        Button btnCancel = view.findViewById(R.id.btn_cancel);
        Button btnConfirm = view.findViewById(R.id.btn_confirm);
        btnCancel.setOnClickListener((View view2) -> {
            dialog.dismiss();
            listener.onCancelClickListener(view2);
        });
        btnConfirm.setOnClickListener((View view3) -> {
            dialog.dismiss();
            listener.onOKClickListener(view3);

        });
        context.showDialog(dialog);
    }

    public static void showSaveSensorDialog(BaseActivity context, final OnDialogClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.dialog_save_alert, (null));
        final AlertDialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.setView(view);
        Button btnCancel = view.findViewById(R.id.btn_cancel);
        Button btnConfirm = view.findViewById(R.id.btn_confirm);
        btnCancel.setOnClickListener((View view2) -> {
            dialog.dismiss();
            listener.onCancelClickListener(view2);
        });
        btnConfirm.setOnClickListener((View view3) -> {
            dialog.dismiss();
            listener.onOKClickListener(view3);
        });
        context.showDialog(dialog);
    }
}
