package com.photo.keyboard.themes.pager;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;

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.Target;
import com.photo.keyboard.themes.databinding.ActivitySuccessBinding;
import com.photo.keyboard.themes.manager.Utils;
import com.photo.keyboard.themes.manager.ConstValue;
import com.photo.keyboard.themes.topon.AdManager;

import jp.wasabeef.glide.transformations.BlurTransformation;

public class SuccessActivity extends BaseActivity<ActivitySuccessBinding> {
    private ActivitySuccessBinding vb;
    public static String key_name = "key_name";
    private int mPreviousKeyboardHeight = -1;

    @NonNull
    @Override
    public ActivitySuccessBinding returnViewbinding() {
        Utils.INSTANCE.initFull(this, false);
        return ActivitySuccessBinding.inflate(getLayoutInflater());
    }

    @Override
    public void onInit() {
        vb = getVb();
        AdManager.loadAllAd();
        String stringExtra = getIntent().getStringExtra(key_name);
        vb.title.setText(stringExtra);
        Utils.INSTANCE.setFont(vb.title);

        String curPath = Utils.INSTANCE.getCurPath();
        vb.idBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (curPath == null) {
            return;
        }
        String stringBuilder = curPath + ConstValue.INSTANCE.getXx_path() +
                ConstValue.INSTANCE.getPreview_bg();
        Drawable bgDraw = Utils.INSTANCE.getbgic(this, stringBuilder);
        if (bgDraw != null) {

            Glide.with(this)
                    .load(bgDraw)
                    .apply(bitmapTransform(new BlurTransformation(15, 3))) // 设置模糊半径和模糊采样
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                            vb.relayout.setBackground(resource);
                            return false;
                        }
                    })
                    .preload();

        }
        keyboardheight();
        vb.et.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

    }


    private void keyboardheight() {
        final View rootView = getWindow().getDecorView();
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {


            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                rootView.getWindowVisibleDisplayFrame(r);
                int screenHeight = rootView.getRootView().getHeight();
                int keypadHeight = screenHeight - r.bottom;

                if (keypadHeight != mPreviousKeyboardHeight) {
                    // 软键盘高度发生变化时的处理逻辑
                    Log.d("KeyboardSize", "Keyboard Height is: " + keypadHeight);
                    if (mPreviousKeyboardHeight < keypadHeight) {
                        Log.d("KeyboardSize", " 11111111111");
                        mPreviousKeyboardHeight = keypadHeight;
                        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) vb.et.getLayoutParams();
                        params.bottomMargin = mPreviousKeyboardHeight;
                        vb.et.setLayoutParams(params);
                    }

                }
            }
        });
    }
}