using System.Diagnostics;
using System.IO;

namespace LJLib.Cmd
{
    public static class CMDHelper
    {
        public static string RunWithReturn(string cmd, string workingdir = null)
        {
            using (Process p = new Process())
            {
                if (!string.IsNullOrEmpty(workingdir) && Directory.Exists(workingdir))
                {
                    p.StartInfo.WorkingDirectory = workingdir;
                }

                RunCmd(p, cmd);

                string line = p.StandardOutput.ReadLine();
                while (line != cmd)
                {
                    line = p.StandardOutput.ReadLine();
                }

                string rslt = string.Empty;
                line = p.StandardOutput.ReadLine();
                while (line != "exit")
                {
                    if (string.IsNullOrEmpty(rslt))
                    {
                        rslt = line;
                    }
                    else
                    {
                        rslt += "\r\n" + line;
                    }
                    line = p.StandardOutput.ReadLine();
                }

                string errmsg = p.StandardError.ReadToEnd().Trim();
                if (!string.IsNullOrEmpty(errmsg))
                {
                    rslt += "\r\nERROR:\r\n" + errmsg;
                }
                return rslt;
            }
        }

        //public static string RunWithReturn(string cmd, Encoding encoding)
        //{
        //    Process p = new Process();
        //    RunCmd(p, cmd);
        //    string rslt = "";

        //    byte[] St = new byte[1024];
        //    using (MemoryStream ms = new MemoryStream())
        //    {
        //        int cnt = 0;
        //        while ((cnt = p.StandardOutput.BaseStream.Read(St, 0, St.Length)) > 0)
        //        {
        //            ms.Write(St, 0, cnt);
        //        }
        //        St = ms.ToArray();
        //    }
        //    string msg = Encoding.UTF8.GetString(St);

        //    using (StreamReader reader = new StreamReader(p.StandardOutput.BaseStream, encoding))
        //    using (StreamReader errReader = new StreamReader(p.StandardError.BaseStream, encoding))
        //    {
        //        rslt += reader.ReadToEnd() + "\r\n" + errReader.ReadToEnd();
        //    }
        //    return rslt;
        //}

        public static void Run(string cmd)
        {
            using (Process p = new Process())
            {
                RunCmd(p, cmd);
            }
        }

        private static void RunCmd(Process p, string cmd)
        {
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine("echo off");
            p.StandardInput.WriteLine(cmd);
            p.StandardInput.WriteLine("exit");
        }
    }
}
