package com.yyrh.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import cn.yyxx.support.ResUtils;

/**
 * @author #Suyghur.
 * Created on 2021/10/19
 */
public class AutoLauncherDialog extends Dialog implements DialogInterface.OnKeyListener {

    private ImageView ivLoading;
    private Button btnCancel;
    private Animation rotateAnimation;

    public AutoLauncherDialog(Context context) {
        super(context);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(ResUtils.getResId(context, "hnyy_auto_launcher_dialog", "layout"), null);
        setContentView(view);

        WindowManager.LayoutParams attr = getWindow().getAttributes();
        attr.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        attr.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        attr.gravity = Gravity.CENTER;

        ivLoading = findViewById(ResUtils.getResId(context, "hnyy_iv_loading", "id"));
        rotateAnimation = AnimationUtils.loadAnimation(context, ResUtils.getResId(context, "hnyy_rotate_anim", "anim"));
        ivLoading.startAnimation(rotateAnimation);

        btnCancel = findViewById(ResUtils.getResId(context, "hnyy_btn_cancel", "id"));

        setOnKeyListener(this);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        rotateAnimation.cancel();
        ivLoading.clearAnimation();
    }

    public Button getButton() {
        return btnCancel;
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        return keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0;
    }

}
