package com.kdanmobile.kdan_others_library_for_android.dialog;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import androidx.appcompat.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.kdanmobile.kdan_others_library_for_android.R;


/**
 * Created by Shan on 2017/4/28.
 */

public class MozbiiConnectDialog {
    private Context context;
    private AlertDialog alertDialog;

    private ImageView connectImage;
    private Button getMozbii;
    private AnimationDrawable animationDrawable;

    private Runnable getMozbiiL;
    private Runnable dismissL;

    public MozbiiConnectDialog(Context context) {
        this.context = context;
        View v = LayoutInflater.from(context).inflate(R.layout.dialog_mozbii_connect, null);
        connectImage = (ImageView) v.findViewById(R.id.connect_image);
        getMozbii = (Button) v.findViewById(R.id.get_mozbii);
        animationDrawable = (AnimationDrawable) connectImage.getDrawable();
        alertDialog = new AlertDialog.Builder(context)
                .setView(v).create();
        getMozbii.setOnClickListener(view -> {
            if (getMozbiiL != null) getMozbiiL.run();
            alertDialog.dismiss();
        });
        alertDialog.setOnDismissListener(dialogInterface -> {
            if (dismissL != null) dismissL.run();
        });
    }

    public void show() {
        if (alertDialog != null) {
            alertDialog.show();
            alertDialog.getWindow().setLayout(
                    context.getResources().getDimensionPixelSize(R.dimen.mozbii_dialog_width),
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            animationDrawable.start();
        }
    }

    public void dismiss() {
        animationDrawable.stop();
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
    }

    public void setOnMozbiiClick(Runnable l) {
        getMozbiiL = l;
    }

    public void setOnDismiss(Runnable l) {
        dismissL = l;
    }


}
