/***********************************************************************************************
* KeyInfo.cs
*
* Developed by     : SafeNet, Inc
*
* Copyright (C) 2009 SafeNet, Inc. All rights reserved.
*
* Purpose          : GetKeyInfoEx API user input output form. This is Source file for 
                     GetKeyInfo Form.

***********************************************************************************************/

//Include name space
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Sproeval
{
    /// <summary>
    /// GetKeyInfoEx API user input output form.This is Source file for 
    /// GetKeyInfo Form.
    /// </summary>
    public class KeyInfo : System.Windows.Forms.Form
    {
        // Duffrent resource control declaration here
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button CANCELBtn;
        private System.Windows.Forms.TextBox KeyID;
        private System.Windows.Forms.TextBox HardLimit;
        private System.Windows.Forms.TextBox LicUse;
        private System.Windows.Forms.TextBox NumTimeOut;
        private System.Windows.Forms.TextBox HighUse;

        // Variable for KeyIndex parameter of RNBOsproGetKeyInfoEx API
        private uint NextKey;
        private System.Windows.Forms.Label lblSerialNumber;
        private System.Windows.Forms.TextBox tbxSerialNum;
        private System.Windows.Forms.Label lblCap;
        private System.Windows.Forms.TextBox tbxCap;
        private Label label6;
        private Label label7;
        private TextBox writePasswordCounter;
        private TextBox OverwritePasswordCounter;
        private Label label8;
        private TextBox formFactor;
        private Label label9;
        private TextBox SoftwareKeyType;
        private Label label10;
        private TextBox StartDate;
        private Label label11;
        private TextBox EndDate;
        private System.ComponentModel.Container components = null;

        public KeyInfo()
        {
            //
            // Required for Windows Form Designer support
            // call to initialize diffrent resources in Form.
            //
            InitializeComponent();
        }

        /// <summary>
        // Distroy Form resource at time of closing the form.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.CANCELBtn = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.NumTimeOut = new System.Windows.Forms.TextBox();
            this.HighUse = new System.Windows.Forms.TextBox();
            this.HardLimit = new System.Windows.Forms.TextBox();
            this.LicUse = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.KeyID = new System.Windows.Forms.TextBox();
            this.tbxSerialNum = new System.Windows.Forms.TextBox();
            this.lblSerialNumber = new System.Windows.Forms.Label();
            this.lblCap = new System.Windows.Forms.Label();
            this.tbxCap = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.writePasswordCounter = new System.Windows.Forms.TextBox();
            this.OverwritePasswordCounter = new System.Windows.Forms.TextBox();
            this.label8 = new System.Windows.Forms.Label();
            this.formFactor = new System.Windows.Forms.TextBox();
            this.label9 = new System.Windows.Forms.Label();
            this.SoftwareKeyType = new System.Windows.Forms.TextBox();
            this.label10 = new System.Windows.Forms.Label();
            this.StartDate = new System.Windows.Forms.TextBox();
            this.label11 = new System.Windows.Forms.Label();
            this.EndDate = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // CANCELBtn
            // 
            this.CANCELBtn.Location = new System.Drawing.Point(213, 523);
            this.CANCELBtn.Name = "CANCELBtn";
            this.CANCELBtn.Size = new System.Drawing.Size(80, 27);
            this.CANCELBtn.TabIndex = 2;
            this.CANCELBtn.Text = "Cancel";
            this.CANCELBtn.Click += new System.EventHandler(this.CANCELBtn_Click);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(51, 523);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(80, 27);
            this.button1.TabIndex = 1;
            this.button1.Text = "NextKey";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(8, 182);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(69, 16);
            this.label2.TabIndex = 0;
            this.label2.Text = "Hard Limit";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(8, 217);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(75, 16);
            this.label3.TabIndex = 0;
            this.label3.Text = "Lic in Use";
            // 
            // NumTimeOut
            // 
            this.NumTimeOut.Location = new System.Drawing.Point(128, 249);
            this.NumTimeOut.MaxLength = 10;
            this.NumTimeOut.Name = "NumTimeOut";
            this.NumTimeOut.ReadOnly = true;
            this.NumTimeOut.Size = new System.Drawing.Size(214, 20);
            this.NumTimeOut.TabIndex = 7;
            // 
            // HighUse
            // 
            this.HighUse.Location = new System.Drawing.Point(128, 284);
            this.HighUse.MaxLength = 10;
            this.HighUse.Name = "HighUse";
            this.HighUse.ReadOnly = true;
            this.HighUse.Size = new System.Drawing.Size(214, 20);
            this.HighUse.TabIndex = 8;
            // 
            // HardLimit
            // 
            this.HardLimit.Location = new System.Drawing.Point(128, 179);
            this.HardLimit.MaxLength = 10;
            this.HardLimit.Name = "HardLimit";
            this.HardLimit.ReadOnly = true;
            this.HardLimit.Size = new System.Drawing.Size(214, 20);
            this.HardLimit.TabIndex = 5;
            // 
            // LicUse
            // 
            this.LicUse.Location = new System.Drawing.Point(128, 214);
            this.LicUse.MaxLength = 10;
            this.LicUse.Name = "LicUse";
            this.LicUse.ReadOnly = true;
            this.LicUse.Size = new System.Drawing.Size(214, 20);
            this.LicUse.TabIndex = 6;
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 32);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(77, 17);
            this.label1.TabIndex = 0;
            this.label1.Text = "DevID (hex)";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(8, 252);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(96, 17);
            this.label4.TabIndex = 0;
            this.label4.Text = "Num Timed Out";
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(8, 288);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(96, 15);
            this.label5.TabIndex = 0;
            this.label5.Text = "Highest in Use";
            // 
            // KeyID
            // 
            this.KeyID.Location = new System.Drawing.Point(128, 30);
            this.KeyID.MaxLength = 4;
            this.KeyID.Name = "KeyID";
            this.KeyID.Size = new System.Drawing.Size(214, 20);
            this.KeyID.TabIndex = 0;
            this.KeyID.Text = "FFFF";
            // 
            // tbxSerialNum
            // 
            this.tbxSerialNum.Location = new System.Drawing.Point(128, 65);
            this.tbxSerialNum.MaxLength = 10;
            this.tbxSerialNum.Name = "tbxSerialNum";
            this.tbxSerialNum.ReadOnly = true;
            this.tbxSerialNum.Size = new System.Drawing.Size(214, 20);
            this.tbxSerialNum.TabIndex = 3;
            // 
            // lblSerialNumber
            // 
            this.lblSerialNumber.Location = new System.Drawing.Point(8, 68);
            this.lblSerialNumber.Name = "lblSerialNumber";
            this.lblSerialNumber.Size = new System.Drawing.Size(96, 15);
            this.lblSerialNumber.TabIndex = 0;
            this.lblSerialNumber.Text = "Serial Number";
            // 
            // lblCap
            // 
            this.lblCap.Location = new System.Drawing.Point(8, 102);
            this.lblCap.Name = "lblCap";
            this.lblCap.Size = new System.Drawing.Size(110, 17);
            this.lblCap.TabIndex = 0;
            this.lblCap.Text = "Device Capabilities";
            // 
            // tbxCap
            // 
            this.tbxCap.Location = new System.Drawing.Point(128, 101);
            this.tbxCap.MaxLength = 200;
            this.tbxCap.Multiline = true;
            this.tbxCap.Name = "tbxCap";
            this.tbxCap.ReadOnly = true;
            this.tbxCap.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.tbxCap.Size = new System.Drawing.Size(214, 64);
            this.tbxCap.TabIndex = 4;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(8, 323);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(121, 13);
            this.label6.TabIndex = 9;
            this.label6.Text = "Write Password Counter";
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(8, 356);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(141, 13);
            this.label7.TabIndex = 10;
            this.label7.Text = "Overwrite Password Counter";
            // 
            // writePasswordCounter
            // 
            this.writePasswordCounter.BackColor = System.Drawing.SystemColors.Control;
            this.writePasswordCounter.Location = new System.Drawing.Point(182, 320);
            this.writePasswordCounter.Name = "writePasswordCounter";
            this.writePasswordCounter.Size = new System.Drawing.Size(160, 20);
            this.writePasswordCounter.TabIndex = 11;
            // 
            // OverwritePasswordCounter
            // 
            this.OverwritePasswordCounter.BackColor = System.Drawing.SystemColors.Control;
            this.OverwritePasswordCounter.Location = new System.Drawing.Point(182, 355);
            this.OverwritePasswordCounter.Name = "OverwritePasswordCounter";
            this.OverwritePasswordCounter.Size = new System.Drawing.Size(160, 20);
            this.OverwritePasswordCounter.TabIndex = 12;
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(8, 389);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(63, 13);
            this.label8.TabIndex = 13;
            this.label8.Text = "Form Factor";
            // 
            // formFactor
            // 
            this.formFactor.BackColor = System.Drawing.SystemColors.Control;
            this.formFactor.Location = new System.Drawing.Point(128, 388);
            this.formFactor.Name = "formFactor";
            this.formFactor.Size = new System.Drawing.Size(212, 20);
            this.formFactor.TabIndex = 14;
            // 
            // label9
            // 
            this.label9.AutoSize = true;
            this.label9.Location = new System.Drawing.Point(8, 422);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(97, 13);
            this.label9.TabIndex = 15;
            this.label9.Text = "Software Key Type";
            // 
            // SoftwareKeyType
            // 
            this.SoftwareKeyType.BackColor = System.Drawing.SystemColors.Control;
            this.SoftwareKeyType.Location = new System.Drawing.Point(128, 422);
            this.SoftwareKeyType.Name = "SoftwareKeyType";
            this.SoftwareKeyType.Size = new System.Drawing.Size(212, 20);
            this.SoftwareKeyType.TabIndex = 16;            
            // 
            // label10
            // 
            this.label10.AutoSize = true;
            this.label10.Location = new System.Drawing.Point(8, 457);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(55, 13);
            this.label10.TabIndex = 17;
            this.label10.Text = "Start Date";
            // 
            // StartDate
            // 
            this.StartDate.BackColor = System.Drawing.SystemColors.Control;
            this.StartDate.Location = new System.Drawing.Point(128, 457);
            this.StartDate.Name = "StartDate";
            this.StartDate.Size = new System.Drawing.Size(212, 20);
            this.StartDate.TabIndex = 18;
            // 
            // label11
            // 
            this.label11.AutoSize = true;
            this.label11.Location = new System.Drawing.Point(8, 494);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(52, 13);
            this.label11.TabIndex = 19;
            this.label11.Text = "End Date";
            // 
            // EndDate
            // 
            this.EndDate.BackColor = System.Drawing.SystemColors.Control;
            this.EndDate.Location = new System.Drawing.Point(128, 491);
            this.EndDate.Name = "EndDate";
            this.EndDate.Size = new System.Drawing.Size(212, 20);
            this.EndDate.TabIndex = 20;
            // 
            // KeyInfo
            // 
            this.AcceptButton = this.button1;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(422, 572);
            this.Controls.Add(this.EndDate);
            this.Controls.Add(this.label11);
            this.Controls.Add(this.StartDate);
            this.Controls.Add(this.label10);
            this.Controls.Add(this.SoftwareKeyType);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.formFactor);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.OverwritePasswordCounter);
            this.Controls.Add(this.writePasswordCounter);
            this.Controls.Add(this.tbxCap);
            this.Controls.Add(this.lblCap);
            this.Controls.Add(this.tbxSerialNum);
            this.Controls.Add(this.lblSerialNumber);
            this.Controls.Add(this.CANCELBtn);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.HighUse);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.NumTimeOut);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.LicUse);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.HardLimit);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.KeyID);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "KeyInfo";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "GetKeyInfoEx";
            this.Load += new System.EventHandler(this.KeyInfo_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        /***************************************************************************************
        * Procedure : button1_Click
        *
        * Purpose   : Called on OK button click
        *
        * Descrip   : Validation for diffrent user inputs.RNBOsproGetKeyInfo API call here.
        ***************************************************************************************/
        private void button1_Click(object sender, System.EventArgs e)
      {
         ushort status = 0;
         ushort developerId = 0;
         string sdeveloperId = "";
         int i_IsValid = -1;
         uint iCap;
         string szCap = "";
         
         byte[] szStartDate = new byte[30];
         byte[] szEndDate = new byte[30];

         // Get DevID value entered by user as string
         sdeveloperId = KeyID.Text;
         //Perform validation for DEVID string value and convert into integer.
         i_IsValid = MainForm.isValidData(sdeveloperId.Trim(), MainForm.VALIDDATA_LENGTH, ref developerId);

         if (i_IsValid == 1) //If DevID is blank
         {
            MessageBox.Show("Specify DeveloperID in input box to proceed.", "GetKeyInfoEx");
            return;
         }
         if (i_IsValid == 2)// If DevID string is of invalid length
         {
            MessageBox.Show("DeveloperID must be with length not exceeding 4 digits.", "GetKeyInfoEx");
            return;
         }
         if (i_IsValid == 3) // If DevID string is not valid HexString
         {
            MessageBox.Show("DeveloperID must be in hex.", "GetKeyInfoEx");
            return;
         }
         NextKey = (ushort)(NextKey + 1); // Increment value by one to get Next KeyInfo
         Superpro.NSPRO_MONITOR_INFO_EX monitorInfo = new Superpro.NSPRO_MONITOR_INFO_EX();
         byte[] bKeyBuffer = new byte[monitorInfo.size];
         //**************RNBOsproGetKeyInfoEx API **********************/
         status = MainForm.oSuperPro.RNBOsproGetKeyInfoEx(developerId, NextKey, 0, bKeyBuffer, monitorInfo.size);
         // If API get success
         if (status == 0)
         {
            //Extract Keyinfo value from KeyInfoEx structure
            monitorInfo.GetKeyInfo(bKeyBuffer);

            // Display Key info value in output box
            KeyID.Text = Convert.ToString(monitorInfo.sproKeyMonitorInfo.devId, 16).ToUpper();
            tbxSerialNum.Text = Convert.ToString(monitorInfo.sproKeyMonitorInfo.serialNum, 16).ToUpper();
            iCap = monitorInfo.sproKeyMonitorInfo.capabilities;
            if ((iCap & Superpro.SP_CAPS_AES_ALGO) != 0)
            {
               szCap += "AES_ALGO\r\n";
            }

            if ((iCap & Superpro.SP_CAPS_PASSWORD_COUNTER) != 0)
            {
               szCap += "PASSWORD_COUNTER\r\n";
            }

            if ((iCap & Superpro.SP_CAPS_SECURE_TUNNEL) != 0)
            {
               szCap += "SECURE_TUNNEL\r\n";
            }
            if ((iCap & Superpro.SP_CAPS_DISABLE_DEVICE_SHARING) != 0)
            {
               szCap += "DISABLE_DEVICE_SHARING";
            }
            tbxCap.Text = szCap;
            HardLimit.Text = monitorInfo.sproKeyMonitorInfo.hardLimit.ToString();
            LicUse.Text = monitorInfo.sproKeyMonitorInfo.inUse.ToString();
            NumTimeOut.Text = monitorInfo.sproKeyMonitorInfo.numTimeOut.ToString();
            HighUse.Text = monitorInfo.sproKeyMonitorInfo.highestUse.ToString();
            writePasswordCounter.Text = monitorInfo.sproKeyMonitorInfo.writePasswordCounter.ToString();
            OverwritePasswordCounter.Text = monitorInfo.sproKeyMonitorInfo.overwritePasswordCounter.ToString();

            if (monitorInfo.sproKeyMonitorInfo.formFactor == 0)
            {
               formFactor.Text = "SP_KEY_FORM_FACTOR_PARALLEL";
            }
            else if (monitorInfo.sproKeyMonitorInfo.formFactor == 1)
            {
               formFactor.Text = "SP_KEY_FORM_FACTOR_USB";
            }
            else if (monitorInfo.sproKeyMonitorInfo.formFactor == 2)
            {
               formFactor.Text = "SP_KEY_FORM_FACTOR_SOFT";
            }

             if (monitorInfo.sproKeyMonitorInfo.formFactor == 2) //SP_KEY_FORM_FACTOR_SOFT
             {
                if (monitorInfo.sproKeyMonitorInfo.softKeyType == 1)//SP_SIMPLE_SOFT
                {
                   SoftwareKeyType.Text = "SP_SIMPLE_SOFT";
                }
                else if (monitorInfo.sproKeyMonitorInfo.softKeyType == 2)//SP_SECURE_SOFT
                {
                   SoftwareKeyType.Text =  "SP_SECURE_SOFT";
                }
                else if (monitorInfo.sproKeyMonitorInfo.softKeyType == 3)//SP_EVALUATION_SOFT
                {
                   SoftwareKeyType.Text =  "SP_EVALUATION_SOFT";
                }
                else
                {
                   SoftwareKeyType.Text =  "UNKNOWN SOFT KEY TYPE";
                }
             }
             else
             {
                SoftwareKeyType.Text =  "N/A";
             }

             if (monitorInfo.sproKeyMonitorInfo.formFactor == 2)//SP_KEY_FORM_FACTOR_SOFT
             {
                if (monitorInfo.sproKeyMonitorInfo.startDate.year == 0)
                {
                   StartDate.Text = "N/A";
                   EndDate.Text =  "N/A";
                }
                else
                {
                    StartDate.Text = monitorInfo.sproKeyMonitorInfo.startDate.year.ToString() + "-" + monitorInfo.sproKeyMonitorInfo.startDate.month.ToString("D2") + "-" + monitorInfo.sproKeyMonitorInfo.startDate.day.ToString("D2") + "  (yyyy - MM - dd)";
                    EndDate.Text = monitorInfo.sproKeyMonitorInfo.endDate.year.ToString() + "-" + monitorInfo.sproKeyMonitorInfo.endDate.month.ToString("D2") + "-" + monitorInfo.sproKeyMonitorInfo.endDate.day.ToString("D2") + "  (yyyy - MM - dd)";
                }
             }
             else
             {
                 StartDate.Text = "N/A";
                 EndDate.Text = "N/A";
             }
         }
         //If API get fail
         else
         {
            NextKey = (ushort)(NextKey - 1); // decrement value by one)
            MessageBox.Show("GetKeyInfoEx failed!\n Return code is " + status + ".", "GetKeyInfoEx", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
      }

        /***************************************************************************************
          * Procedure : CANECLBtn_Click
          *
          * Purpose   : Called on Cancel button click
          *
          * Descrip   : Close form and clear output box as blank
          ***************************************************************************************/
        private void CANCELBtn_Click(object sender, System.EventArgs e)
        {
            // assign blank string for diffrent output box
            KeyID.Text = "";
            HardLimit.Text = "";
            LicUse.Text = "";
            NumTimeOut.Text = "";
            NumTimeOut.Text = "";
            HighUse.Text = "";
            writePasswordCounter.Text = "";
            OverwritePasswordCounter.Text = "";
            //close the form.
            Close();
        }

        /***************************************************************************************
          * Procedure : KeyInfo_Load
          *
          * Purpose   : Called AT TIME OF Form Load
          *
          * Descrip   : Assign some default value in KeyInfo textBox
          ***************************************************************************************/
        private void KeyInfo_Load(object sender, System.EventArgs e)
        {
            KeyID.Text = "FFFF";
            NextKey = 0;
        }        
    }
}
