using Microsoft.AspNetCore.SignalR;
using NetPanel.Bl;
using NetPanel.Entity;
using NetPanel.Help;
using Renci.SshNet.Common;
using Renci.SshNet.Messages;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;

namespace NetPanel.SignalR
{
    public class ChatHub : Hub
    {

        /// <summary>
        /// ssh 管理
        /// </summary>
        private static Dictionary<string, SshBl> dicSsh = new Dictionary<string, SshBl>();

        /// <summary>
        /// 建立连接时
        /// </summary>
        /// <returns></returns>
        public override async Task OnConnectedAsync()
        {

            ReturnMsg rm = new ReturnMsg();
            rm.Code = 1;

            var cid = Context.ConnectionId;
            rm.Obj = cid;

            //根据id获取指定客户端
            var client = Clients.Client(cid);
            //向指定用户发送消息
            await client.SendAsync("ConnectionId", rm);
        }

        /// <summary>
        /// 断开异步时
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>

        public override async Task OnDisconnectedAsync(Exception? ex)
        {
            var cid = Context.ConnectionId;
            if (dicSsh.ContainsKey(cid))
            {
                dicSsh[cid].Dispose();
                dicSsh.Remove(cid);
            }
            await base.OnDisconnectedAsync(ex);
        }

        /// <summary>
        /// 创建终端链接
        /// </summary>
        /// <param name="connectionId"></param>
        /// <param name="command"></param>
        /// <returns></returns>
        public async Task<ReturnMsg> CreateConnectionAsync(uint width, uint height, uint columns, uint rows)
        {

            var cid = Context.ConnectionId;
            ReturnMsg rm = new ReturnMsg();
            rm.Code = 1;
            if (!dicSsh.ContainsKey(cid))
            {
                SshBl ssh = new SshBl("172.29.119.239", "root", "123456789", columns, rows, width, height);
                dicSsh.Add(cid, ssh);
                ReceiveMessage(cid);
            }
            return rm;
        }

        /// <summary>
        /// 写入命令
        /// </summary>
        /// <param name="curr_line"></param>
        /// <returns></returns>
        public async Task<ReturnMsg> WriteLineAsync(string curr_line)
        {
            ReturnMsg rm = new ReturnMsg();
            rm.Code = 1;
            var cid = Context.ConnectionId;
            if (dicSsh.ContainsKey(cid))
            {
                dicSsh[cid].WriteLine(curr_line);
                ReceiveMessage(cid);
            }
            return rm;
        }




        /// <summary>
        /// 发送响应
        /// </summary>
        /// <param name="ssh"></param>
        /// <returns></returns>
        public async Task ReceiveMessage(string cid)
        {
            ReturnMsg rm = new ReturnMsg();
            rm.Code = 1;
            int count = 0;
            SshBl ssh = dicSsh[cid];
            //根据id获取指定客户端
            var client = Clients.Client(cid);
            while (true)
            {
                try
                {
                    
                    string line = ssh.ReadLine();
                    if (line.IsNotNull())
                    {
                        if (line.Contains(":~#"))
                        {
                            rm.Code = 2; ;
                            rm.Obj = line;
                            await client.SendAsync("ReceiveMessage", rm);
                            break;
                        }
                        else
                        {
                            rm.Obj = line + "\n";
                            await client.SendAsync("ReceiveMessage", rm);
                        }                       
                    }
                    else
                    {
                        await Task.Delay(100);
                    }
                }
                catch (Exception ex)
                {
                }
            }
         
          
          

        }


    }
}
