using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Skyscraper.Text.Encodings;

namespace skyscraper5.Skyscraper.Text
{
    abstract class SkyscraperBaseEncoding16 : SkyscraperBaseEncoding8
    {
        protected abstract override char[] Decrypt(byte[] preprocessed);

        protected override int GetPreProcessLength(byte[] buffer, int index, int count)
        {
            int result = 0;
            byte t, u;
            for (int i = 0; i < count; i += 2)
            {
                t = buffer[i + index];
                if (t == 0xe0)
                {
                    u = buffer[i + index + 1];
                    if (u >= 0x80 && u <= 0x85)
                        continue;       //reserved for future use
                    if (u == 0x86)
                        continue;       //character emphasis on
                    if (u == 0x87)
                        continue;       //character emphasis off
                    if (u >= 0x88 && u <= 0x89)
                        continue;       //reserved for future use
                    if (u == 0x8a)
                        result += 4;    //CRLF
                    if (u >= 0x8b && u <= 0x9f)
                        continue;       //user defined
                }
                result += 2;
            }
            return result;
        }

        protected override byte[] PreProcess(byte[] buffer, int index, int count, int predictedLength)
        {
            byte[] result = new byte[predictedLength];
            int outputOffset = 0;
            byte t, u;
            count /= 2;
            count *= 2;
            for (int i = 0; i < count; i += 2)
            {
                t = buffer[i + index];
                if (t == 0xe0)
                {
                    u = buffer[i + index + 1];
                    if (u >= 0x80 && u <= 0x85)
                        continue;       //reserved for future use
                    if (u == 0x86)
                        continue;       //character emphasis on
                    if (u == 0x87)
                        continue;       //character emphasis off
                    if (u >= 0x88 && u <= 0x89)
                        continue;       //reserved for future use
                    if (u == 0x8a)      
                    {                   //CRLF
                        result[outputOffset++] = 0x00;
                        result[outputOffset++] = 0x10;
                        result[outputOffset++] = 0x00;
                        result[outputOffset++] = 0x13;
                        continue;
                    }
                    if (u >= 0x8b && u <= 0x9f)
                        continue;       //user defined
                }
                else
                {
                    result[outputOffset++] = t;
                    result[outputOffset++] = buffer[i + index + 1];
                }
            }

            return result;
        }
    }
}
