using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NiApiLib;

namespace NI
{  
    public class SrvrSession
    {
        private NiApiLib.SrvrSession srvrSession;
        public Boolean isConnected = false;
        public long connectionStatus = 0;

        public String catalogName;
        public String hostAddr;
        public int hostPort;
        public String journal;
        public String name;
        public String partInsert;
        public String password;


        private Boolean isCrypted = false;
        private string cryptoLibrary = "NiSec.Dll";//"NiSecure.dll";
        private string keyBase;
        private string userKey;
        private string serverKey;


        public delegate void LoginHandler(int i);
        public event LoginHandler Login;


        public SrvrSession(String host, int port, String usr, String pswd, bool isCrypted)
        {
            srvrSession = new NiApiLib.SrvrSessionClass();
            this.isConnected = false;
            this.isCrypted = isCrypted;

            hostAddr = host;
            hostPort = port;
            name = usr;
            password = pswd;
            journal = "log.txt";


            srvrSession.OnMessage += new _ISrvrSessionEvents_OnMessageEventHandler(onMessage);
            srvrSession.OnLogin += new _ISrvrSessionEvents_OnLoginEventHandler(onLogin);
            srvrSession.OnConnectionLost += new _ISrvrSessionEvents_OnConnectionLostEventHandler(onConnectionLost);
        }

        public void setCryptoParameters(string keyBase, string userKey, string serverKey)
        {
            isCrypted = true;
            this.keyBase = keyBase;
            this.userKey = userKey;
            this.serverKey = serverKey;
        }

        public NiApiLib.SrvrSession getNISrvrSession()
        {
            return srvrSession;
        }

        public void onMessage(object Msg)
        {
        }

        public void onLogin(int LoginResult)
        {
            connectionStatus = LoginResult;
            this.isConnected = true;
            Console.WriteLine("Logined");
            Login(LoginResult);
        }

        public void onConnectionLost(){
            isConnected = false;
            connectionStatus = -1;
        }

        public void connect()
        {
            if (this.isConnected == false)
            {
                srvrSession.HostAddr = hostAddr;
                srvrSession.HostPort = hostPort;
                srvrSession.Name = name;
                srvrSession.Password = password;
                srvrSession.Journal = journal;
                if (hostAddr != null && hostPort != 0
                    && name != null && password != null)
                {
                    encrypt();
                    srvrSession.Connect();
                }
            }
        }

        public void disconnect() 
        {
            isConnected = false;
            connectionStatus = 0;
            isCrypted = false;
            encrypt();
            srvrSession.Disconnect();
        }

        public void encrypt(){
                if (isCrypted == true)
                {
                    try
                    {
                        this.srvrSession.CryptoStart(this.cryptoLibrary, this.keyBase, this.userKey, this.serverKey);
                    }
                    catch (System.ArgumentException eCryptInvArg)
                    {
                        Console.WriteLine("Crypto parameters are not valid.\n" + eCryptInvArg.Message);
                    }
                    catch (System.FieldAccessException eFaildArg)
                    {
                        Console.WriteLine("Crypto hasn't been activated." + eFaildArg.Message);
                    }
                    catch (System.NotImplementedException eNotEmpl)
                    {
                        Console.WriteLine("Crypto hasn't been inisialized" + eNotEmpl.Message);
                    }
                    catch (System.Exception exp)
                    {
                        Console.WriteLine("Crypto Name of file or key base is not correct" + exp.Message);
                    }

                }
                else
                {
                    try
                    {
                        this.srvrSession.CryptoStop();
                    }
                    catch (System.UnauthorizedAccessException eCrypt)
                    {
                        Console.WriteLine("Crypto hasn't been initilized" + eCrypt.Message);
                    }
                }
        }


    }
}
