//  
//   Rebex Sample Code License
// 
//   Copyright 2026, 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Rebex.IO.Compression;

namespace Rebex.Samples
{
    /// <summary>
    /// A ZIP archive options dialog.
    /// </summary>
    public partial class OptionsDialog : Form
    {
        // values of form controls (set when the form is shown)
        private int _compressionLevel;
        private int _compressionMethod;
        private int _encryptionAlgorithm;
        private string _password;
        private string _comment;

        /// <summary>
        /// Gets the selected compression method.
        /// </summary>
        private CompressionMethod CompressionMethod
        {
            get
            {
                switch (comboCompression.SelectedIndex)
                {
                    case 1: return CompressionMethod.Deflate;
                    case 2: return CompressionMethod.EnhancedDeflate;
                    default: return CompressionMethod.NoCompression;
                }
            }
        }

        /// <summary>
        /// Gets the selected encryption algorithm.
        /// </summary>
        private EncryptionAlgorithm EncryptionAlgorithm
        {
            get
            {
                switch (comboEncryption.SelectedIndex)
                {
                    case 1: return EncryptionAlgorithm.Aes256;
                    case 2: return EncryptionAlgorithm.Aes192;
                    case 3: return EncryptionAlgorithm.Aes128;
                    case 4: return EncryptionAlgorithm.Zip20;
                    default: return EncryptionAlgorithm.None;
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the dialog.
        /// </summary>
        public OptionsDialog()
        {
            InitializeComponent();

            // select default values
            trackCompressionLevel.Value = 6; // Level 6
            comboCompression.SelectedIndex = 1; // Deflate
            comboEncryption.SelectedIndex = 0; // No encryption
        }

        /// <summary>
        /// Initializes the dialog with the specified ZIP archive.
        /// </summary>
        /// <param name="archive">ZIP archive to use for initialization.</param>
        public void Initialize(ZipArchive archive)
        {
            txtComment.Text = archive.Comment;

            ApplyFormValues(archive);
        }

        /// <summary>
        /// Displays the dialog with the specified message.
        /// </summary>
        /// <param name="owner">Owner of the dialog.</param>
        /// <param name="archive">Archive to set the selected values to.</param>
        public void ShowDialog(Control owner, ZipArchive archive)
        {
            if (archive == null || archive.IsReadOnly)
                txtComment.Enabled = false;
            else
                txtComment.Enabled = true;

            RememberFormValues();

            if (base.ShowDialog(owner) == DialogResult.OK)
                ApplyFormValues(archive);
            else
                RestoreFormValues();
        }

        /// <summary>
        /// Saves the form values (to be able to restore them when the Cancel button is pressed).
        /// </summary>
        private void RememberFormValues()
        {
            _compressionLevel = trackCompressionLevel.Value;
            _compressionMethod = comboCompression.SelectedIndex;
            _encryptionAlgorithm = comboEncryption.SelectedIndex;
            _password = txtPassword.Text;
            _comment = txtComment.Text;
        }

        /// <summary>
        /// Restores the form values. (Called when the Cancel button is pressed.)
        /// </summary>
        private void RestoreFormValues()
        {
            trackCompressionLevel.Value = _compressionLevel;
            comboCompression.SelectedIndex = _compressionMethod;
            comboEncryption.SelectedIndex = _encryptionAlgorithm;
            txtPassword.Text = _password;
            txtComment.Text = _comment;
        }

        /// <summary>
        /// Sets ZIP archive options to the dialog values.
        /// </summary>
        /// <param name="archive">ZIP archive whose optiosn to set.</param>
        private void ApplyFormValues(ZipArchive archive)
        {
            if (archive == null)
                return;

            archive.CompressionLevel = trackCompressionLevel.Value;
            archive.CompressionMethod = this.CompressionMethod;
            archive.EncryptionAlgorithm = this.EncryptionAlgorithm;

            if (txtPassword.Text.Length == 0)
                archive.Password = null;
            else
                archive.Password = txtPassword.Text;

            if (!archive.IsReadOnly)
                archive.Comment = txtComment.Text;
        }

        /// <summary>
        /// Handles an event to display correct ToolTip of compression level.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trackCompressionLevel_ValueChanged(object sender, EventArgs e)
        {
            string msg = trackCompressionLevel.Value.ToString();
            switch (trackCompressionLevel.Value)
            {
                case 0: msg = msg + " No compression"; break;
                case 1: msg = msg + " Worst compression (fast)"; break;
                case 6: msg = msg + " Default value"; break;
                case 9: msg = msg + " Best compression (slow)"; break;
                default:
                    break;
            }
            toolTip.SetToolTip(trackCompressionLevel, msg);
        }
    }
}