using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TreeksLicensingLibrary2.EasyIntegration;

namespace TLLCsExample
{
    public partial class Form1 : Form
    {

        TLLInterface tlli = null;

        const string MessageBoxTitle = "TLL C# Example";

        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "All files (*.*)|*.*";

            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtInitChunkPath.Text = ofd.FileName;
            }
        }

        private void btnInitTLL_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] chunkbytes = System.IO.File.ReadAllBytes(txtInitChunkPath.Text);
                tlli = new TLLInterface(chunkbytes, txtInitChunkPassword.Text);

                LoadLicenseStatus();

                GroupBox1.Enabled = false;
                GroupBox2.Enabled = true;
                GroupBox3.Enabled = true;

                MessageBox.Show("TLL Interface object successfully initialized.", MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Initialization error: " + ex.Message, MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            string strRegistrationResult = tlli.Register(txtLicense.Text);

            if (strRegistrationResult == "OK")
            {
                LoadLicenseStatus();
                MessageBox.Show("License was successfully registered.", MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Registration failed: " + strRegistrationResult, MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void LoadLicenseStatus()
        {
            if (tlli.MyLicense == null)
            {
                txtLicStatus.Text = "License validation failed: " + tlli.LastVerificationResult;
            }
            else
            {
                txtLicStatus.Text = GenerateLicenseDump(tlli.MyLicense);
            }

        }


        private string GenerateLicenseDump(TreeksLicensingLibrary2.License lic)
        {
            if (lic == null)
                return "No license is present.";

            string strLicenseDump = "Correct and working license present." + Environment.NewLine + Environment.NewLine + "Product: " + lic.ProductName + Environment.NewLine + "License owner: " + lic.OwnerName;

            strLicenseDump += Environment.NewLine + "Serial: " + lic.SerialNumber;

            strLicenseDump += Environment.NewLine + Environment.NewLine + "License expiration enabled: " + lic.ExpirationDateEnabled;

            if (lic.ExpirationDateEnabled)
            {
                strLicenseDump += Environment.NewLine + "Expiration date: " + lic.ExpirationDate;
                strLicenseDump += Environment.NewLine + "Expiration check time server: " + lic.ExpirationCheckTimeServer;
                strLicenseDump += Environment.NewLine + "Treat offline as expired: " + lic.ExpirationCheckOnlineOnly;
            }

            if (lic.HardwareID.Count > 0)
            {
                strLicenseDump += Environment.NewLine + Environment.NewLine + "Hardware locking enabled: True";

                strLicenseDump += Environment.NewLine + "Allowed hardware IDs: ";

                bool first = true;
                foreach (string hwid in lic.HardwareID)
                {
                    if (first)
                    {
                        strLicenseDump += hwid;
                        first = false;
                    }
                    else
                    {
                        strLicenseDump += ", " + hwid;
                    }
                }
            }
            else
            {
                strLicenseDump += Environment.NewLine + Environment.NewLine + "Hardware locking enabled: False";
            }


            if (lic.AllowedFeatures.Count > 0)
            {
                strLicenseDump += Environment.NewLine + Environment.NewLine + "Allowed features: ";

                bool first = true;
                foreach (string af in lic.AllowedFeatures)
                {
                    if (first)
                    {
                        strLicenseDump += af;
                        first = false;
                    }
                    else
                    {
                        strLicenseDump += ", " + af;
                    }
                }
            }
            else
            {
                strLicenseDump += Environment.NewLine + Environment.NewLine + "Allowed features: none";
            }

            if (lic.BlockedFeatures.Count > 0)
            {
                strLicenseDump += Environment.NewLine + Environment.NewLine + "Blocked features: ";

                bool first = true;
                foreach (string bf in lic.BlockedFeatures)
                {
                    if (first)
                    {
                        strLicenseDump += bf;
                        first = false;
                    }
                    else
                    {
                        strLicenseDump += ", " + bf;
                    }
                }
            }
            else
            {
                strLicenseDump += Environment.NewLine + Environment.NewLine + "Blocked features: none";
            }

            strLicenseDump += Environment.NewLine + Environment.NewLine + "Custom fields:";

            strLicenseDump += Environment.NewLine + "Info 1: " + lic.Info1;
            strLicenseDump += Environment.NewLine + "Info 2: " + lic.Info2;
            strLicenseDump += Environment.NewLine + "Info 3: " + lic.Info3;
            strLicenseDump += Environment.NewLine + "Info 4: " + lic.Info4;
            strLicenseDump += Environment.NewLine + "Info 5: " + lic.Info5;
            strLicenseDump += Environment.NewLine + "Info 6: " + lic.Info6;
            strLicenseDump += Environment.NewLine + "Info 7: " + lic.Info7;
            strLicenseDump += Environment.NewLine + "Info 8: " + lic.Info8;
            strLicenseDump += Environment.NewLine + "Info 9: " + lic.Info9;
            strLicenseDump += Environment.NewLine + "Info 10: " + lic.Info10;

            return strLicenseDump;
        }

        private void btnDeleteLicense_Click(object sender, EventArgs e)
        {
            tlli.DeleteLicenseInRegistry();
            LoadLicenseStatus();
            MessageBox.Show("License deleted.", MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
