package com.key.coolkeyboard.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.key.coolkeyboard.CoolKeyboardSkin;
import com.key.coolkeyboard.R;
import com.key.coolkeyboard.activity.MainActivity;
import com.key.coolkeyboard.callback.RecommendDialogCallback;
import com.key.coolkeyboard.callback.StepDialogCallback;
import com.key.coolkeyboard.databinding.DialogBinding;
import com.key.coolkeyboard.databinding.ToastDialogBinding;
import com.key.coolkeyboard.tool.Mytool;
import com.key.coolkeyboard.topon.AdManager;
import com.key.coolkeyboard.topon.onActionListener;

public class RecommendDialogFragment extends DialogFragment {

    private Activity mActivity;
    private ToastDialogBinding vb;


    private RecommendDialogCallback callback;

    private String im;

    public void setIm(String im) {
        this.im = im;
    }

    public RecommendDialogFragment(Activity activity, RecommendDialogCallback stepDialogCallback) {
        mActivity = activity;
        callback = stepDialogCallback;

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        vb = ToastDialogBinding.inflate(inflater, container, false);
        init();
        return vb.getRoot();
    }

    private void init() {

        Dialog dialog = getDialog();
        setCancelable(false);
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                window.setBackgroundDrawableResource(R.color.transparent);
                window.getDecorView().setPadding(0, 0, 0, 0);

                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.gravity = Gravity.CENTER;
                attributes.width = WindowManager.LayoutParams.WRAP_CONTENT;
                attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(attributes);
            }
        }


        Glide.with(CoolKeyboardSkin.app)
                .asDrawable()
                .load(im)
                .placeholder(R.mipmap.logo)
                .into(vb.toastDialogImage);


        vb.toastDialogApply.setOnClickListener(v -> {
            if (callback != null) {
                callback.onTryNow();
            }

        });
        vb.toastDialogCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    public void closeDialog(){
        dismiss();
    }


    public static RecommendDialogFragment newInstance(Activity activity, RecommendDialogCallback stepDialogCallback) {

        RecommendDialogFragment recommendDialogFragment = new RecommendDialogFragment(activity, stepDialogCallback);
        return recommendDialogFragment;
    }
}
