// 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;
using Gtk;
using Hiperscan.Common.I18n;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        private void InitPharmacyOperatorCompletion()
        {
            ListStore store = new ListStore(typeof(String));
            this.PharmacyOperatorEntry.GtkEntry.Completion = new EntryCompletion();
            this.PharmacyOperatorEntry.GtkEntry.Completion.InlineSelection = false;
            this.PharmacyOperatorEntry.GtkEntry.Completion.Model = store;
            this.PharmacyOperatorEntry.GtkEntry.Completion.TextColumn = 0;
            this.PharmacyOperatorEntry.GtkEntry.Completion.MatchFunc = this.OnMatchEntryCompletion;
        }

        private void InitSpecimenProducerCompletion()
        {
            ListStore store = new ListStore(typeof(String));
            this.Control.GuiSettings.UpdateProducerCompletion(CatalogLocale.Language);
           
            this.SpecimenProducerEnntry.GtkEntry.Completion = new EntryCompletion();
            this.SpecimenProducerEnntry.GtkEntry.Completion.InlineSelection = false;
            this.SpecimenProducerEnntry.GtkEntry.Completion.Model = store;
            this.SpecimenProducerEnntry.GtkEntry.Completion.TextColumn = 0;
            this.SpecimenProducerEnntry.GtkEntry.Completion.MatchFunc = this.OnMatchEntryCompletion;
        }

        private bool OnMatchEntryCompletion(EntryCompletion completion, string key, TreeIter iter)
        {
            if (key == " ")
                return true;

            key = key.ToLower().Trim();
            key = key.Replace(",", String.Empty);
            key = key.Replace(";", String.Empty);
            key = key.Replace("-", String.Empty);
            key = key.Replace(".", String.Empty);
            key = key.Replace("\u0061\u0308", "\u00e4");
            key = key.Replace("\u006f\u0308", "\u00f6");
            key = key.Replace("\u0075\u0308", "\u00fc");

            TreeModel store = completion.Model;
            string line = (((string)store.GetValue(iter, completion.TextColumn)) ?? String.Empty).Trim().ToLower();
            line = line.Replace(",", String.Empty);
            line = line.Replace(";", String.Empty);
            line = line.Replace("-", String.Empty);
            line = line.Replace(".", String.Empty);
            line = line.Replace("ß", "ss");

            return line.StartsWith(key, StringComparison.InvariantCulture);
        }




        private void InitSubstanceSearchEntry()
        {
            Console.WriteLine("Init auto completion substance search entry ...");

            Gtk.EntryCompletion completion = new EntryCompletion();
            completion.InlineSelection = false;

            CellRendererPixbuf pixpuf_cell = new CellRendererPixbuf();
            pixpuf_cell.Xpad = 5;

            completion.PackStart(pixpuf_cell, false);
            completion.AddAttribute(pixpuf_cell, "pixbuf", 0);

            completion.TextColumn = 1;
            completion.MatchFunc = this.OnMatchSubstance;
            completion.MatchSelected += this.OnSubstanceMatchSelected;

            Gtk.ListStore store = new ListStore(typeof(Gdk.Pixbuf), typeof(string));
            completion.Model = store;

            this.SubstanceSearchEntry.GtkEntry.Completion = completion;
        }

        private bool OnMatchSubstance(EntryCompletion completion, string key, TreeIter iter)
        {
            if (key == " ")
                return true;

            key = key.ToLower().Trim();
            key = key.Replace(",", String.Empty);
            key = key.Replace(";", String.Empty);
            key = key.Replace("-", String.Empty);
            key = key.Replace(".", String.Empty);
            key = key.Replace("\u0061\u0308", "\u00e4");
            key = key.Replace("\u006f\u0308", "\u00f6");
            key = key.Replace("\u0075\u0308", "\u00fc");

            TreeModel store = completion.Model;
            string line = (((string)store.GetValue(iter, completion.TextColumn)) ?? String.Empty).Trim().ToLower();
            line = line.Replace(",", String.Empty);
            line = line.Replace(";", String.Empty);
            line = line.Replace("-", String.Empty);
            line = line.Replace(".", String.Empty);
            line = line.Replace("ß", "ss");

            if (key.Length < 4)
            {
                if (line.StartsWith(key, StringComparison.InvariantCulture) == true)
                    return true;

                string[] line_data = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                int count = 0;
                foreach (string sub_line in line_data)
                {
                    if (sub_line.StartsWith(key, StringComparison.InvariantCulture) == true)
                        count++;
                }
                if (count == line_data.Length)
                    return true;
            }
            else
            {
                if (line.Contains(key) == true)
                    return true;
            }

            return false;
        }
    }
}
