package com.apps.chillscreen.uiView;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

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

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.apps.chillscreen.R;
import com.apps.chillscreen.databinding.FragmentSetPaperBinding;
import com.apps.chillscreen.listener.ShowFragmentListener;

import java.util.Objects;
public class SetPaperFragment extends DialogFragment {

    private FragmentSetPaperBinding vb;
    private ShowFragmentListener 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(ShowFragmentListener listener) {
        this.listener = listener;
    }

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

        vb = FragmentSetPaperBinding.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(v -> {
            if(listener!= null){
                listener.onSelectType(type_home);
                dismiss();

            }

        });
        vb.layoutLock.setOnClickListener(v -> {
            if(listener!= null){
                listener.onSelectType(type_lock);
                dismiss();

            }
        });
        vb.layoutBoth.setOnClickListener(v -> {
            if(listener!= null){
                listener.onSelectType(type_both);
                dismiss();

            }
        });
    }
}