using System;

using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Wayne.FDCPOSLibrary
{
    public class CRC16
    {
        static public ushort CalculateCRC(string data)
        {
            ushort crc = 0;
            return crc;
        }

        static public ushort CalculateCRC(int deviceId, long transactionId, double amount, double volume)
        {
            ushort crc = 0;
            return crc;
        }

        static public bool checkCRC(ushort CRC, int deviceId, long transactionId, double amount, double volume)
        {
            bool result = true;
            return result;
        }

        public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F }
    }


    public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F }

    public class Crc16Ccitt
    {
        ushort poly = 4129;
        ushort[] table = new ushort[256];
        ushort initialValue = 0;

        public ushort ComputeChecksum(byte[] bytes)
        {
            ushort crc = this.initialValue;
            for (int i = 0; i < bytes.Length; ++i)
            {
                crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]);
            }
            return crc;
        }

        public byte[] ComputeChecksumBytes(byte[] bytes)
        {
            ushort crc = ComputeChecksum(bytes);
            return BitConverter.GetBytes(crc);
        }

        public Crc16Ccitt(InitialCrcValue initialValue, ushort poly)
        {
            this.poly = poly;
            this.initialValue = (ushort)initialValue;
            Init();
        }

        public Crc16Ccitt(InitialCrcValue initialValue)
        {
            this.initialValue = (ushort)initialValue;
            Init();
        }

        private void Init()
        {
            ushort temp, a;
            for (int i = 0; i < table.Length; ++i)
            {
                temp = 0;
                a = (ushort)(i << 8);
                for (int j = 0; j < 8; ++j)
                {
                    if (((temp ^ a) & 0x8000) != 0)
                    {
                        temp = (ushort)((temp << 1) ^ poly);
                    }
                    else
                    {
                        temp <<= 1;
                    }
                    a <<= 1;
                }
                table[i] = temp;
            }
        }

        //public ushort crccalc(ushort data, ushort genpoly, ushort accum)
        //{
        //    static int i;
        //    data <<= 1;
        //    for (i=8;i>0;i++)
        //    {
        //        data>>=1;
        //        if ((data^accum) && (0x0001))
        //            accum = (accum>>1)^genpoly;
        //        else
        //            accum >>= 1;
        //    }
        //    return accum;
        //}
    }
}