using System;
using System.IO;
using System.Windows.Forms;

namespace dotnet_demo
{
    public partial class FileItemForm : Form
    {
        public FileItemForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var fileContent = string.Empty;
            var filePath = string.Empty;

            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.FileName = textBoxFileName.Text;
                openFileDialog.Filter = "PDF Documents (*.pdf)|*.pdf|All files (*.*)|*.*";
                openFileDialog.FilterIndex = 0;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filePath = openFileDialog.FileName;
                    textBoxFileName.Text = filePath;
                }
            }
        }

        private void textBoxFileName_TextChanged(object sender, EventArgs e)
        {
            buttonOK.Enabled = File.Exists(textBoxFileName.Text);
        }

        public string GetFileName()
        {
            return textBoxFileName.Text;
        }

        public string GetPassword()
        {
            return textBoxPassword.Text;
        }

        public string GetPageList()
        {
            return textBoxPageList.Text;
        }

        public void SetValues(string fileName, string password, string pageList)
        {
            textBoxFileName.Text = fileName;
            textBoxPassword.Text = password;
            textBoxPageList.Text = pageList;
        }

        private void FixPageList()
        {
            string validPageList = "";
            string pageList = textBoxPageList.Text;
            for (int x = 0; x < pageList.Length; x++)
            {
                if (((pageList[x] >= '0') && (pageList[x] <= '9')) || (pageList[x] == ',') || (pageList[x] == '-'))
                {
                    validPageList += pageList[x];
                }
            }
            textBoxPageList.Text = validPageList;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            FixPageList();
        }

        private void textBoxPageList_Leave(object sender, EventArgs e)
        {

            FixPageList();
        }
    }
}
