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

using System.Diagnostics;
using System.IO;
using System.Collections;


using AppClass;
using System.Runtime.Remoting.Messaging;

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



        App app = new App();
        Dictionary<string, Form2> arrForm = new Dictionary<string, Form2>();


        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                string[] shezhi = File.ReadAllLines(Directory.GetCurrentDirectory() + "\\shezhi.txt");
                textBox1.Text = shezhi[0].Split('=')[1];
                textBox2.Text = shezhi[1].Split('=')[1];
                textBox3.Text = shezhi[2].Split('=')[1];
                textBox4.Text = shezhi[3].Split('=')[1];
                textBox5.Text = shezhi[4].Split('=')[1];
                textBox6.Text = shezhi[5].Split('=')[1];
                textBox7.Text = shezhi[6].Split('=')[1];
                textBox8.Text = shezhi[7].Split('=')[1];
            }
            catch
            {
            }
            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            GuanBi();
        }






        /// <summary>
        /// 获取ID
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            ArrayList arr = app.huoquID();
            if (arr.Count == 0) { 
                listBox1.Items.Add("没有设备连接");
                return;
            }
            
            foreach (string s in arr)
            {
                listBox1.Items.Add(s);
            }
        }






        /// <summary>
        /// 运行/关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "运行")
            {
                button2.Text = "关闭";
                YunXing();
            }
            else
            {
                button2.Text = "运行";
                GuanBi();
            }
        }


        private void button4_Click(object sender, EventArgs e)
        {
            string path = Directory.GetCurrentDirectory() + "\\shezhi.txt";
            string[] txt = new string[8];
            txt[0] = "0=" + textBox1.Text;//番茄小说阅读时间
            txt[1] = "1=" + textBox2.Text;//番茄小说收金时间
            txt[2] = "2=" + textBox3.Text;//7猫小说阅读时间
            txt[3] = "3=" + textBox4.Text;//7猫小说收金时间
            txt[4] = "4=" + textBox5.Text;//百度极速视频时间
            txt[5] = "5=" + textBox6.Text;//百度极速收金时间
            txt[6] = "6=" + textBox7.Text;//快手视频时间
            txt[7] = "7=" + textBox8.Text;//快手收金时间
            File.WriteAllLines(path, txt);
        }



        private void listBox1_Click(object sender, EventArgs e)
        {
            if (!arrForm.Keys.Contains(listBox1.Text)) return;
            //激活对应窗体
            arrForm[listBox1.Text].Activate();
        }



        /// <summary>
        /// 运行，开启线程任务
        /// </summary>
        private void YunXing() 
        {

            foreach (string s in listBox1.Items) {
                Form2 form2 = new Form2();
                form2.Text = s;
                form2.Show();
                arrForm.Add(s, form2);
                Task.Delay(1000).Wait();
            }
          
        }


        /// <summary>
        /// 关闭，所有线程任务
        /// </summary>
        private void GuanBi()
        {
            foreach (Form2 s in arrForm.Values)
            {
                s.Close();
            }
            app.guanbiAdb();
            app.guanbiCmd();
        }




        


        /*
         
         thread = new Thread(() => {});
         thread.Start();

         同步线程延时
         Thread.Sleep(5000);

         异步线程延时
         Task.Delay(2000).Wait();
         
         
         线程委托主线程，读写控件
         this.Invoke(new MethodInvoker(delegate () { 
             textBox1.Text += "task";
         }));
         
         
         
         
         
         
         */







        /// <summary>
        /// 测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>


        private void button3_Click(object sender, EventArgs e)
        {
            
          

        }

        
    }
}
