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

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatButton;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;

import com.file.clean.q.business.admob.AdmobManager;
import com.file.clean.q.business.admob.NativeView;
import com.file.clean.q.R;
import com.file.clean.q.business.newAdmob.AdmobHelps;

import java.lang.ref.WeakReference;


public class CustomScoreDialog extends Dialog {

    private WeakReference<Activity> weakActivity;
    private OnDialogClickListener listener;
    private boolean isDialogDestroyed = false;

    public interface OnDialogClickListener {
        void onCancel();

        void onSubmit();
    }

    // 修改构造函数，添加 title 参数
    public CustomScoreDialog(Activity activity,int num) {
        super(activity, R.style.TransparentDialogTheme);
        this.weakActivity = new WeakReference<>(activity);
        setContentView(R.layout.dialog_function_exit_item);

        // 获取标题控件并设置标题
        TextView title = findViewById(R.id.tv_title);
        TextView tv_name = findViewById(R.id.tv_name);
        switch (num){
            case 1:
                //垃圾清理
                title.setText("Exit Clean Junk?");
                tv_name.setText("Exit Junk Cleaner? Unremoved junkhles may be taking up space.");
                break;
            case 2:
                //电池
                title.setText("Exit Battery Info?");
                tv_name.setText("You are currently viewing the detailed battery info. If you exit, you will not be able to continue monitoring the battery data in real time. Are you sure you want to return?");
                break;
            case 3:
                //大文件
                title.setText("Exit Large File Cleaner?");
                tv_name.setText("The large file cleaner task is in progress. Exiting at this time will interrupt the scanning and cleaning operations. Are you sure you want to exit?");
                break;
            case 6:
                //截图
                title.setText("Exit Screenshot Cleaner?");
                tv_name.setText("Your screenshot cleaning work hasn't been completed yet. Are you sure you want to abandon the current cleaning and return?");
                break;
            case 7:
                //相似图片
                title.setText("Exit Similar Photos Cleaner?");
                tv_name.setText("The similar picture cleaning has not been finished yet. Exiting may lead to incomplete cleaning. Are you sure you want to exit this function page?");

            default:
                title.setText("Exit Clean Junk?");
                tv_name.setText("Exit Junk Cleaner? Unremoved junkhles may be taking up space.");
                break;
        }
        AppCompatButton dialog_cancel = findViewById(R.id.dialog_cancel);
        AppCompatButton dialog_ok = findViewById(R.id.dialog_ok);
        CardView cardView = findViewById(R.id.cardview);
        cardView.setCardBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.transparent));

        FrameLayout nativeView = findViewById(R.id.fl_ad);
        if (!isDialogDestroyed){
            AdmobHelps.INSTANCE.showNativeAd(activity,nativeView,R.layout.layout_ad_native_small,AdmobHelps.INSTANCE.isShowAdNativeExitFunction(),false);
        }

        dialog_cancel.setOnClickListener(v -> {
            if (listener != null) {
                activity.runOnUiThread(() -> listener.onCancel());
            }
            dismiss();
        });

        dialog_ok.setOnClickListener(v -> {
            if (listener != null) {
                activity.runOnUiThread(() -> listener.onSubmit());
            }
            dismiss();
        });

        setCanceledOnTouchOutside(false);
    }

    @Override
    public void dismiss() {
        isDialogDestroyed = true;
        super.dismiss();
    }

    public void show() {
        Activity activity = weakActivity.get();
        if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
            super.show();
        }
    }

    public void setOnDialogClickListener(OnDialogClickListener listener) {
        this.listener = listener;
    }
}