package com.swiftcleaner.chovey.view.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.swiftcleaner.chovey.R;

public class CustomBottomSheetDialog extends BottomSheetDialogFragment {
    public interface OnOpenSettingClickListener {
        void onOpenSettingClick(boolean isSwitchChecked);
    }

    private OnOpenSettingClickListener openSettingClickListener;

    public void setOnOpenSettingClickListener(OnOpenSettingClickListener listener) {
        this.openSettingClickListener = listener;
    }

    public CustomBottomSheetDialog() {
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_bottom_large_file, container, false);
        AppCompatButton open_setting = view.findViewById(R.id.open_setting);
        Switch switch_id = view.findViewById(R.id.switch_id);

        open_setting.setOnClickListener(v -> {
            if (openSettingClickListener != null) {
                openSettingClickListener.onOpenSettingClick(switch_id.isChecked());
            }
        });
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
        dismiss();
    }

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

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
        dialog.setOnShowListener(dialog1 -> {
            BottomSheetDialog d = (BottomSheetDialog) dialog1;
            View bottomSheetInternal = d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
            if (bottomSheetInternal != null) {
                BottomSheetBehavior<?> behavior = BottomSheetBehavior.from(bottomSheetInternal);
                behavior.setState(BottomSheetBehavior.STATE_EXPANDED); // 展开状态
                behavior.setHideable(false); // 禁止隐藏
                behavior.setDraggable(false); // 禁止拖动
            }
        });
        return dialog;
    }
}
