using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UdPdfEncrypt
{
    public partial class FrmMain : Form
    {
        //const string PASSWORD = "123abc";       //加密密码

        public FrmMain()
        {
            InitializeComponent();
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                this.textBox1.Text = this.openFileDialog1.FileName;
                this.textBox2.Text = System.IO.Path.GetDirectoryName(this.textBox1.Text);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.SelectedPath = this.textBox2.Text;
            if (this.folderBrowserDialog1.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                this.textBox2.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }

        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Length == 0 && this.textBox2.Text.Length == 0)
                return;

            string encCmd = System.IO.Path.Combine(Application.StartupPath,"tools\\UdPdfTools.exe");
            string args = string.Format("-input {0} -output {1} -password {2}", this.textBox1.Text, this.textBox2.Text, this.tbxPassword.Text);
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = encCmd;
            p.StartInfo.Arguments = args;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.CreateNoWindow = true;//不显示程序窗口
            p.Start();
            p.WaitForExit();
            string strOutput = p.StandardOutput.ReadToEnd();
            if (p.ExitCode == 0)
            {
                MessageBox.Show("加密成功:\r\n" + strOutput, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("加密失败，错误码：" + p.ExitCode + "\r\n" + strOutput,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnConfigEncrypt_Click(object sender, EventArgs e)
        {
            string encCmd = System.IO.Path.Combine(Application.StartupPath, "tools\\UdPdfTools.exe");
            string args = string.Format("-encrypt {0}", this.textBox3.Text);
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = encCmd;
            p.StartInfo.Arguments = args;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.CreateNoWindow = true;//不显示程序窗口
            p.Start();
            p.WaitForExit();
            string strOutput = p.StandardOutput.ReadToEnd();
            this.textBox4.Text = strOutput;
        }
    }
}
