using System;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace StartServerWPF.Models
{
    public class JsonParser
    {

        public static string systemConfigPath = "SystemConfig.json";
        public static string serverVision = "20230517";
        public const string workareaPath = "serverprogram\\workarea";

        public SystemConfigModel ReadSystemJsonFile(string jsonFile)
        {
            //try
            {
                //StreamReader sr = File.OpenText(jsonFile);
                //JsonTextReader reader = new JsonTextReader(sr);
                //JObject jobj = (JObject)JToken.ReadFrom(reader);
                SystemConfigModel sc = new SystemConfigModel();
                string str = File.ReadAllText(jsonFile);
                sc = JsonConvert.DeserializeObject<SystemConfigModel>(str);
                //sc.SystemLogPath = jobj["SystemLogPath"].ToString();
                //sc.DataSavePath = jobj["DataSavePath"].ToString();

                ////RemoteDb
                //JObject jordb = JObject.Parse(jobj["remoteDb"].ToString());
                //sc.remoteDb = new RemoteDataBase();
                //sc.remoteDb.ServerAddress = jordb["ServerAddress"].ToString();
                //sc.remoteDb.ServerPort = jordb["ServerPort"].ToString();
                //sc.remoteDb.UserName = jordb["UserName"].ToString();
                //sc.remoteDb.Password = jordb["Password"].ToString();
                //sc.remoteDb.DataBase = jordb["DataBase"].ToString();
                //sc.remoteDb.TableName = jordb["TableName"].ToString();

                ////VPN
                //JObject jovpn = JObject.Parse(jobj["vpnInfo"].ToString());
                //sc.vpnInfo = new VpnInfo();
                //sc.vpnInfo.VpnName = jovpn["VpnName"].ToString();
                //sc.vpnInfo.VpnIP = jovpn["VpnIP"].ToString();
                //sc.vpnInfo.VpnUserName = jovpn["VpnUserName"].ToString();
                //sc.vpnInfo.VpnPsw = jovpn["VpnPsw"].ToString();

                ////server
                //JObject joserver = JObject.Parse(jobj["proServer"].ToString());
                //sc.proServer = new ProcessInfo();
                //sc.proServer.ProName = "server";
                //sc.proServer.ProPath = joserver["ProPath"].ToString();
                //sc.proServer.ProParams = joserver["ProParams"].ToString();

                ////gw.recvftp
                //JObject jorecvftp = JObject.Parse(jobj["proRecv"].ToString());
                //sc.proRecv = new ProcessInfo();
                //sc.proRecv.ProName = "gw.recvftp";
                //sc.proRecv.ProPath = jorecvftp["ProPath"].ToString();
                //sc.proRecv.ProParams = jorecvftp["ProParams"].ToString();
                //sc.proRecv.JsonPath = jorecvftp["JsonPath"].ToString();

                ////gw.apms
                //JObject joapms = JObject.Parse(jobj["proApms"].ToString());
                //sc.proApms = new ProcessInfo();
                //sc.proApms.ProName = "gw.apms";
                //sc.proApms.ProPath = joapms["ProPath"].ToString();
                //sc.proApms.ProParams = joapms["ProParams"].ToString();
                //sc.proApms.JsonPath = joapms["JsonPath"].ToString();

                ////gw.monitor
                //JObject jomonitor = JObject.Parse(jobj["proMonitor"].ToString());
                //sc.proMonitor = new ProcessInfo();
                //sc.proMonitor.ProName = "gw.monitor";
                //sc.proMonitor.ProPath = jomonitor["ProPath"].ToString();
                //sc.proMonitor.ProParams = jomonitor["ProParams"].ToString();

                ////gw.plot
                //JObject joplot = JObject.Parse(jobj["proPlot"].ToString());
                //sc.proPlot = new ProcessInfo();
                //sc.proPlot.ProName = "gw.plot";
                //sc.proPlot.ProPath = joplot["ProPath"].ToString();

                //reader.Close();
                //sr.Close();
                return sc;
            }
            //catch (Exception)
            //{
            //    MessageBox.Show("没有系统配置文件！");
            //    return null;
            //}
        }
        public static void WriteSystemConfigFile(string jsonFile, object sc)
        {
            string str = JsonConvert.SerializeObject(sc, Formatting.Indented);
            File.WriteAllText(jsonFile, str);
        }

        public static void UpdateApmsJson(string apmsJsonPath, string savePath)
        {
            using (StreamReader reader = new StreamReader(apmsJsonPath, Encoding.UTF8))
            {
                string saveStr = reader.ReadToEnd();
                // strInd = saveStr.IndexOf("station");

                int strInd = saveStr.IndexOf("Main.savepath") - 1;
                string str1 = saveStr.Substring(strInd);
                strInd = str1.IndexOf(",");
                string str2 = str1.Substring(0, strInd);
                strInd = str2.IndexOf(":");
                string strs1 = str2.Substring(0, strInd);
                string strs2 = str2.Substring(strInd + 1);
                savePath = savePath.Replace("\\", "\\\\");
                saveStr = saveStr.Replace(strs2, $"\"{savePath}\"");
                reader.Close();
                File.WriteAllText(apmsJsonPath, saveStr);
            }
        }
        public static void UpdateRecvJson(string recvJsonPath, string savePath)
        {
            StreamReader reader = new StreamReader(recvJsonPath, Encoding.UTF8);
            string saveStr = reader.ReadToEnd();
            int strInd = saveStr.IndexOf("savepath") - 1;
            string str1 = saveStr.Substring(strInd);
            strInd = str1.IndexOf(",");
            string str2 = str1.Substring(0, strInd);
            strInd = str2.IndexOf(":");
            string strs1 = str2.Substring(0, strInd);
            string strs2 = str2.Substring(strInd + 1);
            savePath = savePath.Replace("\\", "\\\\");
            saveStr = saveStr.Replace(strs2, $"\"{savePath}\"");
            reader.Close();
            File.WriteAllText(recvJsonPath, saveStr);
        }

        public static string CreateMonitorStartParam(string dataPath, DateTime beginTime)
        {
            string webPath = dataPath.Replace("\\", "/");
            string moniSp = "-sta n1105_station_xyz.csv -port 8086 -rdir " + webPath +
                " -btime " + beginTime.GetDateTimeFormats('s')[0].ToString() + " >outMonitor.txt";
            return moniSp;
        }

        public static T DeepCopy<T>(T obj)
        {
            object retval;
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                //序列化成流
                bf.Serialize(ms, obj);
                ms.Seek(0, SeekOrigin.Begin);
                //反序列化成对象
                retval = bf.Deserialize(ms);
                ms.Close();
            }
            return (T)retval;
        }
        //public static T DeepCopy<T>(T obj)
        //{
        //    object retval;
        //    using (MemoryStream ms = new MemoryStream())
        //    {
        //        DataContractSerializer ser = new DataContractSerializer(typeof(T));
        //        ser.WriteObject(ms, obj);
        //        ms.Seek(0, SeekOrigin.Begin);
        //        retval = ser.ReadObject(ms);
        //        ms.Close();
        //    }
        //    return (T)retval;
        //}
    }
}
