using IpcLibrary.Core;
using IpcLibrary.Core.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IpcLibrary.Client {
    /// <summary>
    /// IPC客户端工厂
    /// </summary>
    public static class IPCClientFactory {
        /// <summary>
        /// 创建默认配置的客户端
        /// </summary>
        public static IIPCClient CreateClient() {
            return new IPCClient();
        }

        /// <summary>
        /// 创建自定义配置的客户端
        /// </summary>
        public static IIPCClient CreateClient(IPCConfiguration configuration) {
            return new IPCClient(configuration);
        }

        /// <summary>
        /// 创建并连接客户端
        /// </summary>
        public static async Task<IIPCClient> CreateAndConnectAsync(string serverAddress, IPCConfiguration configuration = null) {
            var client = new IPCClient(configuration);

            if (!await client.ConnectAsync(serverAddress)) {
                client.Dispose();
                throw new ConnectionException("无法连接到服务器");
            }

            return client;
        }
    }
}
