using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using TextBox = System.Windows.Forms.TextBox;


namespace SSH_Test
{
    public partial class FormParameters : Form
    {
        public Dictionary <string,string> dParameters{ get; set; }
        public FormParameters(Dictionary<string, string> parameters, string helpertext, string formtext)
        {
            InitializeComponent();
            dParameters = new Dictionary<string, string>();
            dParameters = parameters;
            this.Text = formtext;            
            for (int i = 0; i < parameters.Count; i++)
            {
                string sKey  = parameters.ElementAt(i).Key;
                dataGridViewParameters.Rows.Add(sKey, parameters[sKey]);                           
                              
            }
            this.dataGridViewParameters.AllowUserToAddRows = false;
            // Put edit cursor on the first row
            this.dataGridViewParameters.CurrentCell = dataGridViewParameters.Rows[0].Cells[1];
            this.dataGridViewParameters.CellEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridViewParameters_CellEnter);
            this.dataGridViewParameters.Focus();
            this.dataGridViewParameters.AllowDrop = true;
            this.dataGridViewParameters.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
            this.richTextBoxHelper.Text = helpertext;
            richTextBoxHelper.SelectionStart = richTextBoxHelper.Text.Length;
            richTextBoxHelper.ScrollToCaret();

        }

        private void bOK_Click(object sender, EventArgs e)
        {
            
           
            for (int i = 0; i < dParameters.Count; i++)
            {
                if (dataGridViewParameters.Rows[i].Cells[1].Value != null)
                    dParameters[dataGridViewParameters.Rows[i].Cells[0].Value.ToString()] = dataGridViewParameters.Rows[i].Cells[1].Value.ToString().TrimEnd().TrimStart();

            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        //to skip TAB stop at the colummn "parameters" 1
        private void dataGridViewParameters_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            // Check if the current column is the one you want to skip
            if (e.ColumnIndex == 0) // Use the zero-based index of your column
            {
                // Send a Tab key press to move to the next cell
                SendKeys.Send("{TAB}");
            }
        }

        private void dataGridViewParameters_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ensure the click is on a valid cell and not the header
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                // Set the cell as the current cell (if not already)
                dataGridViewParameters.CurrentCell = dataGridViewParameters.Rows[e.RowIndex].Cells[e.ColumnIndex];

                // Start the edit mode immediately
                dataGridViewParameters.BeginEdit(true);
            }
        }

        private void dataGridViewParameters_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string sTemp = (string)dataGridViewParameters.Rows[e.RowIndex].Cells[1].Value;
            if (sTemp != null) 
                dParameters[(string)dataGridViewParameters.Rows[e.RowIndex].Cells[0].Value] = sTemp;
            if (e.RowIndex == dataGridViewParameters.RowCount - 1)
                dataGridViewParameters.Rows[e.RowIndex].Cells[1].Selected = false;

        }

        private void dataGridViewParameters_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //mask password cells
            // check if it is the value column
            if (e.ColumnIndex == 1 && e.Value != null)
                if (dParameters.ElementAt(e.RowIndex).Key.Contains("Password"))
                {
                    e.Value = new String('*', e.Value.ToString().Length);
                    e.FormattingApplied = true;
                }

        }
    }
}
