package com.swiftcleaner.chovey.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.widget.FrameLayout;

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

import com.clean.file.b.business.newAdmob.AdmobHelps;
import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.business.admob.AdmobManager;
import com.swiftcleaner.chovey.business.admob.NativeView;
import com.swiftcleaner.chovey.view.activity.EndCleanJunkActivity;

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();
    }

    public CustomScoreDialog(Activity activity) {
        super(activity, R.style.TransparentDialogTheme);
        this.weakActivity = new WeakReference<>(activity);
        setContentView(R.layout.dialog_function_exit_item);

        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.isShowAdNativeBottom(),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;
    }
}