//  
//   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.Text;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using Rebex.TerminalEmulation;

namespace Rebex.Samples
{
    /// <summary>
    /// The main form of the application. It contains the terminal control.
    /// </summary>
    public class MainForm : Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        private System.Windows.Forms.FontDialog terminalFontDialog;
        private System.Windows.Forms.OpenFileDialog terminalOpenFileDialog;
        private System.Windows.Forms.ToolStripMenuItem menuItemFile;
        private System.Windows.Forms.ToolStripMenuItem menuItemOpen;
        private System.Windows.Forms.ToolStripMenuItem menuItemTools;
        private System.Windows.Forms.ToolStripMenuItem menuItemFont;
        private System.Windows.Forms.ToolStripMenuItem menuItemOptions;
        private Rebex.TerminalEmulation.TerminalControl tcConsole;
        private System.Windows.Forms.MenuStrip mmenu;
        private System.Windows.Forms.ToolStripMenuItem menuItemExit;
        private System.Windows.Forms.ToolStripMenuItem menuItemClearScreen;

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

            CheckForIllegalCrossThreadCalls = true;

            // set the default encoding
            tcConsole.Options.Encoding = Encoding.UTF8;

            // set the size of the form to 80 columns and 25 rows
            ClientSize = tcConsole.GetControlSize(80, 25);

