using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.SshNet;
using Npgsql;

namespace migrate_data
{
    public class PGHelper
    {
        public void MakeSSHTunnel(string tblName = null, List<string> sql = null )
        {
            using (SshClient client = new SshClient("192.168.0.108", "mcarman", "@ng31F@rm0823262"))
            {
                int index = 0;
                client.Connect();
                var tunnel = new ForwardedPortLocal("127.0.0.1", "127.0.0.1", 5432);
                client.AddForwardedPort(tunnel);
                if (client.IsConnected)
                {
                    tunnel.Start();
                    string connString = string.Format("Server={0};Database=accessdb_v2;Port={1};User Id=mcarman;Password=@ng31F@rm0823262;", tunnel.BoundHost, tunnel.BoundPort );
                    try
                    {
                        using (var conn = new NpgsqlConnection(connString))
                        {

                            System.Data.DataTable tbl = new System.Data.DataTable("user");
                            conn.Open();
                            NpgsqlCommand cmd = conn.CreateCommand();// new NpgsqlCommand();
                            cmd.CommandType = System.Data.CommandType.Text;
                            foreach (string s in sql)
                            { 
                                cmd.CommandText = s;
                                int cmdInt = cmd.ExecuteNonQuery();
                                Console.WriteLine(string.Format("Row {0} Updatede - Command Result: {1}", index, cmdInt));
                                index += 1;
                            }
                            //("select * from \"User\";", conn);

                           // cmd.CommandType = System.Data.CommandType.Text;
                            //NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd);
                            //adapter.Fill(tbl);
                            Console.Write("Yes");
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                    }
                   
                }
                else Console.Write("Broken");
            }
        }

        public void CloseSSHTunnel(SshClient client)
        {
            client.Disconnect();
        }
    }
}
