package com.swiftcleaner.chovey.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.widget.AppCompatButton;
import androidx.cardview.widget.CardView;

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.AppProcessActivity;

public class AppExitDialog extends Dialog {

    private Activity activity;
    private OnDialogClickListener listener;
    private CountDownTimer countDownTimer;
    public interface OnDialogClickListener {
        void onCancel();

        void onSubmit();
    }

    public AppExitDialog(Activity activity) {
        super(activity, R.style.TransparentDialogTheme);
        this.activity = activity;
        setContentView(R.layout.dialog_app_exit_item);

        AppCompatButton dialog_exit = findViewById(R.id.dialog_exit);
        AppCompatButton dialog_experience = findViewById(R.id.dialog_experience);
        ImageView iv_dialog_close = findViewById(R.id.iv_dialog_close);
        CardView cardView = findViewById(R.id.cardview);
        cardView.setCardBackgroundColor(Color.TRANSPARENT);

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

        dialog_experience.setOnClickListener(v -> {
            if (listener != null) {
                listener.onCancel();
            }
            dismiss();
        });
        iv_dialog_close.setOnClickListener(v -> {
            dismiss();
        });

        dialog_exit.setEnabled(false);
        countDownTimer = new CountDownTimer(4000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                long secondsRemaining = millisUntilFinished / 1000;

                // 仅在倒计时剩余2秒和1秒时显示倒计时
                if (secondsRemaining <= 3 && secondsRemaining >= 1) {
                    dialog_exit.setText("Exit" + "(" + secondsRemaining + ")");
                } else {
                    // 倒计时大于2秒时不显示
                    dialog_exit.setText("Exit");
                }
            }

            @Override
            public void onFinish() {
                dialog_exit.setEnabled(true);
                dialog_exit.setText("Exit");
                countDownTimer.cancel();
            }
        }.start();


        setCanceledOnTouchOutside(false);

        NativeView nativeView = findViewById(R.id.fl_ad);
        AdmobManager.INSTANCE.showNativeAd(activity, (nativeAd) -> {
            nativeView.removeAllViews();
            nativeView.setNativeAd(nativeAd, R.layout.layout_ad_native);
            return null;
        }, null, null);
    }

    public void show() {
        super.show();
    }

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