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

namespace RogerRud.RUD
{
    /// <summary>
    /// RUD-4v1.0 Reader
    /// </summary>
    class RUD2Reader: UsbReader
    {

        public RUD2Reader()
        {
 
        }


        /// <summary>
        /// List of all RUD-4v1.0
        /// </summary>
        /// <returns></returns>
        public List<String[]> FindAll()
        {
            //get only RUD4
            return getDevices(rud.RUD_4);
        }

        /// <summary>
        /// Read card number
        /// </summary>
        /// <returns> card number or empty string</returns>
        public String readProxy()
        {
            //Get EPSO 2 Function A1 - Read Card buffer
            //see: EPSO 2 Protocol

            string frame = Epso.readCardBuffer(0x00,(byte)'A', (byte)'1');       //0x00 - RUD-2 v2.0 device ID
            string cardNumber = string.Empty;

            if (IsConnect== true)
            {
                if (write(frame) == true)
                {
                    string readFrame = read();

                    //Get card number from received frame
                    //Frame contains:
                    //  - string with new card number or
                    //  - empty string - new card no found
                    cardNumber = Epso.getCardBuffer(readFrame,(byte)'A', (byte)'1');

                    cardNumber = cardNumber.TrimStart('R');         //Trim start 'R'- see: EPSO 2 Protocol
                    
                }                
            }
            return cardNumber;
        }

        /// <summary>
        /// Read Mf card bufer
        /// </summary>
        /// <returns> card number or empty string</returns>
        public String readMifareBuff()
        {
            //Get EPSO 2 Function A6 
            //see: EPSO 2 Protocol

            string frame = Epso.readCardBuffer(0x00, (byte)'A', (byte)'7');       
            string cardNumber = string.Empty;

            if (IsConnect == true)
            {
                if (write(frame) == true)
                {
                    string readFrame = read();

                    //Get card number from received frame
                    //Frame contains:
                    //  - string with new card number or
                    //  - empty string - new card no found
                    cardNumber = Epso.getCardBuffer(readFrame, (byte)'A', (byte)'7');

                    cardNumber = cardNumber.TrimStart('R');         //Trim start 'R'- see: EPSO 2 Protocol

                }
            }
            return cardNumber;
        }

        /// <summary>
        /// Read EM card bufer
        /// </summary>
        /// <returns> card number or empty string</returns>
        public String readEMBuff()
        {
            //Get EPSO 2 Function A7 
            //see: EPSO 2 Protocol

            string frame = Epso.readCardBuffer(0x00, (byte)'A', (byte)'8');
            string cardNumber = string.Empty;

            if (IsConnect == true)
            {
                if (write(frame) == true)
                {
                    string readFrame = read();

                    //Get card number from received frame
                    //Frame contains:
                    //  - string with new card number or
                    //  - empty string - new card no found
                    cardNumber = Epso.getCardBuffer(readFrame, (byte)'A', (byte)'8');

                    cardNumber = cardNumber.TrimStart('R');         //Trim start 'R'- see: EPSO 2 Protocol

                }
            }
            return cardNumber;
        }

        /// <summary>
        /// buzzer on
        /// </summary>
        /// <returns> </returns>
        public bool buzzerON()
        {
            //Get EPSO 2 Function B1 
            //see: EPSO 2 Protocol

            string frame = Epso.OutputON(0x00, (byte)'B', (byte)'1');
            string answer = string.Empty;

            if (IsConnect == true)
            {
                if (write(frame) == true)
                {
                    string readFrame = read();
                    answer = Epso.getAnswer(readFrame, (byte)'B', (byte)'1');

                    return true;

                }
            }
            return false;
        }

        /// <summary>
        /// buzzer off
        /// </summary>
        /// <returns> </returns>
        public bool buzzerOff()
        {
            //Get EPSO 2 Function B0
            //see: EPSO 2 Protocol

            string frame = Epso.OutputOff(0x00, (byte)'B', (byte)'0');
            string answer = string.Empty;

            if (IsConnect == true)
            {
                if (write(frame) == true)
                {
                    string readFrame = read();
                    answer = Epso.getAnswer(readFrame, (byte)'B', (byte)'0');

                    return true;

                }
            }
            return false;
        }
        /// <summary>
        /// led green on
        /// </summary>
        /// <returns> </returns>
        public bool lenGreenON()
        {
            //Get EPSO 2 Function D5
            //see: EPSO 2 Protocol

            string frame = Epso.OutputON(0x00, (byte)'D', (byte)'5');
            string answer = string.Empty;

            if (IsConnect == true)
            {
                if (write(frame) == true)
                {
                    string readFrame = read();
                    answer = Epso.getAnswer(readFrame, (byte)'D', (byte)'5');

                    return true;

                }
            }
            return false;
        }

        /// <summary>
        /// led green off
        /// </summary>
        /// <returns> </returns>
        public bool lenGreenOff()
        {
            //Get EPSO 2 Function D4
            //see: EPSO 2 Protocol

            string frame = Epso.OutputOff(0x00, (byte)'D', (byte)'4');
            string answer = string.Empty;

            if (IsConnect == true)
            {
                if (write(frame) == true)
                {
                    string readFrame = read();
                    answer = Epso.getAnswer(readFrame, (byte)'D', (byte)'4');

                    return true;

                }
            }
            return false;
        }

        /// <summary>
        /// led red on
        /// </summary>
        /// <returns> </returns>
        public bool lenRedON()
        {
            //Get EPSO 2 Function D1
            //see: EPSO 2 Protocol

            string frame = Epso.OutputON(0x00, (byte)'D', (byte)'1');
            string answer = string.Empty;

            if (IsConnect == true)
            {
                if (write(frame) == true)
                {
                    string readFrame = read();
                    answer = Epso.getAnswer(readFrame, (byte)'D', (byte)'1');

                    return true;

                }
            }
            return false;
        }

        /// <summary>
        /// led red off
        /// </summary>
        /// <returns> </returns>
        public bool lenRedOff()
        {
            //Get EPSO 2 Function D0
            //see: EPSO 2 Protocol

            string frame = Epso.OutputOff(0x00, (byte)'D', (byte)'0');
            string answer = string.Empty;

            if (IsConnect == true)
            {
                if (write(frame) == true)
                {
                    string readFrame = read();
                    answer = Epso.getAnswer(readFrame, (byte)'D', (byte)'0');

                    return true;

                }
            }
            return false;
        }


        /// <summary>
        /// relay on
        /// </summary>
        /// <returns> </returns>
        public bool RelayON()
        {
            //Get EPSO 2 Function C1
            //see: EPSO 2 Protocol

            string frame = Epso.OutputON(0x00, (byte)'C', (byte)'1');
            string answer = string.Empty;

            if (IsConnect == true)
            {
                if (write(frame) == true)
                {
                    string readFrame = read();
                    answer = Epso.getAnswer(readFrame, (byte)'C', (byte)'1');

                    return true;

                }
            }
            return false;
        }

        /// <summary>
        /// relay off
        /// </summary>
        /// <returns> </returns>
        public bool RelayOff()
        {
            //Get EPSO 2 Function C0
            //see: EPSO 2 Protocol

            string frame = Epso.OutputOff(0x00, (byte)'C', (byte)'0');
            string answer = string.Empty;

            if (IsConnect == true)
            {
                if (write(frame) == true)
                {
                    string readFrame = read();
                    answer = Epso.getAnswer(readFrame, (byte)'C', (byte)'0');

                    return true;

                }
            }
            return false;
        }
    }
}
