using System;
using System.Diagnostics;

namespace Thousandto.DIY
{
    public class CmdOption
    {
        public static void DoCommand(string argument)
        {
            DoCommand(argument, "", null);
        }

        public static void DoCommand(string argument, string workDir, EventHandler finishCallback)
        {
            if(!argument.StartsWith("/c"))
            {
                argument = "/c" + " " + argument;
            }
            DoCommand("cmd.exe", argument, workDir, finishCallback);
        }

        public static void DoCommand(string excuteFileName, string argument, string workDir, EventHandler finishCallback)
        {
            ProcessStartInfo start = new ProcessStartInfo(excuteFileName, argument);
            start.WorkingDirectory = workDir;
            start.CreateNoWindow = false;
            start.ErrorDialog = true;
            start.UseShellExecute = true;


            start.RedirectStandardInput = false;
            start.RedirectStandardOutput = false;
            start.RedirectStandardError = false;
            //start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
            //start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;



            var compileProcess = new Process();
            compileProcess.EnableRaisingEvents = true;

            if (finishCallback != null)
            {
                compileProcess.Exited += finishCallback;
            }
            else
            {
                compileProcess.WaitForExit();
            }
            //_compileProcess.ErrorDataReceived += ErrorDataReceived;
            //_compileProcess.OutputDataReceived += OutputDataReceived;

            compileProcess.StartInfo = start;
            compileProcess.Start();
        }
    }
}