﻿using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace TS3.ClientQuery
{
    /// <summary>
    /// Command to get informations about a channel at the current selected server-connection-handler.
    /// </summary>
    public class ChannelConnectInfo : SimpleCommand
    {
        private static readonly Regex LINE_PATTERN = new Regex(@"path=(?<path>\S*)( password=(?<password>\S*))?");

        /// <summary>
        /// Path to the channel.
        /// </summary>
        public string Path { get; protected set; } = "";

        /// <summary>
        /// Password for the channel.
        /// </summary>
        public string Password { get; protected set; } = "";

        /// <summary>
        /// Constructor which stets the call for this command.
        /// </summary>
        public ChannelConnectInfo() : this(0) { }

        /// <summary>
        /// Constructor which stets the call for this command with a id for the channel.
        /// </summary>
        /// <param name="channelID">Id for the channel to get the channel-informations. (default = 0 | current channel)</param>
        public ChannelConnectInfo(int channelID) : base("") { SetChannelID(channelID); }

        /// <summary>
        /// Sets the id for the channel to get the informations from.
        /// </summary>
        /// <param name="channelID">Id for the channel to get the channel-informations. (default = 0 | current channel)</param>
        public void SetChannelID(int channelID)
        {
            Call = "channelconnectinfo";

            if (channelID > 0)
                Call += " cid=" + channelID;
        }

        /// <summary>
        /// A template-method to process all lines of the requested answer to properties.
        /// It's indirectly called from the connected client after getting an answer.
        /// </summary>
        /// <param name="lines">All lines of the requested answer without the error-id and error-message.</param>
        protected override void ProcessAnswer(List<string> lines)
        {
            Match match;

            foreach (string line in lines)
            {
                match = LINE_PATTERN.Match(line);

                if (match.Success)
                {
                    Path = TransformText(match.Groups["path"].Value);
                    Password = match.Groups["password"].Value;
                }
            }
        }
    }
}
