using CodeMeter;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SanSystem
{
    class Dongle: IDisposable
    {
        uint FirmCode;
        uint ProductCode;

        Api cmApi;
        CmCredential cmCred;
        CmAccess2 cmAcc;
        HCMSysEntry hcmse;
        CmBoxInfo cmBoxInfo;
        CmBoxEntry2 BoxContent;

        public Dongle(uint ProductCode)
        {
#if !DEBUG
            this.FirmCode = 103086;
#else
            this.FirmCode = 10;
#endif

            this.ProductCode = ProductCode;
            cmApi = new Api();
            cmCred = new CmCredential();
            cmAcc = new CmAccess2();

            cmAcc.Credential = cmCred;

            cmAcc.Ctrl |= CmAccess.Option.UserLimit;
            cmAcc.FirmCode = this.FirmCode;
            cmAcc.ProductCode = this.ProductCode;


            hcmse = cmApi.CmAccess2(CmAccessOption.Local, cmAcc);
            if(hcmse == null)
            {
                ErrorCodes2 code = cmApi.CmGetLastErrorCode2();
                string output = string.Format("{0}", code);
                
            }

            if (!CheckDongleVorhanden())
                Trace.WriteLine("Dongle nicht vorhanden");

            cmBoxInfo = new CmBoxInfo();

            CmGetBoxContentsOption boxOptions = new CmGetBoxContentsOption();
            boxOptions = CmGetBoxContentsOption.FirmItem;

            CmBoxEntry2[] tmpBoxContent;

            tmpBoxContent = cmApi.CmGetBoxContents2(hcmse, boxOptions, this.FirmCode, cmBoxInfo);

            foreach (CmBoxEntry2 boxes in tmpBoxContent)
            {
                if (boxes.ProductCode == this.ProductCode)
                {
                    this.BoxContent = boxes;
                }
            }
        }
        ~Dongle()
        {
            CleanDongle();
        }

        public void CleanDongle()
        {
            cmApi.CmRelease(hcmse);
        }

        public bool CheckDongleVorhanden()
        {
#if LAPTOP
            return true;
#else
            if (hcmse == null)
                return false;
            else
                return true;
#endif
        }

        public string GetDongleSerial()
        {

            CmBoxInfo res = (CmBoxInfo)cmApi.CmGetInfo(hcmse, CmGetInfoOption.BoxInfo);
            if (null != res)
            {
                return res.SerialNumber.ToString();
            }
            else
            {
                throw new Exception("Fehler beim aufrufen der Seriennummer");
            }
        }

        public uint GetFeatureMap()
        {
            return BoxContent.FeatureMap;
        }

        public string GetName()
        {
            return "";
        }

        public bool IsLicensed(byte neededMask)
        {
#if LAPTOP
            return true;
#else
            uint DongleFeature = GetFeatureMap();

            //Trace.WriteLine("DongleFeature: " + DongleFeature);

            byte DongleFeatureB = (byte)DongleFeature;

            if ((DongleFeatureB & neededMask) == neededMask)
                return true;
            return false;
#endif
        }

        public void Dispose()
        {
            CleanDongle();
        }
    }

    
}
