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

namespace Project.Dialog
{
    public partial class fSystem : Form
    {
        public bool shutdown = false;
        public fSystem()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UTIL.RunProcess(@"c:\windows\system32\shutdown.exe", "-r -t 5");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            UTIL.RunProcess(@"c:\windows\system32\shutdown.exe", "-s -t 5");
        }

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

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void lbMsg_Click(object sender, EventArgs e)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            System.Diagnostics.Process prc = new System.Diagnostics.Process();
			System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo("explorer")
			{
				Arguments = path
			};
			prc.StartInfo = si;
            prc.Start();
        }

        private void arLabel1_Click(object sender, EventArgs e)
        {
            SendKeys.Send("^{ESC}");
        }

        private void arLabel6_Click(object sender, EventArgs e)
        {
            SendKeys.Send("^+{ESC}");
        }

        private void arLabel9_Click(object sender, EventArgs e)
        {
            shutdown = true;
            PUB.SystemShutdown(10);
            this.Close();
          
        }

        private void arLabel10_Click(object sender, EventArgs e)
        {
            shutdown = true;
            PUB.SystemReboot(10);
            this.Close();
        }

        private void arLabel4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void arLabel2_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder sb = new StringBuilder();

            sb.AppendLine("Current : " + System.Diagnostics.Process.GetCurrentProcess().ProcessName);

            foreach (var prc in System.Diagnostics.Process.GetProcesses())
            {
                if (prc.ProcessName.StartsWith("svchost")) continue;
                sb.Append("  " + prc.ProcessName);
            }
            UTIL.MsgI(sb.ToString(),true);
        }

        private void fSystem_Load(object sender, EventArgs e)
        {
            this.label1.Text = "Patch Version " + PUB.PatchVersion + $"IP:{PUB.IP}";
            this.label2.Text = "HMI Version " + Application.ProductVersion.ToString();
        }

        private void arLabel3_Click(object sender, EventArgs e)
        {
            var file = System.IO.Path.Combine(UTIL.CurrentPath, "Emulator.exe");
            if(System.IO.File.Exists(file)==false)
            {
                UTIL.MsgE("에물레이터 실행 파일이 없습니다", true);
                return;
            }

            UTIL.RunProcess(file);
        }

        private void arLabel5_Click(object sender, EventArgs e)
        {
            //현재 폴더에서 dll 과, amkor.exe 파일을 압축한다
            var path = new System.IO.DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
            var files_dll = path.GetFiles("*.dll");
            var file_exe = System.IO.Path.Combine(path.FullName, "amkor.exe");
            if(System.IO.File.Exists(file_exe)==false)
            {
                UTIL.MsgE("실행파일 amkor.exe 가 없습니다.");
                return;
            }

            var veri = Application.ProductVersion.Split('.');
            var newfilename = "Patch_AGV_" + veri[0] + veri[1] + veri[2] + "_" + veri[3] + ".zip";
            
            // 임시 폴더 생성
            var tempDir = Path.Combine(Path.GetTempPath(), "PatchTemp");
            if (Directory.Exists(tempDir))
                Directory.Delete(tempDir, true);
            Directory.CreateDirectory(tempDir);

            // 파일 복사
            File.Copy(file_exe, Path.Combine(tempDir, "amkor.exe"));
            foreach (var filedll in files_dll)
                File.Copy(filedll.FullName, Path.Combine(tempDir, filedll.Name));

            // 압축 파일 생성
            ZipFile.CreateFromDirectory(tempDir, newfilename);

            // 임시 폴더 삭제
            Directory.Delete(tempDir, true);

            UTIL.MsgI("다음 패치 파일이 생성됨\n" + newfilename);
        }

        private void arLabel7_Click(object sender, EventArgs e)
        {
            PUB.SystemReboot(5,true);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            //mapeditor
            var fn = new  System.IO.FileInfo(@".\test\AGVMapEditor.exe");
            if (fn.Exists == false) return;
            UTIL.RunExplorer(fn.FullName);
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            var f = new Dialog.fACSCommand();
            f.TopMost = true;
            f.Show();
            this.Close();
        }
    }
}
