//  
//   Rebex Sample Code License
// 
//   Copyright 2025, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;
using System.IO;
using System.Windows.Forms;
using Rebex.TerminalEmulation;

namespace Rebex.Samples
{
    /// <summary>
    /// The main form of the application. It cantains the ssh terminal control.
    /// </summary>
    public class MainForm : Form
    {
        /// <summary>
        /// Path to the config file at Local application data storage on the File system.
        /// </summary>
        private static readonly string _configFile = Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), 
            string.Format("Rebex{0}TerminalEmulation{0}SimpleWinFormClient.xml",Path.DirectorySeparatorChar));

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        private SshTerminalControl stcConsole;

        /// <summary>
        /// Initialize this class.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = true;
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
            this.stcConsole = new Rebex.TerminalEmulation.SshTerminalControl();
            this.SuspendLayout();
            // 
            // stcConsole
            // 
            this.stcConsole.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right)));
            this.stcConsole.BackColor = System.Drawing.Color.Magenta;
            this.stcConsole.Location = new System.Drawing.Point(0, 0);
            this.stcConsole.Name = "stcConsole";
            this.stcConsole.Recorder = null;
            this.stcConsole.ServerName = "";
            this.stcConsole.Size = new System.Drawing.Size(737, 400);
            this.stcConsole.TabIndex = 0;
            this.stcConsole.ServerParameters = null;
            this.stcConsole.ServerPort = 22;
            this.stcConsole.UserName = "";
            this.stcConsole.UserPassword = null;
            this.stcConsole.UserPrivateKey = null;
            // 
            // MainForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(737, 400);
            this.Controls.Add(this.stcConsole);
            this.Name = "MainForm";
            this.Text = "Rebex Simple SSH Winform Client";
            this.Load += new System.EventHandler(this.MainForm_Load);
            this.ResumeLayout(false);

        }

        #endregion

        /// <summary>
        /// Handles the form load event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            // show connection dialog
            Connection connection = new Connection(_configFile);

            // if not confirmed, exit the application
            if (connection.ShowDialog() != DialogResult.OK)
            {
                Close();
                return;
            }

            // initialize connection information with the values from the connection dialog
            stcConsole.ServerName = connection.HostName;
            stcConsole.UserName = connection.UserName;
            stcConsole.UserPassword = connection.Password;

            // try connecting
            try
            {
                stcConsole.Connect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Exception occured: {0}", ex));
                Close();
                return;
            }
        }
    }
}