package com.swiftcleaner.chovey.view.dialog;

import android.app.Activity;
import android.app.Dialog;
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.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.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();
    }

    public CustomScoreDialog(Activity activity, int type) {
        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);
        TextView tvExitTitle = findViewById(R.id.tv_exit_title);
        TextView tvExitContent = findViewById(R.id.tv_exit_content);
        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.isShowAdExitDialogNative(),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();
        });
        setExitText(tvExitTitle, tvExitContent, type);
        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;
    }

    private void setExitText(TextView titleTextView, TextView contentTextView, int type){
        int resTitleId = R.string.exit_title_0;
        switch (type){
            case 1: resTitleId = R.string.exit_title_1;
                break;
            case 2: resTitleId = R.string.exit_title_2;
                break;
            case 3: resTitleId = R.string.exit_title_3;
                break;
            case 4: resTitleId = R.string.exit_title_4;
                break;
            case 5: resTitleId = R.string.exit_title_5;
                break;
            case 6: resTitleId = R.string.exit_title_6;
                break;
            default: break;
        }
        titleTextView.setText(resTitleId);
        int resContentId = R.string.exit_content_0;
        switch (type){
            case 1: resContentId = R.string.exit_content_1;
                break;
            case 2: resContentId = R.string.exit_content_2;
                break;
            case 3: resContentId = R.string.exit_content_3;
                break;
            case 4: resContentId = R.string.exit_content_4;
                break;
            case 5: resContentId = R.string.exit_content_5;
                break;
            case 6: resContentId = R.string.exit_content_6;
                break;
            default: break;
        }
        contentTextView.setText(resContentId);
    }
}