package com.swiftcleaner.chovey.view.dialog;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.airbnb.lottie.LottieAnimationView;
import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.view.MainActivity;
import com.swiftcleaner.chovey.view.activity.CleanJunkActivity;

/**
 * 作者：马海钊
 * 时间：2025/2/6 16:47
 * 功能：
 */
public class FullscreenDialog {

    private final Context context;
    private Dialog dialog;
    private LottieAnimationView lottieView;
    private OnDialogDismissListener dismissListener;

    public FullscreenDialog(@NonNull Context context) {
        this.context = context;
    }

    // 设置回调接口
    public void setOnDialogDismissListener(OnDialogDismissListener listener) {
        this.dismissListener = listener;
    }

    public void show() {
        dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.fullscreen_dialog);

        // 设置Dialog窗口属性为全屏
//        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#99000000")));
        dialog.getWindow().setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );

        // 设置点击外部区域不会关闭Dialog
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);

        // 设置点击关闭按钮时关闭Dialog
        AppCompatButton button = dialog.findViewById(R.id.fullscreen_button);
        ConstraintLayout cl_fullscreen = dialog.findViewById(R.id.cl_fullscreen);

        // 设置点击事件
        button.setOnClickListener(v -> handleButtonClick());
        cl_fullscreen.setOnClickListener(v -> handleButtonClick());

        lottieView = dialog.findViewById(R.id.lottie_animattion_view1);
        lottieView.setAnimation(R.raw.cleaner_junk_found_animation); // 替换为你的动画文件名
        lottieView.playAnimation(); // 开始播放动画
        
        // 显示Dialog
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.setOnDismissListener(null); // 移除监听器，避免重复调用
            dialog.setOnCancelListener(null); // 移除取消监听器
            dialog.dismiss();

            if (lottieView != null) {
                lottieView.cancelAnimation(); // 停止动画
                lottieView.clearAnimation(); // 清除动画
                lottieView.setImageDrawable(null); // 清除图像
                lottieView.destroyDrawingCache(); // 销毁缓存
            }
        }
    }

    private void handleButtonClick() {
        if (dismissListener != null) {
            dismissListener.onSuccButton();
        }
    }


    // 定义回调接口
    public interface OnDialogDismissListener {
        void onSuccButton();
    }
}