

    using System.ComponentModel;
using System.Diagnostics;
namespace DragonVideoPlayer
{
    public class DragDropForm : Form
    {
        private TextBox textbox_main_mes;

        private TextBox textbox_count;
        private TextBox textbox_sousu;
        private ProgressBar progressBar1;

        private TextBox textbox_add_folder;

        private String _folder_list;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public String folder_list
        {
            get => _folder_list;
            set => _folder_list = value;
        }





        public DragDropForm()
        {
            InitializeUI();
            this.Load += Form1_Load;

            this.FormClosing += close;
        }
        private Drag_form_state _Drag_form_state = new Drag_form_state();
        private void InitializeUI()
        {
            // フォーム設定（Window相当）
            this.Text = "ドラッグ&ドロップ登録フォーム";
            this.Width = 800;
            this.Height = 450;
            this.BackColor = Color.FromArgb(229, 229, 229);
            this.AllowDrop = true;

            this.DragOver += Grid_PreviewDragOver;
            this.DragDrop += Grid_Drop;

            // ===== textbox_main_mes =====
            textbox_main_mes = new TextBox();
            textbox_main_mes.Location = new Point(80, 204);
            textbox_main_mes.Size = new Size(270, 200);
            textbox_main_mes.Multiline = true;
            this.Controls.Add(textbox_main_mes);

            // ===== 初期化ボタン =====
            Button btn_format = new Button();
            btn_format.Text = "初期化";
            btn_format.Location = new Point(95, 114);
            btn_format.Size = new Size(50, 30);
            btn_format.Width = 70;
            btn_format.Click += Button_format;
            this.Controls.Add(btn_format);

            Label mes2Label = new Label();
            mes2Label.Text = "このテキストボックスにフォルダをD&Dしてください";
            mes2Label.Location = new Point(380, 20);
            mes2Label.AutoSize = true;
            this.Controls.Add(mes2Label);

            // ===== textbox_add_folder =====

            textbox_add_folder = new TextBox();
            textbox_add_folder.Location = new Point(380, 44);
            textbox_add_folder.Size = new Size(270, 200);
            textbox_add_folder.Multiline = true;
            textbox_add_folder.AllowDrop = true;
            textbox_add_folder.DragOver += TextBox_DragOver;
            textbox_add_folder.DragDrop += Grid_Drop_textbox;
            this.Controls.Add(textbox_add_folder);

            Label mesLabel = new Label();
            mesLabel.Text = "フォームにファイルかフォルダをD&Dしてください。";
            mesLabel.Location = new Point(10, 24);
            mesLabel.AutoSize = true;
            this.Controls.Add(mesLabel);

            // ===== デッドリンクチェック =====
            Button btn_link_check = new Button();
            btn_link_check.Text = "デッドリンクチェック";
            btn_link_check.Location = new Point(165, 114);
            btn_link_check.Size = new Size(85, 30);
            btn_link_check.Width = 100;
            btn_link_check.Click += Button_link_check;
            this.Controls.Add(btn_link_check);

            // ===== ProgressBar =====
            progressBar1 = new ProgressBar();
            progressBar1.Location = new Point(80, 154);
            progressBar1.Size = new Size(255, 15);
            this.Controls.Add(progressBar1);

            // ===== 停止 =====
            Button btn_stop = new Button();
            btn_stop.Text = "停止";
            btn_stop.Location = new Point(105, 64);
            btn_stop.Size = new Size(50, 30);
            btn_stop.Click += Button_stop;
            this.Controls.Add(btn_stop);

            // ===== 登録 =====
            Button btn_folder_add = new Button();
            btn_folder_add.Text = "登録";
            btn_folder_add.Location = new Point(175, 64);
            btn_folder_add.Size = new Size(50, 30);
            btn_folder_add.Click += Button_folder_add;
            this.Controls.Add(btn_folder_add);

            // ===== textbox_count =====
            textbox_count = new TextBox();
            textbox_count.Location = new Point(88, 181);
            textbox_count.Size = new Size(42, 20);
            this.Controls.Add(textbox_count);

            // ===== textbox_sousu =====
            textbox_sousu = new TextBox();
            textbox_sousu.Location = new Point(145, 181);
            textbox_sousu.Size = new Size(42, 20);
            this.Controls.Add(textbox_sousu);

            // ===== サムネイル作成 =====
            Button btn_thumbnail = new Button();
            btn_thumbnail.Text = "サムネイル作成";
            btn_thumbnail.Location = new Point(265, 114);
            btn_thumbnail.Size = new Size(85, 30);
            btn_thumbnail.Width = 100;
            btn_thumbnail.Click += Button_thum;
            this.Controls.Add(btn_thumbnail);
        }

