//  
//   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.Drawing;
using System.Windows.Forms;
using Rebex.Security.Certificates;
using Rebex.Net;

namespace Rebex.Samples
{
    public partial class ResolveInvalidKeyDialog : Form
    {
        public ResolveInvalidKeyDialog(
            ResolveDialogMode dialogMode,
            string failureReason, 
            SignatureHashAlgorithm hashAlgorithm, 
            SshHostKeyAlgorithm storedKeyAlgorithm, 
            string storedFingerprint,
            SshHostKeyAlgorithm serverKeyAlgorithm,
            string serverKeyFingerprint)
        {
            InitializeComponent();
            lblReasons.Text = failureReason;
            lblHashAlgorighm.Text = hashAlgorithm.ToString();			
            lblServerKeyAlgorithm.Text = serverKeyAlgorithm.ToString();
            lblServerKeyFingerprint.Text = serverKeyFingerprint;

            if (dialogMode == ResolveDialogMode.EntryDiffer)
            {
                lblReasons.ForeColor = Color.Red;
                lblStoredKeyAlgorithm.Text = storedKeyAlgorithm.ToString();
                lblStoredFingerprint.Text = storedFingerprint;
            }
            else
            {
                lblStoredKeyAlgorithm.Visible = false;
                lblStoredFingerprint.Visible = false;

                lblStoredKeyAlgorithmLabel.Visible = false;
                lblStoredKeyFingerprintLabel.Visible = false;
            }
        }

        public KeyVerifyingMode GetChoosenMode()
        {
            if (DialogResult != DialogResult.OK)
                throw new InvalidOperationException("Cannot resolve choosen mode when the dialog has not been confirmed by the OK button.");

            if (rbAcceptAny.Checked)
                return KeyVerifyingMode.AcceptAnyKey;
            if (rbStoreLocally.Checked)
                return KeyVerifyingMode.LocalyStoredFingerprint;
            if (rbReject.Checked)
                return KeyVerifyingMode.RejectKey;

            throw new InvalidOperationException("Unexpected state when resolving CertfificateVerifyingMode.");

        }

        private void ResolveInvalidCertficiateDialog_Activated(object sender, EventArgs e)
        {
            // recalculate height
            int delta = this.Height - pnlLayout.Height;
            pnlLayout.Size = new Size(pnlLayout.Width, pnlButtons.Top + pnlButtons.Height);
            this.Size = new Size(this.Width, pnlLayout.Height + delta);
            this.MinimumSize = new Size(this.MinimumSize.Width, this.Height);
        }

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

