using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lkfxdok.LKF.errors
{
    public class ContextError
    {
        string[] _values;
        ContextErrorObjectList _objectList;

        
        internal ContextError (ContextWithData context, ContextErrorObjectList objectList, uint errorIndex, int[] errorFields)
        {
            _objectList = objectList;
            _values = new string[errorFields.Length];
            for (int i = 0; i < errorFields.Length; i++)
            {
                _values[i] = context.CD.GetErrorField(errorIndex, (uint)errorFields[i]);
            }
        }

        internal ContextError(ContextWithData context, uint errorIndex)
        {
            var line = new LKFLine(context.CD.GetError(errorIndex));
            _values = line.Elements;
        }

        public string this[int index]
        {
            get
            {
                if (index >= 0 && index < _values.Length)
                {
                    return _values[index];
                }
                return null;
            }
        }

        public string GetValueForErrorField (int errorField)
        {
            if (_objectList == null)
                return null;
            int position = _objectList.PositionForErrorField(errorField);
            if (position < 0)
                return null;
            return _values[position];
        }

        public string GetValueForErrorField(ContextErrorField errorField)
        {
            if (_objectList == null)
                return null;
            int position = _objectList.PositionForErrorField(errorField);
            if (position < 0)
                return null;
            return _values[position];
        }

        public string GetLine (string separator = "|")
        {
            if (_values != null)
            {
                String.Join(separator, _values);
            }
            return null;
        }

        public override string ToString()
        {
            return GetLine() ?? string.Empty;
        }

    }
}
