package com.mishiranu.dashchan.ui.gallery;

import android.app.ActionBar;
import android.app.Dialog;
import android.media.AudioManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toolbar;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.mishiranu.dashchan.C;
import com.mishiranu.dashchan.R;
import com.mishiranu.dashchan.ui.ActivityHandler;
import com.mishiranu.dashchan.widget.ViewFactory;

public class GalleryDialog extends Dialog {
	private final Fragment fragment;
	private ViewFactory.ToolbarHolder toolbarHolder;
	private View actionBar;

	public GalleryDialog(Fragment fragment) {
		super(fragment.requireContext(), R.style.Theme_Gallery);
		this.fragment = fragment;
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
	}

	private boolean actionBarAnimationsFixed = false;

	public void setTitleSubtitle(CharSequence title, CharSequence subtitle) {
		if (C.API_LOLLIPOP) {
			toolbarHolder.update(title, subtitle);
		} else {
			setTitle(title);
			getActionBar().setSubtitle(subtitle);
		}
	}

	@Override
	public ActionBar getActionBar() {
		ActionBar actionBar = super.getActionBar();
		if (actionBar != null && !actionBarAnimationsFixed) {
			actionBarAnimationsFixed = true;
			// Action bar animations are enabled only after onStart
			// which is called first time before action bar created
			onStop();
			onStart();
		}
		if (C.API_LOLLIPOP && toolbarHolder == null) {
			Toolbar toolbar = (Toolbar) getActionBarView();
			toolbarHolder = ViewFactory.addToolbarTitle(toolbar);
			setTitle(null);
		}
		return actionBar;
	}

	public View getActionBarView() {
		if (actionBar == null) {
			actionBar = getWindow().getDecorView().findViewById(fragment
					.getResources().getIdentifier("action_bar", "id", "android"));
		}
		return actionBar;
	}

	public interface OnFocusChangeListener {
		void onFocusChange(boolean hasFocus);
	}

	private OnFocusChangeListener onFocusChangeListener;

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);

		if (onFocusChangeListener != null) {
			onFocusChangeListener.onFocusChange(hasFocus);
		}
	}

	public void setOnFocusChangeListener(OnFocusChangeListener listener) {
		this.onFocusChangeListener = listener;
	}

	@Override
	public void onBackPressed() {
		if (!(fragment instanceof ActivityHandler) || !((ActivityHandler) fragment).onBackPressed()) {
			super.onBackPressed();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(@NonNull Menu menu) {
		fragment.onCreateOptionsMenu(menu, fragment.requireActivity().getMenuInflater());
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(@NonNull Menu menu) {
		fragment.onPrepareOptionsMenu(menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, @NonNull MenuItem item) {
		if (featureId == Window.FEATURE_OPTIONS_PANEL) {
			return onOptionsItemSelected(item);
		} else {
			return false;
		}
	}

	@Override
	public boolean onOptionsItemSelected(@NonNull MenuItem item) {
		return fragment.onOptionsItemSelected(item);
	}
}

@@ -909,16 +909,217 @@
 RATED);%0A
+%09%09WindowManager.LayoutParams layoutParams = getWindow().getAttributes();%0A%09%09layoutParams.setTitle(getContext().getPackageName() + %22/%22 + getClass().getName());%0A%09%09getWindow().setAttributes(layoutParams);%0A
 %09%09setVol
@@ -1937,21 +1937,221 @@
 %0A%09%09%09
-setTitle(null
+WindowManager.LayoutParams layoutParams = getWindow().getAttributes();%0A%09%09%09CharSequence title = layoutParams.getTitle();%0A%09%09%09setTitle(null);%0A%09%09%09layoutParams.setTitle(title);%0A%09%09%09getWindow().setAttributes(layoutParams
 );%0A%09
@@ -3024,32 +3024,383 @@
 ssed();%0A%09%09%7D%0A%09%7D%0A%0A
+%09@Override%0A%09public boolean onPreparePanel(int featureId, View view, @NonNull Menu menu) %7B%0A%09%09super.onPreparePanel(featureId, view, menu);%0A%09%09// Dialog removes the menu completely if menu becomes once empty.%0A%09%09// This logic is different from Activity and causes unwanted behavior.%0A%09%09// Return %22true%22 here to always keep menu existing.%0A%09%09return true;%0A%09%7D%0A%0A
 %09@Override%0A%09publ

Fix GalleryDialog menu behavior