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


namespace RogerMfReaderExample.Mifare
{
    //Read/Write to card Mifare Classic 1kB and 4kB
    class Classic
    {
        public enum  KeyTypeBlockAccess { TypeA, TypeB};
        private Byte Sector = 0;
        private Byte Block = 0;
        public Byte KeyType = EpsoParser.MF_CLASSIC_KEY_TYPE_A;
        private Byte[] Key = new Byte[6];

        //Sector 
        public Byte sector
        {
            get
            {
                return Sector;
            }

            set 
            {
                //if (value < 40)             //Mifare Classic 1kB - 16 sectors, 4kB - 40 sectors - library check
                //{
                    Sector = value;
               // }
            }

        }

        //Block
        public Byte block
        {
            get
            {
                return Block;
            }

            set
            {
                // if (value < 16)             //Mifare Classic 4kB - sectors 32-39 have 16 block - library check
               // {
                    Block = value;
               // }
            }

        }

        //Key Type
        public KeyTypeBlockAccess keyType
        {
            get 
            {
                if (KeyType == EpsoParser.MF_CLASSIC_KEY_TYPE_A)
                    return KeyTypeBlockAccess.TypeA;
                else
                    return KeyTypeBlockAccess.TypeB;
            }
            set
            {
                if (value == KeyTypeBlockAccess.TypeA)
                    KeyType = EpsoParser.MF_CLASSIC_KEY_TYPE_A;
                else
                    KeyType = EpsoParser.MF_CLASSIC_KEY_TYPE_B;
            }
        }

        //Key Type string
        public String GetKeyType()
        {
            if (KeyType == EpsoParser.MF_CLASSIC_KEY_TYPE_A)
                return "TypeA";
            else
                return "TypeB";
        }

        //Key
        public Byte[] key
        {
            get
            {
                return Key;
            }
            
            set
            {
                if (value.Length == 6)
                {
                    value.CopyTo(Key, 0);
                }
 
            }
        }

        //Key str
        public String GetKeyStr()
        {
            String keyStr = String.Empty;

            for (int i = 0; i < 6; i++)
                keyStr += Char.ConvertFromUtf32(Key[i]);

            return keyStr;
        }

        //Get default Keys for sectors 
        public Byte[] defaultKey
        {
            get
            {
                Byte[] retKey = new Byte[6] {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

                if (Sector == 0x00)
                {

                    if (KeyType == EpsoParser.MF_CLASSIC_KEY_TYPE_A)
                    {
                        retKey[0] = 0xFF;
                        retKey[1] = 0xFF;
                        retKey[2] = 0xFF;
                        retKey[3] = 0xFF;
                        retKey[4] = 0xFF;
                        retKey[5] = 0xFF;
                    }
                    else
                    {
                        retKey[0] = 0xFF;
                        retKey[1] = 0xFF;
                        retKey[2] = 0xFF;
                        retKey[3] = 0xFF;
                        retKey[4] = 0xFF;
                        retKey[5] = 0xFF;
                    }
                }

                return retKey;
            }

        }

         //Set key and keyType
        public void setKey(Byte[] KeyValue,  KeyTypeBlockAccess KeyType)        
        {

            Array.Copy(KeyValue,key, 0x06);
            keyType = KeyType;

        }

        public Classic()
        {
            Array.Copy(defaultKey,Key, 0x06); 
          
        }

    }

}
