// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2022 Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.



using System;
namespace Hiperscan.GtkExtensions
{
    public class ScrolledWindowSetter
    {
        public ScrolledWindowSetter(Gtk.Container adjustable_container, Gtk.Alignment scalable_container = null)
        {
            this.AdjustableContainer = adjustable_container;
            this.ScalableContainer = scalable_container;
            this.FindMainWindow();
        }

        public void SetScrollBars()
        {
            this.CheckForSettingScrolledWindow();
        }

        private void FindMainWindow()
        {
            Gtk.Widget widget = this.AdjustableContainer;

            for (; ; )
            {
                if (widget.Parent == null)
                {
                    this.MainWindow = widget as Gtk.Window;
                    break;
                }

                widget = widget.Parent;
            }
        }

        private void AddScrolledWindow()
        {
            if (this.ScrolledWindow != null)
                return;

            Gtk.Container parent = this.AdjustableContainer.Parent as Gtk.Container;
            parent.Remove(this.AdjustableContainer);

            Gtk.Viewport viewport = new Gtk.Viewport();
            viewport.ShadowType = Gtk.ShadowType.None;
            viewport.Add(this.AdjustableContainer);

            Gtk.ScrolledWindow scrolled_window = new Gtk.ScrolledWindow();
            scrolled_window.HscrollbarPolicy = Gtk.PolicyType.Never;
            scrolled_window.VscrollbarPolicy = Gtk.PolicyType.Never;
            scrolled_window.Add(viewport);
            scrolled_window.ShadowType = Gtk.ShadowType.None;

            parent.Add(scrolled_window);

            this.ScrolledWindow = scrolled_window;
            this.ScrolledWindow.ShowAll();
        }

        private void CheckForSettingScrolledWindow()
        {
            Gtk.Requisition desired_size = this.MainWindow.SizeRequest();
            int width = (int)((this.MainWindow.Screen.Width / this.MainWindow.Screen.NMonitors) * 0.9);
            int height = (int)((this.MainWindow.Screen.Height) * 0.9);

            this.AddScrolledWindow();
                bool can_maximize = this.ScrolledWindow.HscrollbarPolicy == Gtk.PolicyType.Never &&
                                    this.ScrolledWindow.VscrollbarPolicy == Gtk.PolicyType.Never;

            if (this.ScrolledWindow.HscrollbarPolicy == Gtk.PolicyType.Never)
            {
                if (desired_size.Width > width)
                {
                    this.ScrolledWindow.HscrollbarPolicy = Gtk.PolicyType.Automatic;

                    if (can_maximize && this.MaximizeBySettingScrolledWindow)
                        this.MainWindow.Maximize();

                    if (this.ScalableContainer != null)
                        this.ScalableContainer.Xscale = 1;
                }
            }

            if (this.ScrolledWindow.VscrollbarPolicy == Gtk.PolicyType.Never)
            {
                if (desired_size.Height > height)
                {
                    this.ScrolledWindow.VscrollbarPolicy = Gtk.PolicyType.Automatic;

                    if (can_maximize && this.MaximizeBySettingScrolledWindow)
                        this.MainWindow.Maximize();

                    if (this.ScalableContainer != null)
                        this.ScalableContainer.Yscale = 1;
                }
            }
        }

        private Gtk.Window MainWindow { get; set; }
        private Gtk.Container AdjustableContainer { get; set; }
        private Gtk.Alignment ScalableContainer { get; set; }
        public bool SettingScolledWindowAllowed { private get; set; }
        private Gtk.ScrolledWindow ScrolledWindow { get; set; }
        public bool MaximizeBySettingScrolledWindow { private get; set; }
    }
}
