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

namespace OPSRoutingInterception
{
    class Program
    {
        static OpsClient client;
        static string prompt = string.Empty;

        static void Main(string[] args)
        {
            ShowGreetingMessage();
            ReadLoginInfos();
            SetRoutingInterceptor();
            Console.ReadLine();
        }

        private static void ShowGreetingMessage()
        {
            Console.WriteLine("This is a simple Ozeki Phone System XE demo written in C#.");
            Console.WriteLine("It can be used to create dynamic routing on Ozeki Phone System XE.");
            Console.WriteLine(@"The sample program sets ""do not disturb"" state and declines every call,");
            Console.WriteLine("when *90 is dialled.");
            Console.WriteLine("You can cancel this if you dial *91.");
            Console.WriteLine("-------------------------------------------------------------------------------");
            Console.WriteLine();
        }

        private static void ReadLoginInfos()
        {
            Console.WriteLine("Please enter the IP address of Ozeki Phone System XE.");

            var serverAddress = Read("Server address (default: 127.0.0.1)", false);

            if (string.IsNullOrEmpty(serverAddress))
                serverAddress = "127.0.0.1";

            Console.WriteLine("Please enter the username and password of the user created in Ozeki Phone System XE");
            var username = Read("Username (default: admin)", false);

            if (string.IsNullOrEmpty(username))
                username = "admin";

            var password = Read("Password", true);
            Login(serverAddress, username, password);
        }

        private static void SetRoutingInterceptor()
        {
            client.SetCallRoutingInterceptor(new MyCallRoutingInterceptor());
        }

        private static void Login(string serverAddress, string username, string password)
        {
            client = new OpsClient();
            Console.WriteLine("Connecting...");

            client.ErrorOccurred += client_ErrorOccurred;
            var result = client.Login(serverAddress, username, password);

            if (result)
            {

                Console.WriteLine("Successfully connected to {0} with username: {1}.", serverAddress, username);

        
            }
            else
            {
                Console.WriteLine("Connection fail try again.");
                Console.WriteLine("Please check whether the IP address is correct and the given user has right to use OPS SDK. This can be checked under PBX features/Preferences/User access profile.");
                ReadLoginInfos();
            }

        }

        static void client_ErrorOccurred(object sender, ErrorInfo e)
        {
            Console.WriteLine(e.Message);
        }

        static string Read(string inputName, bool readWhileEmpty)
        {
            Console.Write(prompt + "" + inputName + ": ");
            while (true)
            {
                var input = Console.ReadLine();

                if (!readWhileEmpty)
                    return input;

                if (!string.IsNullOrEmpty(input))
                    return input;

                Console.WriteLine(inputName + " cannot be empty.");
                Console.Write(inputName + ": ");
            }
        }

        

    }
}
