package com.newapp.paperwall.uiview;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

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

import com.newapp.paperwall.R;
import com.newapp.paperwall.databinding.DialogSettingBinding;
import com.newapp.paperwall.listener.DialogListener;

import java.util.Objects;

public class SettingDialog extends DialogFragment {

    private DialogSettingBinding vb;
    private DialogListener listener;

    public static final int type_home =0;
    public static final int type_lock =1;
    public static final int type_both =2;

    public void setListener(DialogListener listener) {
        this.listener = listener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        vb = DialogSettingBinding.inflate(getLayoutInflater());

        init();
        return vb.getRoot();
    }
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);

        // 设置自定义样式
        Objects.requireNonNull(dialog.getWindow()).requestFeature(android.view.Window.FEATURE_NO_TITLE);
        dialog.getWindow().setWindowAnimations(R.style.BottomSheetDialogStyle);

        // 调整对话框布局参数，使其从底部弹起
        dialog.getWindow().setLayout(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().setGravity(android.view.Gravity.BOTTOM);

        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.setCanceledOnTouchOutside(true);
            Window window = dialog.getWindow();
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        }
    }

    private void init(){
        vb.layoutHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!= null){
                    listener.onSelectType(type_home);
                    dismiss();

                }

            }
        });
        vb.layoutLock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!= null){
                    listener.onSelectType(type_lock);
                    dismiss();

                }
            }
        });
        vb.layoutBoth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!= null){
                    listener.onSelectType(type_both);
                    dismiss();

                }
            }
        });
    }
}
