package com.tool.applockpro.fragment;


import android.app.Dialog;
import android.os.Bundle;
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 androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.tool.applockpro.R;
import com.tool.applockpro.databinding.DialogOkBinding;


public class OkDialogFragment extends DialogFragment {


    private DialogOkBinding vb;


    private String msgStr;


    public OkDialogFragment(String msg) {
        msgStr = msg;
    }

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

    }


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

    public void updateContent(String msg) {
        msgStr = msg;
        if (msgStr != null && !msgStr.isEmpty()) {
            if (vb != null) {
                vb.textContent.setText(msgStr);
            }
        }
    }

    private void init() {

        Dialog dialog = getDialog();
        setCancelable(true);
        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.MATCH_PARENT;
                attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(attributes);
            }
        }
        vb.textContent.setText(msgStr);

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

    }

    public void closeDialog() {
        dismiss();
    }


    public static OkDialogFragment newInstance(String msg) {

        OkDialogFragment okDialogFragment = new OkDialogFragment(msg);
        return okDialogFragment;
    }
}
