// 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/>.



/*
 * Created by SharpDevelop.
 * User: hoepfner
 * Date: 26.09.2017
 * Time: 09:05
 * 
 * 
 */
using System;
using System.Collections.Generic;



namespace Hiperscan.Extensions.ApoIdentCommon.Scanner
{
        
    public class SeparatedParams
    {
        // constructor
        public SeparatedParams( string avalue)
        {
            fstrList = new List<string>();
            fseparator = ';';
            //fwhitespaces = ' ';
            if( avalue.Length > 0 )
                NewString = avalue;
        }
        
        //Methods
        public int AddEntry( string avalue )
        {
            fstrList.Add( avalue );
            return fstrList.Count;
        }
        
        public int AddEntries( string avalue )
        {
            
            return fstrList.Count;
        }
        
        public int AddList( SeparatedParams value )
        {
            
            return fstrList.Count;
        }
        
        public void Clear()
        {
            fstrList.Clear();
        }
        
        public int Count
        {
            get
            {
                return fstrList.Count;
            }
        }
        
        public string GetEntry( int indx )
        {
            if ( indx < 0 ) return "";
            if ( indx >= fstrList.Count) return "";
            return fstrList[indx];
        }
        
        public void SetEntry( int indx, string value )
        {
            if ( indx < 0 ) return;
            if ( indx >= fstrList.Count) return;
            fstrList[indx] = value;
        }
        
        public string GetEntryName( int indx )
        {
            if ( indx < 0 ) return "";
            if ( indx >= fstrList.Count) return "";
            return LeftStringToMarker( fstrList[indx], '=' );
        }    
        
        public int LastIndx
        {
            get
            {
                return fstrList.Count-1;
            }
        }
        
        public string GetParameter( int indx )
        {
            if ( indx < 0 ) return "";
            if ( indx >= fstrList.Count) return "";
            string res = fstrList[indx];
            int posi = res.IndexOf( "=", StringComparison.CurrentCultureIgnoreCase );
            if( posi < 0 )
            {
                return res;
            }
            else
            {
                return RightStringFromMarker( res, '=' );
            }
        }
        
        public string GetParamByID( string indx )
        {
            if ( fstrList.Count < 1 )
            {
                return "";
            }
            
            foreach (string item in fstrList)
            {
                if( item.IndexOf( indx +"=", StringComparison.CurrentCultureIgnoreCase ) == 0 )
                {
                    return RightStringFromMarker( item, '=' );
                }
            }
            return "";
        }
        
        public void SetParameter( int indx, string value )
        {
            if ( indx < 0 ) 
            {
                fstrList.Insert( 0, value );
                return;
            }
            if ( indx >= fstrList.Count) 
            {
                fstrList.Add( value );
            }
            fstrList[indx] = value;
        }
        
        public void SetParamByID( string indx, string value )
        {
            if ( fstrList.Count < 1 )
            {
                fstrList.Add( indx + "=" + value );
                return;
            }
            
            for( int ii = 0; ii < fstrList.Count; ii++)
            {
                if( fstrList[ii].IndexOf( indx +"=", StringComparison.CurrentCultureIgnoreCase ) == 0 )
                {
                    fstrList[ii] = indx + "=" + value;
                    return;
                }
            }
            
            fstrList.Add( indx +"=" + value );
        }
        
        public Char Separator
        {
            get 
            {
                return fseparator;
            }
            set 
            {
                fseparator = value;
            }
        }
        
        
        public string NewString
        {
            set 
            {
                fstrList.Clear();
                   string[] parts = value.Split( fseparator ); // Call Split method
                foreach (string item in parts)
                {
                    fstrList.Add( item );
                }
            }
        }
        
        public string SumString( Char sep )
        {
            
            if (fstrList.Count < 1)
                return "";
        
            string res = "";
            for( int ii = 0; ii < fstrList.Count; ii++)
            {
                res = res + fstrList[ii];
                if(ii < fstrList.Count-1 )
                    res = res + fseparator;
            }
            return res;
        }
        
        public string ReadSubConfig( string value )
        {
            
            SeparatedParams asp = new SeparatedParams( "");
            
            for( int ii = 0; ii < fstrList.Count; ii++)
            {
                if( fstrList[ii].IndexOf( value, StringComparison.CurrentCultureIgnoreCase ) == 0 )
                    asp.AddEntry( fstrList[ii] );
            }
            return asp.SumString(fseparator);
        }
        
        public int ClearSubConfig( string value )
        {
            
            SeparatedParams asp = new SeparatedParams( "");
            
            int ii = fstrList.Count-1;
            while( ii>=0)
            {
                if( fstrList[ii].IndexOf( value, StringComparison.CurrentCultureIgnoreCase ) == 0 )
                    fstrList.RemoveAt(ii);
                ii--;
            }
            return fstrList.Count;
        }
        
        
        public int ParamByID_AsInt( string indx, int deflt)
        {
            int res = deflt;
            string ss = GetParamByID( indx );
            if( ss == "" ) 
                return deflt;
            bool success = int.TryParse( ss, out res);
            if( success )
                return res;    
            return deflt;
        }
        
        public double ParamByID_AsDbl( string indx, double deflt)
        {
            double res = deflt;
            string ss = GetParamByID( indx );
            if( ss == "" ) 
                return deflt;
            bool success = double.TryParse( ss, out res);
            if( success )
                return res;    
            return deflt;
        }
        
        public bool ParamByID_AsBool( string indx, bool deflt)
        {
            bool res = deflt;
            string ss = GetParamByID( indx );
            if( ss == "" ) 
                return deflt;
            bool success = bool.TryParse( ss, out res);
            if( success )
                return res;    
            return deflt;
        }
        
        
        private string LeftString( string value, int lenth )
        {
            if( lenth < 0 ) return "";
            return value.Substring( 0, lenth-1 );
        }
        
        private string LeftStringToMarker( string value, Char marker )
        {
            return LeftString( value, value.IndexOf( marker ) + 1 );
        }
        
        private string RightString( string value, int start )
        {
            if( start >= value.Length ) return "";
            return value.Substring( start, value.Length-start );
        }
        
        private string RightStringFromMarker( string value, Char marker )
        {
            return RightString( value, value.IndexOf( marker )+1 );
        }
            
        /*        
             constructor Create( Value : string );
        destructor Destroy; override;
    
        function SumString( sep:String =';' ) : String;
        function AddEntry( Value : String ): Integer;
        function AddEntries(Value: String): Integer;
        function AddList( Value : TSeparatedParams ): Integer;
        function ReadSubConfig( const aValue : String ): String;
        procedure Clear;
        function ParamByID_AsInt( indx: String; default : integer = NOVALIDINT ): Integer;
        function ParamByID_AsDbl( indx: String; default : Double = Nan ): Double;
        function ParamByID_AsBool(indx: String; default: Boolean = False ): Boolean;
    
    
        property Count : Integer read GetCount;
        property LastIdx : Integer read GetLastIndx;
        property Additional : String write SetAdditional;
        property NewString  : String write SetNewstring;
        property Entry[indx: Integer] : String read GetEntry write SetParameter;
        property EntryName[indx: Integer] : String read GetEntryName;
        property Parameter[indx: Integer] : String read GetParameter write SetParameter;
        property ParamByID[indx: String] : String read GetParamByID write SetParamByID;
        property Separators : TSysCharset read FSeparators write FSeparators;
    */
        
    
    
    
       // Data
        private List <string> fstrList;
        private Char fseparator;
        //private Char fwhitespaces;
    
    }
}
