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

namespace skyscraper5.Skyscraper.Text.Encodings
{
    [SkyscraperEncoding("dvb-gb-2312-1980")]
    class dvb_gb_2312_1980 : SkyscraperBaseEncoding8
    {
        protected override char[] Decrypt(byte[] preprocessed)
        {
            StringBuilder resultBuilder = new StringBuilder();
            for (int i = 0; i < preprocessed.Length; i++)
            {
                if (preprocessed[i] <= 0x7e)
                {
                    AsciiTable.GetAsciiChar(preprocessed[i], resultBuilder);
                    continue;
                }
                switch (preprocessed[i])
                {
                    
                    default:
                        resultBuilder.Append(DecodeHanzi(preprocessed[i], preprocessed[i + 1]));
                        i++;
                        break;
                }
            }

            return resultBuilder.ToString().ToCharArray();
        }

        //looks good: https://web.archive.org/web/20160303230643/http://cs.nyu.edu/~yusuke/tools/unicode_to_gb2312_or_gbk_table.html
        private char DecodeHanzi(byte leadByte, byte charByte)
        {
            ushort hanzi = leadByte;
            hanzi <<= 8;
            hanzi += charByte;
            switch (hanzi)
            {
                case 0xd049: return '蠭';
                case 0xd64c: return '諰';
                case 0xdc4d: return '躆';
                case 0xdc4e: return '躈';
                default:
                    throw new NotImplementedException(String.Format("{0:X4}", hanzi));
            }
        }
    }
}
