package com.file.clean.q.view.dialog;

import static com.applovin.sdk.AppLovinSdkUtils.runOnUiThread;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

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

import com.file.clean.q.R;
import com.file.clean.q.model.tool.MemoryUtils;
import com.file.clean.q.view.MainActivity;

/**
 * 作者：马海钊
 * 时间：2025/3/27 17:53
 * 功能：
 */
public class FullscreenDialog {
    private final Context context;
    private Dialog dialog;
    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("#B3000000")));
        dialog.getWindow().setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );

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



        AppCompatButton cl_fullscreen = dialog.findViewById(R.id.clean_up);
        ProgressBar progress = dialog.findViewById(R.id.progressBar);
        TextView phoneMemory = dialog.findViewById(R.id.phone_memory);
        TextView tvBaifenbi = dialog.findViewById(R.id.tv_baifenbi);

        int width = context.getResources().getDimensionPixelSize(R.dimen.main_progress_bar_width); // 宽度
        int height = context.getResources().getDimensionPixelSize(R.dimen.main_progress_bar_height);
        progress.getLayoutParams().width = width;
        progress.getLayoutParams().height = height;
        progress.requestLayout();
        new Thread(){
            @Override
            public void run() {
                super.run();
                // 获取总内存
                String totalMemory = MemoryUtils.getUsedDiskMemory(context);
                // 获取当前使用内存
                String usedMemory = MemoryUtils.getTotalDiskMemory(context);
                String usedDiskMemoryPercent = MemoryUtils.getUsedDiskMemoryPercent(context);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //内存赋值
                        phoneMemory.setText(totalMemory + "/" + usedMemory);
                        tvBaifenbi.setText(usedDiskMemoryPercent);
                        progress.setProgress(Integer.parseInt(usedDiskMemoryPercent));
                    }
                });
            }
        }.start();


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

        // 显示Dialog
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

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


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