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

namespace Web_Filename_Converter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dash_radio.Checked = true;
        }

        //Closes Program
        private void closeButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        //Gets Path to directory
        private void openFile_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog open = new FolderBrowserDialog();

            open.ShowDialog();

            if (open.SelectedPath != null)
                directoryPath.Text = open.SelectedPath;
        }

        //Converts files to web standards
        private void ConvertButton_Click(object sender, EventArgs e)
        {
            if (directoryPath.Text != "") {
                string[] files = Directory.GetFiles(directoryPath.Text);
                string filenames;
                string dash_underscore;
                string destination = directoryPath.Text;

                if(dash_radio.Checked)
                    dash_underscore = "-";
                else
                    dash_underscore = "_";

                for (int i = 0; i <= files.GetUpperBound(0); i++) {
                    filenames = Path.GetFileName(files[i]);
                    filenames = filenames.ToLower();
                    if (filenames.Contains(" "))
                        filenames = filenames.Replace(" ", dash_underscore);

                    File.Move(files[i], destination + "\\" + filenames);
                }
                
                MessageBox.Show("Files were succesfully renamed!", "Job Completed", MessageBoxButtons.OK);
            }
            else
                MessageBox.Show("Please select a directory!", "Error Detected in Input", MessageBoxButtons.OKCancel);
        }

        //About menu item
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
            MessageBox.Show("Web Filename Converter\n\u00a9 2016 Stephen Higley", "About", MessageBoxButtons.OK);
        }

        //Exit menu item
        private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
            Close();
        }
    }
}
