using EWS.ComputerMonitor.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WW.Cad.Base;

namespace Txgy.Microseismic.BaseLib
{
    public class YuWuEventJsonModel: NotifyPropertyBase
    {
        /// <summary>
        /// 发震时刻
        /// </summary>
        public string otime { get; set; }
        /// <summary>
        /// E(经度lon)
        /// </summary>
        public double E { get; set; }
        /// <summary>
        /// N(纬度lat)
        /// </summary>
        public double N { get; set; }
        /// <summary>
        /// 垂深
        /// </summary>
        public double depth { get; set; }
        /// <summary>
        /// 里氏震级
        /// </summary>
        public double ml { get; set; }
        /// <summary>
        /// 残差
        /// </summary>
        public double rms { get; set; }
        /// <summary>
        /// 震相参数集合
        /// </summary>
        public List<YuWuPhasesJsonModel> phases { get; set; }
        /// <summary>
        /// 震级参数集合
        /// </summary>
        public List<YuWuAmpsJsonModel> amps { get; set; }

        public static YuWuEventJsonModel CreateEventModel(string fileName)
        {
            YuWuEventJsonModel yuwuJsonModel = new YuWuEventJsonModel();

            using (StreamReader sr = System.IO.File.OpenText(fileName))
            {
                JsonTextReader reader = new JsonTextReader(sr);

                JArray jArray = (JArray)JToken.ReadFrom(reader);
                //解析普通属性和数组混合的Json文件
                //数组0是普通属性集合
                JObject jobj = (JObject)jArray[0];
                yuwuJsonModel.otime = JsonConvert.SerializeObject(jobj["otime"]).Trim(new char[] { '\\', '\"' }).Substring(0, 23);
                yuwuJsonModel.E = double.Parse(jobj["lon"].ToString());
                yuwuJsonModel.N = double.Parse(jobj["lat"].ToString());
                yuwuJsonModel.depth = double.Parse(jobj["depth"].ToString());
                yuwuJsonModel.ml = double.Parse(jobj["ml"].ToString());
                yuwuJsonModel.rms = double.Parse(jobj["rms"].ToString());

                yuwuJsonModel.phases = new List<YuWuPhasesJsonModel>();
                yuwuJsonModel.amps = new List<YuWuAmpsJsonModel>();
                //textBoxTotalML.Text = strMl;
                JArray phaseArr = JArray.FromObject(jobj["phases"]);
                for (int i = 0; i < phaseArr.Count; i++)
                {
                    YuWuPhasesJsonModel phases = new YuWuPhasesJsonModel();
                    phases.id = phaseArr[i]["id"].ToString();
                    phases.name = phaseArr[i]["name"].ToString();
                    phases.atime = phaseArr[i]["atime"].ToString();
                    phases.resi = double.Parse(phaseArr[i]["resi"].ToString());
                    if (phaseArr[i]["first_motion_direct"]!=null)
                    {
                        phases.first_motion_direct = int.Parse(phaseArr[i]["first_motion_direct"].ToString());
                    }
                    if (phaseArr[i]["fmd_semi_period"]!=null)
                    {
                        phases.fmd_semi_period = double.Parse(phaseArr[i]["fmd_semi_period"].ToString());
                    }
                    
                    phases.deltaKm = double.Parse(phaseArr[i]["deltaKm"].ToString());
                    phases.disKm = double.Parse(phaseArr[i]["disKm"].ToString());
                    phases.model_time = double.Parse(phaseArr[i]["model_time"].ToString());
                    phases.weight = double.Parse(phaseArr[i]["weight"].ToString());
                    phases.prob = double.Parse(phaseArr[i]["prob"].ToString());
                    yuwuJsonModel.phases.Add(phases);
                    //Console.WriteLine(phaseArr[i].ToString());
                }
                JArray ampsArr = JArray.FromObject(jobj["amps"]);
                for (int i = 0; i < ampsArr.Count; i++)
                {
                    YuWuAmpsJsonModel amps = new YuWuAmpsJsonModel();
                    amps.id = ampsArr[i]["id"].ToString();
                    amps.name = ampsArr[i]["name"].ToString();
                    amps.atime = ampsArr[i]["atime"].ToString();
                    amps.mag_type = ampsArr[i]["mag_type"].ToString();
                    amps.mag_value = double.Parse(ampsArr[i]["mag_value"].ToString());
                    amps.amp = double.Parse(ampsArr[i]["amp"].ToString());
                    yuwuJsonModel.amps.Add(amps);
                    //Console.WriteLine(phaseArr[i].ToString());
                }
                reader.Close();
            }
            return yuwuJsonModel;
        }
    }
}
