// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2020 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.Collections.Generic;
using System.IO;
using System.Reflection;
using Hiperscan.Extensions.ApoIdentCommon.Scanner;
using Hiperscan.Spectroscopy.IO;

namespace ScannerLib
{
    public class PpnList
    {
        private enum PpnListColumn
        {
            Ppn = 0,
            RegisteredName = 1,
            SubstanceId = 2,
            PrimaryDefaultName = 3,
            PrimaryDefaultNameLatin = 4,
            PrimaryDefaultNameEnglish = 5,
            AlternateNames = 6,
            Monography = 7,
            SupplierName = 8,
            Amount = 9,
            SupplierNo = 10,
        }

        public  PpnList(byte[] pzn_data)
        {
            try
            {
                this.Load(pzn_data);
                Console.WriteLine("Pzn list reader found {0} valid entries.", this.Entries.Count);
            }
                catch(Exception ex)
            {
                this.Entries.Clear();
                Console.WriteLine("Cannot initialize pzn list reader: {0}", ex);
            }

        }

        private void Load(byte[] pzn_data)
        {
            this.Entries.Clear();
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(pzn_data, 0, pzn_data.Length);
                ms.Position = 0;

                Dictionary<int, List<string>> raw_data = PlainCSV.Read(ms);
                bool skip = true;
                foreach (List<string> line in raw_data.Values)
                {
                    if (skip == true)
                    {
                        skip = false;
                        continue;
                    }

                    ScannerInfo info = new ScannerInfo();
                    info.Ppn = line[(int)PpnListColumn.Ppn].Trim('[', ']');
                    info.RegisteredName = line[(int)PpnListColumn.RegisteredName];
                    info.SubstanceId = line[(int)PpnListColumn.SubstanceId];
                    info.PrimaryDefaultName = line[(int)PpnListColumn.PrimaryDefaultName];
                    info.PrimaryDefaultNameLatin = line[(int)PpnListColumn.PrimaryDefaultNameLatin];
                    info.PrimaryDefaultNameEnglish = line[(int)PpnListColumn.PrimaryDefaultNameEnglish];
                    info.Monography = line[(int)PpnListColumn.Monography];
                    info.SupplierName = line[(int)PpnListColumn.SupplierName];
                    info.Amount = line[(int)PpnListColumn.Amount];
                    info.SupplierNo = line[(int)PpnListColumn.SupplierNo];

                    string[] tmp = line[(int)PpnListColumn.AlternateNames].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string name in tmp)
                    {
                        string cleaned_name = name.Trim();
                        if (String.IsNullOrEmpty(cleaned_name) == false)
                            info.AlternateNames.Add(cleaned_name);
                    }

                    this.Entries.Add(info.Ppn, info);
                }
            }
        }

        public Dictionary<string, ScannerInfo> Entries
         { get; set; } = new Dictionary<string, ScannerInfo>();
    }
}
