using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PCSC;
using PCSC.Iso7816;

namespace Post
{
    public partial class Form1 : Form
    {
        private static readonly byte[] _data_to_write = { 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
        const byte MSB = 0x00;
        const byte LSB = 0x08; //BLOK
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private string ChooseReader(string[] readerNames)
        {

            
            //Console.WriteLine(new string('=', 79));
            //Console.WriteLine("WARNING!! This will overwrite data in MSB {0:X2} LSB {1:X2} using the default key.", MSB, LSB);
            //Console.WriteLine(new string('=', 79));

            // Show available readers.
            //Console.WriteLine("Available readers: ");
            for (var i = 0; i < readerNames.Length; i++)
            {
                //Console.WriteLine("[" + i + "] " + readerNames[i]);
            }

            // Ask the user which one to choose.
            //Console.Write("Which reader has an inserted Mifare 1k/4k card? ");

            var line = "0";
            int choice;

            if (int.TryParse(line, out choice) && (choice >= 0) && (choice <= readerNames.Length))
            {
                return readerNames[choice];
            }

            //Console.WriteLine("An invalid number has been entered.");
            //Console.ReadKey();
            return null;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            // Establish Smartcard context
            using (var context = new SCardContext())
            {
                context.Establish(SCardScope.System);

                var readerNames = context.GetReaders();
                if (readerNames == null || readerNames.Length < 1)
                {
                    Console.WriteLine("You need at least one reader in order to run this example.");
                    Console.ReadKey();
                    return;
                }

                var readerName = ChooseReader(readerNames);
                if (readerName == null)
                {
                    return;
                }

                using (var isoReader = new IsoReader(context, readerName, SCardShareMode.Shared, SCardProtocol.Any, false))
                {
                    var card = new MifareCard(isoReader);
                   

                    var loadKeySuccessful = card.LoadKey(
                        KeyStructure.VolatileMemory,
                        0x00, // first key slot
                        new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } // key
                    );

                    if (!loadKeySuccessful)
                    {
                        throw new Exception("LOAD KEY failed.");
                    }

                    var authSuccessful = card.Authenticate(MSB, LSB, KeyType.KeyA, 0x00);
                    if (!authSuccessful)
                    {
                        throw new Exception("AUTHENTICATE failed.");
                    }

                    var result = card.ReadBinary(MSB, LSB, 16);

                    for (int i = 1; i < 16; i++)
                    {
                        //textBox1.Text = BitConverter.ToString(result);
                    }
                        
                    


                    var updateSuccessful = card.UpdateBinary(MSB, LSB, _data_to_write);

                    if (!updateSuccessful)
                    {
                        throw new Exception("UPDATE BINARY failed.");
                    }

                    result = card.ReadBinary(MSB, LSB, 16);
                    for (int i = 0; i < 16; i++)
                    {
                        textBox2.Text = BitConverter.ToString(result);
                    }
                    //textBox1.Lines[1] = "Result (after BINARY UPDATE): " + BitConverter.ToString(result);
                }
            }
            //Console.ReadKey();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