        private void close(object sender, FormClosingEventArgs e)
        {

            config_class.save_config_file("ドラッグエリアのフォルダのパス", textbox_add_folder.Text);


        }

        private void Form1_Load(object sender, EventArgs e)
        {

            if (!string.IsNullOrEmpty(config_class.config_text_read("ドラッグエリアのフォルダのパス")))
            {
                textbox_add_folder.Text = config_class.config_text_read("ドラッグエリアのフォルダのパス");

            }


        }
        public void prog(int value, int selelct_c)
        {
            try
            {
                if (selelct_c == 1)
                {
                    progressBar1.Minimum = value;
                }
                if (selelct_c == 2)
                {
                    progressBar1.Maximum = value;
                }
                if (selelct_c == 3)
                {
                    if (value <= progressBar1.Maximum)
                    {
                        progressBar1.Value = value;
                    }

                }

            }
            catch (Exception ex)
            {

            }
        }
        // ===== イベント（WPF相当） =====

        private void Grid_PreviewDragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void Grid_Drop(object sender, DragEventArgs e)
        {
            string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop);

            if (paths.Length > 0)
            {

                try
                {
                    Debug.WriteLine(paths.Length);
                    _Drag_form_state._d_d_file_list = new List<String>(paths);
                    Debug.WriteLine(_Drag_form_state._d_d_file_list.Count.ToString());
                }
                catch (System.Exception ex)
                {

                }



                Task.Run(() =>
                {
                    sql_class ins = new sql_class(this, _Drag_form_state);
                    ins.db_add_start();

                });





            }




        }



        private void TextBox_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void Grid_Drop_textbox(object sender, DragEventArgs e)
        {
            string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop);

            if (paths.Length > 0)
            {

                try
                {

                    String folder_path = paths[0];
                    if (System.IO.Directory.Exists(folder_path) == true)
                    {
                        textbox_add_folder.AppendText(folder_path + Environment.NewLine);
                    }
                }
                catch (System.Exception ex)
                {

                }



            }

        }
        public void textbox_add(string mes_val, int mes_sel)
        {
            Debug.WriteLine(mes_val);
            if (mes_sel == 1)
            {
                textbox_main_mes.Text = "";

            }
            else if (mes_sel == 2)
            {
                //textbox_main_mes.AppendText(Environment.NewLine);

                textbox_main_mes.AppendText(mes_val + Environment.NewLine);

            }

            else if (mes_sel == 3)
            {
                //textbox_main_mes.AppendText(Environment.NewLine);

                textbox_count.Text = mes_val;
            }
            else if (mes_sel == 4)
            {
                textbox_sousu.Text = mes_val;
            }

        }
        private void Button_format(object sender, EventArgs e)
        {

            sql_class ins = new sql_class(this, _Drag_form_state);
            ins.format();


        }
        private void Button_thum(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                sql_class ins = new sql_class(this, _Drag_form_state);
                ins.thum_creat_thread();

            });





        }
        private void Button_link_check(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                sql_class ins = new sql_class(this, _Drag_form_state);
                ins.dead_link_check();

            });





        }

        private void Button_stop(object sender, EventArgs e)
        {

            _Drag_form_state.stop_flag = true;
        }

        private void Button_folder_add(object sender, EventArgs e)
        {
            _Drag_form_state._folder_list = textbox_add_folder.Text;


            Task.Run(() =>
            {
                sql_class ins = new sql_class(this, _Drag_form_state);
                ins.sql_creat_start();

            });

        }
    }
}