// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2021 Torsten Schache, 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;
using System.IO;
using System.Text;
using Glade;
using Hiperscan.Unix;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class IdentServerClient
    {
        public class LicenseDialog
        {
            [Widget] Gtk.Window license_window = null;
            [Widget] Gtk.TextView license_textview = null;

            public static LicenseDialog Start(Gtk.Window parent, Configurations config)
            {
                return new LicenseDialog(parent, config);
            }

            private LicenseDialog(Gtk.Window parent, Configurations config)
            {

                Glade.XML gui = new Glade.XML(null, "ApoIdent.Resources.Glade.FinderWindow.glade", "license_window", "Hiperscan");
                gui.Autoconnect(this);

                this.license_window.SetWmclass(Plugin.name.ToLower(),
                                               Plugin.name.ToLower());

                this.license_window.TransientFor = parent;

                string license_text = string.Empty;
                try
                {
                    if (IdentServerClient.CanConnectToIdentModule == false)
                        throw new Exception();

                    if (config.IdentModuleSettings.UpdateIMLicenceText == false)
                    {
                        using (StreamReader sr = new StreamReader(FilesAndDirs.IMLicenseFile, Encoding.UTF8))
                        {
                            license_text = sr.ReadToEnd();
                        }
                    }

                }
                catch (Exception)
                {
                    license_text = Catalog.GetString("License is currently not available.");
                }

                this.license_textview.Buffer.Text = license_text;

                this.license_window.ShowAll();
                this.license_window.WidthRequest = this.license_window.Allocation.Width + 50;
            }

            private void OnLicenseCloseButtonClicked(object sender, EventArgs e)
            {
                this.license_window.Destroy();
            }
        }
    }
}
