using System;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace m3uTool
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class M3uMakeByDirectory : Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private readonly Container components;

        private TextBox m3uDirectoryTextBox;
        private Button m3uMakeButton;
        private TextBox outputTextBox;
        private Button selectDirectoryButton;

        public M3uMakeByDirectory()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Application.Run(new M3uMakeByDirectory());
        }

        private void selectDirectoryButton_Click(object sender, EventArgs e)
        {
            var fbd = new FolderBrowserDialog();
            DialogResult dialogResult = fbd.ShowDialog(this);

            if (dialogResult == DialogResult.OK)
            {
                m3uDirectoryTextBox.Text = fbd.SelectedPath;
            }
            UpdateRunButton();
        }

        private void UpdateRunButton()
        {
            if (m3uDirectoryTextBox.Text != null && m3uDirectoryTextBox.Text != "" &&
                Directory.Exists(m3uDirectoryTextBox.Text))
            {
                m3uMakeButton.Enabled = true;
            }
            else
                m3uMakeButton.Enabled = false;
        }

        private void CreateM3uThread()
        {
            CreateM3uRecursive(new DirectoryInfo(m3uDirectoryTextBox.Text));
        }

        private void CreateM3uRecursive(DirectoryInfo info)
        {
            var cm3u = new M3uCreate(info);
            cm3u.Create();
            outputTextBox.AppendText("Created " + cm3u.OutputFilename + Environment.NewLine);
            foreach (DirectoryInfo subDir in info.GetDirectories())
            {
                CreateM3uRecursive(subDir);
            }
        }

        private void m3uMakeButton_Click(object sender, EventArgs e)
        {
            var t = new Thread(CreateM3uThread);
            t.Start();
        }

        #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.m3uDirectoryTextBox = new System.Windows.Forms.TextBox();
            this.outputTextBox = new System.Windows.Forms.TextBox();
            this.selectDirectoryButton = new System.Windows.Forms.Button();
            this.m3uMakeButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // m3uDirectoryTextBox
            // 
            this.m3uDirectoryTextBox.Enabled = false;
            this.m3uDirectoryTextBox.Location = new System.Drawing.Point(8, 8);
            this.m3uDirectoryTextBox.Name = "m3uDirectoryTextBox";
            this.m3uDirectoryTextBox.Size = new System.Drawing.Size(176, 20);
            this.m3uDirectoryTextBox.TabIndex = 0;
            this.m3uDirectoryTextBox.Text = "";
            // 
            // outputTextBox
            // 
            this.outputTextBox.Anchor =
                ((System.Windows.Forms.AnchorStyles)
                 ((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                   | System.Windows.Forms.AnchorStyles.Right)));
            this.outputTextBox.Location = new System.Drawing.Point(8, 88);
            this.outputTextBox.Multiline = true;
            this.outputTextBox.Name = "outputTextBox";
            this.outputTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.outputTextBox.Size = new System.Drawing.Size(272, 168);
            this.outputTextBox.TabIndex = 1;
            this.outputTextBox.Text = "";
            // 
            // selectDirectoryButton
            // 
            this.selectDirectoryButton.Location = new System.Drawing.Point(208, 8);
            this.selectDirectoryButton.Name = "selectDirectoryButton";
            this.selectDirectoryButton.TabIndex = 2;
            this.selectDirectoryButton.Text = "Select Dir...";
            this.selectDirectoryButton.Click += new System.EventHandler(this.selectDirectoryButton_Click);
            // 
            // m3uMakeButton
            // 
            this.m3uMakeButton.Enabled = false;
            this.m3uMakeButton.Location = new System.Drawing.Point(208, 40);
            this.m3uMakeButton.Name = "m3uMakeButton";
            this.m3uMakeButton.TabIndex = 3;
            this.m3uMakeButton.Text = "Make M3u";
            this.m3uMakeButton.Click += new System.EventHandler(this.m3uMakeButton_Click);
            // 
            // MakeSubdir
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.m3uMakeButton);
            this.Controls.Add(this.selectDirectoryButton);
            this.Controls.Add(this.outputTextBox);
            this.Controls.Add(this.m3uDirectoryTextBox);
            this.Name = "MakeSubdir";
            this.Text = "M3u Make";
            this.ResumeLayout(false);
        }

        #endregion
    }
}