package com.simperium.android;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.simperium.R;

public class LoginBottomSheetDialogFragment extends SimperiumBottomSheetDialogFragment {
    public static final String TAG = LoginBottomSheetDialogFragment.class.getSimpleName();

    private LoginSheetListener mLoginSheetListener;

    public LoginBottomSheetDialogFragment(@NonNull final LoginSheetListener loginSheetListener) {
        mLoginSheetListener = loginSheetListener;
    }

    @Override
    public void onCancel(@NonNull DialogInterface dialog) {
        mLoginSheetListener.onLoginSheetCanceled();
        super.onCancel(dialog);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View layout = inflater.inflate(R.layout.sheet_login, null);

        AppCompatButton buttonEmail = layout.findViewById(R.id.button_email);
        buttonEmail.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mLoginSheetListener.onLoginSheetEmailClicked();
                }
            }
        );

        AppCompatButton buttonOther = layout.findViewById(R.id.button_other);
        buttonOther.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mLoginSheetListener.onLoginSheetOtherClicked();
                }
            }
        );

        if (getDialog() != null) {
            getDialog().setContentView(layout);

            // Set peek height to full height of view to avoid buttons being off screen when
            // bottom sheet is shown with small screen height (e.g. landscape orientation).
            final BottomSheetBehavior behavior = BottomSheetBehavior.from((View) layout.getParent());
            getDialog().setOnShowListener(
                new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        behavior.setPeekHeight(layout.getHeight());
                    }
                }
            );
        }

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public interface LoginSheetListener {
        void onLoginSheetCanceled();
        void onLoginSheetEmailClicked();
        void onLoginSheetOtherClicked();
    }
}

@@ -183,16 +183,51 @@
 ewGroup;
+%0Aimport android.widget.FrameLayout;
 %0A%0Aimport
@@ -412,16 +412,82 @@
 havior;%0A
+import com.google.android.material.bottomsheet.BottomSheetDialog;%0A
 import c
@@ -2122,16 +2122,42 @@
  of view
+ (i.e. set STATE_EXPANDED)
  to avoi
@@ -2165,16 +2165,31 @@
  buttons
+%0A            //
  being o
@@ -2202,31 +2202,16 @@
 een when
-%0A            //
  bottom 
@@ -2228,63 +2228,186 @@
 hown
- with small screen height (e.g. landscape orientation).
+.%0A            getDialog().setOnShowListener(new DialogInterface.OnShowListener() %7B%0A                @Override%0A                public void onShow(DialogInterface dialogInterface) %7B
 %0A   
@@ -2415,21 +2415,23 @@
         
-final
+       
  BottomS
@@ -2438,28 +2438,36 @@
 heet
-Behavior behavior
+Dialog bottomSheetDialog
  = 
+(
 Bott
@@ -2477,242 +2477,385 @@
 heet
-Behavior.from((View) layout.getParent());%0A            getDialog().setOnShowListener(%0A                new DialogInterface.OnShowListener() %7B%0A                    @Override%0A                    public void onShow(DialogInterface dialog) %7B
+Dialog) dialogInterface;%0A                    FrameLayout bottomSheet = bottomSheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet);%0A%0A                    if (bottomSheet != null) %7B%0A                        BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);%0A                        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
 %0A   
@@ -2891,37 +2891,26 @@
 .set
-PeekHeight(layout.getHeight()
+SkipCollapsed(true
 );%0A 
@@ -2940,32 +2940,32 @@
               %7D%0A
-
             );%0A 
@@ -2960,16 +2960,17 @@
         
+%7D
 );%0A     

Update peek height and collapsed state in login bottom sheet dialog fragment