package com.offline.music.playermp3.dialog;


import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
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 androidx.viewbinding.ViewBinding;

import com.offline.music.playermp3.R;


public abstract class BaseDialog<T extends ViewBinding> extends DialogFragment {

    protected T vb;


    protected abstract T getViewBinding(LayoutInflater inflater, ViewGroup container);

    protected abstract void initView();

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

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

    }

    @SuppressLint("ResourceType")
    private void init() {

        Dialog dialog = getDialog();
        setCancelable(true);

        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {

                if (isFullScreen()) {
                    window.setStatusBarColor(Color.YELLOW);  // 设置状态栏为透明，或其他颜色
                    // 设置 dialog 占据全屏并延伸到状态栏
//                    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

                    // 允许内容绘制到状态栏
                    window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
                    window.setBackgroundDrawableResource(R.color.color_transparent);
                    window.getDecorView().setPadding(0, 0, 0, 0);
                    WindowManager.LayoutParams attributes = window.getAttributes();
                    attributes.gravity = Gravity.BOTTOM;
                    attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                    attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
                    window.setAttributes(attributes);
                } else {
                    window.setBackgroundDrawableResource(R.color.color_transparent);
                    window.getDecorView().setPadding(0, 0, 0, 0);

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

            }
        }

    }

    public abstract boolean isFullScreen();

    public void closeDialog() {
        dismiss();
    }

}
