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

namespace WinRT.BxlComponent.Plugin.BxlMpXamarinSDK.Abstractions
{
    internal class MPosByteArrayHelper
    {
        internal static string hexaString(byte[] array)
        {
            string hexaFormatString = (from d in array select d.ToString("X2")).Aggregate((current, next) => current + " " + next);
            hexaFormatString += (" (" + array.Length.ToString() + ")");
            if (hexaFormatString.IndexOf("42 58 4C") >= 0)
                hexaFormatString = hexaFormatString.Replace("42 58 4C", "B X L");
            return hexaFormatString;
        }
        internal static byte[] concat(params byte[][] arrays)
        {
            using (var mem = new System.IO.MemoryStream(arrays.Sum(a => a.Length)))
            {
                foreach (var array in arrays)
                {
                    mem.Write(array, 0, array.Length);
                }
                return mem.ToArray();
            }
        }
        internal static byte[] getSubDataArray(byte[] dataArray, int startIndex, int count)
        {
            return dataArray.Skip(startIndex).Take(count).ToArray();
        }
        internal static List<int> searchBytePattern(byte[] pattern, byte[] bytes)
        {
            List<int> positions = new List<int>();
            int patternLength = pattern.Length;
            int totalLength = bytes.Length;
            byte firstMatchByte = pattern[0];

            for (int i = 0; i < totalLength; i++)
            {
                if (firstMatchByte == bytes[i] && totalLength - i >= patternLength)
                {
                    byte[] match = new byte[patternLength];
                    Array.Copy(bytes, i, match, 0, patternLength);
                    if (match.SequenceEqual<byte>(pattern))
                    {
                        positions.Add(i);
                        i += patternLength - 1;
                    }
                }
            }
            return positions;
        }
    }
}
