
// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2023. 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.Extensions.ReferenceCheck.View
{
    public class BackgroundProgress
    {
        public BackgroundProgress(Gtk.EventBox event_box)
        {
            this.Canvas = event_box;
            this.Canvas.ExposeEvent += this.OnExposed;
        }

        private void OnExposed(object o, Gtk.ExposeEventArgs e)
        {
            Gdk.Window win = this.Canvas.GdkWindow;

            if (win == null)
                return;

            win.Clear();

            if (this.Visible)
            {
                Cairo.Context ctx = Gdk.CairoHelper.Create(win);

                ctx.Rectangle(0, 2, this.Canvas.Allocation.Width, this.Canvas.Allocation.Height - 4);
                ctx.SetSourceColor(this.BackgroundColor);
                ctx.Fill();

                double width = this.Canvas.Allocation.Width * this.Progress;
                double height = (this.Canvas.Allocation.Height - 4) / 2.0;


                ctx.Rectangle(0, 2, width, height);
                ctx.SetSourceColor(this.TopColor);
                ctx.Fill();

                ctx.Rectangle(0, height + 2, width, height);
                ctx.SetSourceColor(this.BottomColor);
                ctx.Fill();

                ((IDisposable)ctx.GetTarget()).Dispose();
                ((IDisposable)ctx).Dispose();
            }

            foreach (Gtk.Widget widget in this.Canvas.Children)
            {
                if (widget.Visible)
                    widget.SendExpose(e.Event);
            }
        }

        private double progress = 0;
        public double Progress
        {
            private get
            {
                return this.progress;
            }
            set
            {
                if (value > 1)
                    value = 1;

                if (value < 0)
                    value = 0;

                this.progress = value;
                this.Canvas.QueueDraw();
            }
        }

        private bool visible = false;
        public bool Visible
        {
            private get
            {
                return this.visible;
            }
            set
            {
                this.visible = value;
                this.Canvas.QueueDraw();
            }
        }

        private Gtk.EventBox Canvas { get; set; }
        private Cairo.Color TopColor { get; set; } = new Cairo.Color(0.6, 0.6, 0.9);
        private Cairo.Color BottomColor { get; set; } = new Cairo.Color(0.5, 0.5, 0.9);
        private Cairo.Color BackgroundColor { get; set; } = new Cairo.Color(0.8, 0.8, 0.8);

    }
}