        /// <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()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.pnlLayout = new System.Windows.Forms.Panel();
            this.rbReject = new System.Windows.Forms.RadioButton();
            this.pnlButtons = new System.Windows.Forms.Panel();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnOk = new System.Windows.Forms.Button();
            this.rbAcceptAny = new System.Windows.Forms.RadioButton();
            this.rbStoreLocally = new System.Windows.Forms.RadioButton();
            this.label3 = new System.Windows.Forms.Label();
            this.pnlDetails = new System.Windows.Forms.Panel();
            this.lblServerKeyAlgorithm = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.lblServerKeyFingerprint = new System.Windows.Forms.Label();
            this.label11 = new System.Windows.Forms.Label();
            this.lblStoredKeyAlgorithm = new System.Windows.Forms.Label();
            this.lblStoredKeyFingerprintLabel = new System.Windows.Forms.Label();
            this.label10 = new System.Windows.Forms.Label();
            this.lblStoredFingerprint = new System.Windows.Forms.Label();
            this.lblHashAlgorighm = new System.Windows.Forms.Label();
            this.lblStoredKeyAlgorithmLabel = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.lblReasons = new System.Windows.Forms.Label();
            this.pnlLayout.SuspendLayout();
            this.pnlButtons.SuspendLayout();
            this.pnlDetails.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Dock = System.Windows.Forms.DockStyle.Top;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(124, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Server key is not trusted:";
            // 
            // pnlLayout
            // 
            this.pnlLayout.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.pnlLayout.Controls.Add(this.rbReject);
            this.pnlLayout.Controls.Add(this.pnlButtons);
            this.pnlLayout.Controls.Add(this.rbAcceptAny);
            this.pnlLayout.Controls.Add(this.rbStoreLocally);
            this.pnlLayout.Controls.Add(this.label3);
            this.pnlLayout.Controls.Add(this.pnlDetails);
            this.pnlLayout.Controls.Add(this.label2);
            this.pnlLayout.Controls.Add(this.lblReasons);
            this.pnlLayout.Controls.Add(this.label1);
            this.pnlLayout.Location = new System.Drawing.Point(12, 12);
            this.pnlLayout.Name = "pnlLayout";
            this.pnlLayout.Size = new System.Drawing.Size(490, 386);
            this.pnlLayout.TabIndex = 1;
            // 
            // rbReject
            // 
            this.rbReject.AutoSize = true;
            this.rbReject.Dock = System.Windows.Forms.DockStyle.Top;
            this.rbReject.Location = new System.Drawing.Point(0, 309);
            this.rbReject.Name = "rbReject";
            this.rbReject.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0);
            this.rbReject.Size = new System.Drawing.Size(490, 17);
            this.rbReject.TabIndex = 7;
            this.rbReject.Text = "Reject the server key";
            this.rbReject.UseVisualStyleBackColor = true;
            // 
            // pnlButtons
            // 
            this.pnlButtons.Controls.Add(this.btnCancel);
            this.pnlButtons.Controls.Add(this.btnOk);
            this.pnlButtons.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.pnlButtons.Location = new System.Drawing.Point(0, 353);
            this.pnlButtons.Name = "pnlButtons";
            this.pnlButtons.Size = new System.Drawing.Size(490, 33);
            this.pnlButtons.TabIndex = 8;
            // 
            // btnCancel
            // 
            this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(415, 6);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 11;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.UseVisualStyleBackColor = true;
            // 
            // btnOk
            // 
            this.btnOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnOk.Location = new System.Drawing.Point(334, 6);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new System.Drawing.Size(75, 23);
            this.btnOk.TabIndex = 10;
            this.btnOk.Text = "OK";
            this.btnOk.UseVisualStyleBackColor = true;
            // 
            // rbAcceptAny
            // 
            this.rbAcceptAny.AutoSize = true;
            this.rbAcceptAny.CheckAlign = System.Drawing.ContentAlignment.TopLeft;
            this.rbAcceptAny.Dock = System.Windows.Forms.DockStyle.Top;
            this.rbAcceptAny.Location = new System.Drawing.Point(0, 292);
            this.rbAcceptAny.Name = "rbAcceptAny";
            this.rbAcceptAny.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0);
            this.rbAcceptAny.Size = new System.Drawing.Size(490, 17);
            this.rbAcceptAny.TabIndex = 6;
            this.rbAcceptAny.Text = "Ignore the failure and accept the key";
            this.rbAcceptAny.UseVisualStyleBackColor = true;
            // 
            // rbStoreLocally
            // 
            this.rbStoreLocally.AutoSize = true;
            this.rbStoreLocally.CheckAlign = System.Drawing.ContentAlignment.TopLeft;
            this.rbStoreLocally.Checked = true;
            this.rbStoreLocally.Dock = System.Windows.Forms.DockStyle.Top;
            this.rbStoreLocally.Location = new System.Drawing.Point(0, 275);
            this.rbStoreLocally.Name = "rbStoreLocally";
            this.rbStoreLocally.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0);
            this.rbStoreLocally.Size = new System.Drawing.Size(490, 17);
            this.rbStoreLocally.TabIndex = 5;
            this.rbStoreLocally.TabStop = true;
            this.rbStoreLocally.Text = "Accept the server key (and remember it)";
            this.rbStoreLocally.TextAlign = System.Drawing.ContentAlignment.TopLeft;
            this.rbStoreLocally.UseVisualStyleBackColor = true;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Dock = System.Windows.Forms.DockStyle.Top;
            this.label3.Location = new System.Drawing.Point(0, 252);
            this.label3.Name = "label3";
            this.label3.Padding = new System.Windows.Forms.Padding(0, 5, 0, 5);
            this.label3.Size = new System.Drawing.Size(204, 23);
            this.label3.TabIndex = 4;
            this.label3.Text = "You can solve it by one of following ways:";
            // 
            // pnlDetails
            // 
            this.pnlDetails.BackColor = System.Drawing.SystemColors.ControlLightLight;
            this.pnlDetails.Controls.Add(this.lblServerKeyAlgorithm);
            this.pnlDetails.Controls.Add(this.label6);
            this.pnlDetails.Controls.Add(this.lblServerKeyFingerprint);
            this.pnlDetails.Controls.Add(this.label11);
            this.pnlDetails.Controls.Add(this.lblStoredKeyAlgorithm);
            this.pnlDetails.Controls.Add(this.lblStoredKeyFingerprintLabel);
            this.pnlDetails.Controls.Add(this.label10);
            this.pnlDetails.Controls.Add(this.lblStoredFingerprint);
            this.pnlDetails.Controls.Add(this.lblHashAlgorighm);
            this.pnlDetails.Controls.Add(this.lblStoredKeyAlgorithmLabel);
            this.pnlDetails.Dock = System.Windows.Forms.DockStyle.Top;
            this.pnlDetails.Location = new System.Drawing.Point(0, 82);
            this.pnlDetails.Name = "pnlDetails";
            this.pnlDetails.Padding = new System.Windows.Forms.Padding(10);
            this.pnlDetails.Size = new System.Drawing.Size(490, 170);
            this.pnlDetails.TabIndex = 3;
            // 
            // lblServerKeyAlgorithm
            // 
            this.lblServerKeyAlgorithm.Location = new System.Drawing.Point(116, 37);
            this.lblServerKeyAlgorithm.Name = "lblServerKeyAlgorithm";
            this.lblServerKeyAlgorithm.Size = new System.Drawing.Size(143, 17);
            this.lblServerKeyAlgorithm.TabIndex = 13;
            this.lblServerKeyAlgorithm.Text = "DSS";
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(0, 54);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(110, 13);
            this.label6.TabIndex = 12;
            this.label6.Text = "Server key fingerprint:";
            // 
            // lblServerKeyFingerprint
            // 
            this.lblServerKeyFingerprint.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.lblServerKeyFingerprint.Location = new System.Drawing.Point(116, 54);
            this.lblServerKeyFingerprint.Name = "lblServerKeyFingerprint";
            this.lblServerKeyFingerprint.Size = new System.Drawing.Size(371, 42);
            this.lblServerKeyFingerprint.TabIndex = 11;
            this.lblServerKeyFingerprint.Text = "AB-01-F8-etc.";
            // 
            // label11
            // 
            this.label11.AutoSize = true;
            this.label11.Location = new System.Drawing.Point(0, 37);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(106, 13);
            this.label11.TabIndex = 10;
            this.label11.Text = "Server key algorithm:";
            // 
            // lblStoredKeyAlgorithm
            // 
            this.lblStoredKeyAlgorithm.Location = new System.Drawing.Point(116, 106);
            this.lblStoredKeyAlgorithm.Name = "lblStoredKeyAlgorithm";
            this.lblStoredKeyAlgorithm.Size = new System.Drawing.Size(143, 17);
            this.lblStoredKeyAlgorithm.TabIndex = 9;
            this.lblStoredKeyAlgorithm.Text = "DSS";
            // 
            // lblStoredKeyFingerprintLabel
            // 
            this.lblStoredKeyFingerprintLabel.AutoSize = true;
            this.lblStoredKeyFingerprintLabel.Location = new System.Drawing.Point(0, 123);
            this.lblStoredKeyFingerprintLabel.Name = "lblStoredKeyFingerprintLabel";
            this.lblStoredKeyFingerprintLabel.Size = new System.Drawing.Size(110, 13);
            this.lblStoredKeyFingerprintLabel.TabIndex = 8;
            this.lblStoredKeyFingerprintLabel.Text = "Stored key fingerprint:";
            // 
            // label10
            // 
            this.label10.AutoSize = true;
            this.label10.Location = new System.Drawing.Point(0, 10);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(112, 13);
            this.label10.TabIndex = 8;
            this.label10.Text = "Used hash algorigthm:";
            // 
            // lblStoredFingerprint
            // 
            this.lblStoredFingerprint.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.lblStoredFingerprint.Location = new System.Drawing.Point(116, 123);
            this.lblStoredFingerprint.Name = "lblStoredFingerprint";
            this.lblStoredFingerprint.Size = new System.Drawing.Size(371, 42);
            this.lblStoredFingerprint.TabIndex = 7;
            this.lblStoredFingerprint.Text = "AB-01-F8-etc.";
            // 
            // lblHashAlgorighm
            // 
            this.lblHashAlgorighm.Location = new System.Drawing.Point(116, 10);
            this.lblHashAlgorighm.Name = "lblHashAlgorighm";
            this.lblHashAlgorighm.Size = new System.Drawing.Size(143, 17);
            this.lblHashAlgorighm.TabIndex = 7;
            this.lblHashAlgorighm.Text = "SHA-1";
            // 
            // lblStoredKeyAlgorithmLabel
            // 
            this.lblStoredKeyAlgorithmLabel.AutoSize = true;
            this.lblStoredKeyAlgorithmLabel.Location = new System.Drawing.Point(0, 106);
            this.lblStoredKeyAlgorithmLabel.Name = "lblStoredKeyAlgorithmLabel";
            this.lblStoredKeyAlgorithmLabel.Size = new System.Drawing.Size(106, 13);
            this.lblStoredKeyAlgorithmLabel.TabIndex = 6;
            this.lblStoredKeyAlgorithmLabel.Text = "Stored key algorithm:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Dock = System.Windows.Forms.DockStyle.Top;
            this.label2.Location = new System.Drawing.Point(0, 64);
            this.label2.Name = "label2";
            this.label2.Padding = new System.Windows.Forms.Padding(0, 0, 0, 5);
            this.label2.Size = new System.Drawing.Size(42, 18);
            this.label2.TabIndex = 2;
            this.label2.Text = "Details:";
            // 
            // lblReasons
            // 
            this.lblReasons.Dock = System.Windows.Forms.DockStyle.Top;
            this.lblReasons.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.lblReasons.Location = new System.Drawing.Point(0, 13);
            this.lblReasons.Name = "lblReasons";
            this.lblReasons.Padding = new System.Windows.Forms.Padding(10);
            this.lblReasons.Size = new System.Drawing.Size(490, 51);
            this.lblReasons.TabIndex = 1;
            this.lblReasons.Text = "reasons...";
            // 
            // ResolveInvalidKeyDialog
            // 
            this.AcceptButton = this.btnOk;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(514, 408);
            this.Controls.Add(this.pnlLayout);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.MinimumSize = new System.Drawing.Size(530, 39);
            this.Name = "ResolveInvalidKeyDialog";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Untrusted Server Key";
            this.Activated += new System.EventHandler(this.ResolveInvalidCertficiateDialog_Activated);
            this.pnlLayout.ResumeLayout(false);
            this.pnlLayout.PerformLayout();
            this.pnlButtons.ResumeLayout(false);
            this.pnlDetails.ResumeLayout(false);
            this.pnlDetails.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Panel pnlLayout;
        private System.Windows.Forms.Button btnOk;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Label lblReasons;
        private System.Windows.Forms.Panel pnlDetails;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.RadioButton rbAcceptAny;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.RadioButton rbStoreLocally;
        private System.Windows.Forms.RadioButton rbReject;
        private System.Windows.Forms.Panel pnlButtons;
        private System.Windows.Forms.Label lblStoredKeyAlgorithmLabel;
        private System.Windows.Forms.Label label10;
        private System.Windows.Forms.Label lblHashAlgorighm;
        private System.Windows.Forms.Label lblStoredFingerprint;
        private System.Windows.Forms.Label lblStoredKeyFingerprintLabel;
        private System.Windows.Forms.Label lblStoredKeyAlgorithm;
        private System.Windows.Forms.Label lblServerKeyAlgorithm;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label lblServerKeyFingerprint;
        private System.Windows.Forms.Label label11;
    }
}