            // register the RemoteResize event 
            tcConsole.RemoteResize += Console_RemoteResize;
        }

        /// <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.terminalFontDialog = new System.Windows.Forms.FontDialog();
            this.terminalOpenFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.mmenu = new System.Windows.Forms.MenuStrip();
            this.menuItemFile = new System.Windows.Forms.ToolStripMenuItem();
            this.menuItemOpen = new System.Windows.Forms.ToolStripMenuItem();
            this.menuItemClearScreen = new System.Windows.Forms.ToolStripMenuItem();
            this.menuItemTools = new System.Windows.Forms.ToolStripMenuItem();
            this.menuItemFont = new System.Windows.Forms.ToolStripMenuItem();
            this.menuItemOptions = new System.Windows.Forms.ToolStripMenuItem();
            this.tcConsole = new Rebex.TerminalEmulation.TerminalControl();
            this.menuItemExit = new System.Windows.Forms.ToolStripMenuItem();
            this.mmenu.SuspendLayout();
            this.SuspendLayout();
            // 
            // terminalOpenFileDialog
            // 
            this.terminalOpenFileDialog.Filter = "ANSI files (*.ans;*.ansi)|*.ans;*.ansi|All files (*.*)|*.*";
            this.terminalOpenFileDialog.InitialDirectory = "..\\..\\SampleFiles";
            // 
            // mmenu
            // 
            this.mmenu.Items.AddRange(new System.Windows.Forms.ToolStripMenuItem[] {
                                                                                  this.menuItemFile,
                                                                                  this.menuItemTools});
            // 
            // menuItemFile
            // 
            this.menuItemFile.MergeIndex = 0;
            this.menuItemFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripMenuItem[] {
                                                                                         this.menuItemOpen,
                                                                                         this.menuItemClearScreen,
                                                                                         this.menuItemExit});
            this.menuItemFile.Text = "&File";
            // 
            // menuItemOpen
            // 
            this.menuItemOpen.MergeIndex = 0;
            this.menuItemOpen.Text = "&Open...";
            this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click);
            // 
            // menuItemClearScreen
            // 
            this.menuItemClearScreen.MergeIndex = 1;
            this.menuItemClearScreen.Text = "&Clear screen";
            this.menuItemClearScreen.Click += new System.EventHandler(this.menuItemClearScreen_Click);
            // 
            // menuItemTools
            // 
            this.menuItemTools.MergeIndex = 1;
            this.menuItemTools.DropDownItems.AddRange(new System.Windows.Forms.ToolStripMenuItem[] {
                                                                                          this.menuItemFont,
                                                                                          this.menuItemOptions});
            this.menuItemTools.Text = "&Tools";
            // 
            // menuItemFont
            // 
            this.menuItemFont.MergeIndex = 0;
            this.menuItemFont.Text = "&Font...";
            this.menuItemFont.Click += new System.EventHandler(this.menuItemFont_Click);
            // 
            // menuItemOptions
            // 
            this.menuItemOptions.MergeIndex = 1;
            this.menuItemOptions.Text = "&Options...";
            this.menuItemOptions.Click += new System.EventHandler(this.menuItemOptions_Click);
            // 
            // tcConsole
            // 
            this.tcConsole.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.tcConsole.AutoAdjustTerminalSize = true;
            this.tcConsole.BackColor = System.Drawing.Color.Magenta;
            this.tcConsole.Location = new System.Drawing.Point(0, 27);
            this.tcConsole.Name = "tcConsole";
            this.tcConsole.Recorder = null;
            this.tcConsole.ScrollbackResetOnDisplayActivity = true;
            this.tcConsole.ScrollBarEnabled = true;
            this.tcConsole.Size = new System.Drawing.Size(737, 400);
            this.tcConsole.TabIndex = 0;
            this.tcConsole.UserInputEnabled = true;
            // 
            // menuItemExit
            // 
            this.menuItemExit.MergeIndex = 2;
            this.menuItemExit.Text = "E&xit";
            this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
            // 
            // MainForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(736, 425);
            this.Controls.Add(this.tcConsole);
            this.Controls.Add(this.mmenu);
            this.KeyPreview = true;
            this.MainMenuStrip = this.mmenu;
            this.Name = "MainForm";
            this.Text = "Rebex ANSI Viewer";
            this.mmenu.ResumeLayout(false);
            this.mmenu.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        /// <summary>
        /// Handles the console RemoteResize event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Console_RemoteResize(object sender, RemoteResizeEventArgs e)
        {
            // set the size of the form to the size of the console
            ClientSize = tcConsole.GetControlSize(e.Columns, e.Rows);

            // console screen is not set immediately, set it manually right now
            tcConsole.SetScreenSize(e.Columns, e.Rows);
        }

        /// <summary>
        /// Handles the 'Font' menu item click event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemFont_Click(object sender, System.EventArgs e)
        {
            // show the font dialog
            if (terminalFontDialog.ShowDialog() != DialogResult.OK)
                return;

            // check whether the selected font family is supported
            if(!TerminalFont.IsSupportedFamily(terminalFontDialog.Font.FontFamily))
            {
                MessageBox.Show(string.Format("Font {0} is not supported. Please choose another.", 
                    terminalFontDialog.Font.FontFamily.Name));
                return;
            }

            // set selected font to the console
            tcConsole.TerminalFont = new TerminalFont(terminalFontDialog.Font.FontFamily, terminalFontDialog.Font.Size);
        }

        /// <summary>
        /// Handles the 'Option' menu item click event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemOptions_Click(object sender, System.EventArgs e)
        {
            // show options dialog
            Options options = new Options(tcConsole.Options);
            options.ShowDialog();
        }

        /// <summary>
        /// Handles the 'Open' menu item click event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemOpen_Click(object sender, System.EventArgs e)
        {
            // open the file dialog
            if (terminalOpenFileDialog.ShowDialog() != DialogResult.OK)
                return;

            // initialize new StreamReader according to the chosen file and terminal encoding
            using (StreamReader reader = new StreamReader(
                       terminalOpenFileDialog.FileName, 
                       tcConsole.Options.Encoding))
            {
                // read the data from the file
                string data = reader.ReadToEnd();

                // write the data on the terminal screen
                tcConsole.Screen.Write(data);
            }
        }

        /// <summary>
        /// Handles the 'Clear screen' menu item click event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemClearScreen_Click(object sender, System.EventArgs e)
        {
            // clear the terminal screen
            tcConsole.Screen.Clear();
        }

        /// <summary>
        /// Handles the 'Exit' menu item click event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemExit_Click(object sender, System.EventArgs e)
        {
            Close();
        }
    }
}