using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Txgy.EWS.Client.Common;
using Txgy.EWS.Client.FocalMechanism.Model;
using Txgy.EWS.Client.PageModule.Services;
using Txgy.Microseismic.BaseLib.Models;
using Txgy.EWS.Client.FocalMechanism.Core;
using NPOI.SS.Formula.Functions;
using FmGrid = Txgy.EWS.Client.FocalMechanism.Model.FmGrid;
using Txgy.EWS.Client.Models;

namespace Txgy.EWS.Client.PageModule.Common
{
    public class EventFunc
    {
        public static void ComputeFM(MmEvent mmEvent)
        {

            string downLoadStr = mmEvent.EventTimeStr;
            string datePath = downLoadStr.Substring(0, 4) + downLoadStr.Substring(5, 2) + downLoadStr.Substring(8, 2);
            string jsonStr = "HA." + downLoadStr.Substring(0, 4) + downLoadStr.Substring(5, 2)
                + downLoadStr.Substring(8, 2) + downLoadStr.Substring(10, 3)
                + downLoadStr.Substring(14, 2) + downLoadStr.Substring(17, 2) + ".01" + GlobalConfig.DataTypeString + ".json";
            string jsonFilePath = GlobalConfig.ProjectConfig.MseedFilePath + "\\" + datePath + "\\";
            if (!File.Exists(jsonFilePath + jsonStr))
            {
                new DownloadJsonFile().Download(downLoadStr, jsonFilePath, jsonStr, GlobalConfig.UseWaveDataTable);
            }
            using (StreamReader sr = System.IO.File.OpenText(jsonFilePath + jsonStr))
            {
                mmEvent.JsonFile = jsonFilePath + jsonStr;

                JsonTextReader reader = new JsonTextReader(sr);
                JArray jArray = (JArray)JToken.ReadFrom(reader);
                //解析普通属性和数组混合的Json文件
                //数组0是普通属性集合
                JObject jobj = (JObject)jArray[0];
                mmEvent.Energy = GetEnergy(mmEvent.ML);
                mmEvent.Phases = new Dictionary<string, int>();
                JArray phaseArr = JArray.FromObject(jobj["phases"]);
                for (int i = 0; i < phaseArr.Count; i++)
                {
                    if (phaseArr[i]["first_motion_direct"] != null)
                    {
                        mmEvent.Phases.Add(phaseArr[i]["id"].ToString().Substring(3, 3),
                            int.Parse(phaseArr[i]["first_motion_direct"].ToString()));
                    }
                }
            }
            FMMap fmMap = CreateFM(mmEvent, GlobalConfig.ProjectConfig.WorkArea.EMin, GlobalConfig.ProjectConfig.WorkArea.NMin);
            ComputationResult cr = GlobalConfig.fmCore.ComputeResult(fmMap);
            if (cr == null)
                return;
            mmEvent.FocalType = cr.FocalType;
            mmEvent.Direction = cr.Direction;
        }
        public static void ComputeFM(GridItemEventResult eventResult)
        {

            string downLoadStr = eventResult.EventTime;
            string datePath = downLoadStr.Substring(0, 4) + downLoadStr.Substring(5, 2) + downLoadStr.Substring(8, 2);
            string jsonStr = "HA." + downLoadStr.Substring(0, 4) + downLoadStr.Substring(5, 2)
                + downLoadStr.Substring(8, 2) + downLoadStr.Substring(10, 3)
                + downLoadStr.Substring(14, 2) + downLoadStr.Substring(17, 2) + ".01" + GlobalConfig.DataTypeString + ".json";
            string jsonFilePath = GlobalConfig.ProjectConfig.MseedFilePath + "\\" + datePath + "\\";
            if (!File.Exists(jsonFilePath + jsonStr))
            {
                new DownloadJsonFile().Download(downLoadStr, jsonFilePath, jsonStr, GlobalConfig.UseWaveDataTable);
            }
            using (StreamReader sr = System.IO.File.OpenText(jsonFilePath + jsonStr))
            {
                eventResult.JsonFile = jsonFilePath + jsonStr;

                JsonTextReader reader = new JsonTextReader(sr);
                JArray jArray = (JArray)JToken.ReadFrom(reader);
                //解析普通属性和数组混合的Json文件
                //数组0是普通属性集合
                JObject jobj = (JObject)jArray[0];
                eventResult.SetEnergy();
                eventResult.Phases = new Dictionary<string, int>();
                JArray phaseArr = JArray.FromObject(jobj["phases"]);
                for (int i = 0; i < phaseArr.Count; i++)
                {
                    if (phaseArr[i]["first_motion_direct"] != null)
                    {
                        eventResult.Phases.Add(phaseArr[i]["id"].ToString().Substring(3, 3),
                            int.Parse(phaseArr[i]["first_motion_direct"].ToString()));
                    }
                }
            }
            FMMap fmMap = CreateFM(eventResult, GlobalConfig.ProjectConfig.WorkArea.EMin, GlobalConfig.ProjectConfig.WorkArea.NMin);
            ComputationResult cr = GlobalConfig.fmCore.ComputeResult(fmMap);
            if (cr == null)
                return;
            eventResult.SourceChara = (int)cr.FocalType;
            eventResult.Direction = cr.Direction;
        }
        public static FMMap CreateFM(GridItemEventResult gier, double BaseX, double BaseY)
        {
            FMMap fmMap = new FMMap();
            fmMap.X = gier.X - BaseX;
            fmMap.Y = gier.Y - BaseY;
            fmMap.R = gier.RMS;
            foreach (var item in gier.Phases)
            {
                if (item.Value == -1)
                {
                    fmMap.negStation.Add(item.Key);
                }
                else
                {
                    fmMap.posStation.Add(item.Key);
                }
            }
            return fmMap;
        }
        public static FMMap CreateFM(MmEvent mmEvent, double BaseX, double BaseY)
        {
            FMMap fmMap = new FMMap();
            fmMap.X = mmEvent.X - BaseX;
            fmMap.Y = mmEvent.Y - BaseY;
            fmMap.R = mmEvent.RMS;
            foreach (var item in mmEvent.Phases)
            {
                if (item.Value == -1)
                {
                    fmMap.negStation.Add(item.Key);
                }
                else
                {
                    fmMap.posStation.Add(item.Key);
                }
            }
            return fmMap;
        }

        public static double GetEnergy(double ml)
        {
            double mi = Math.Round(4.83 + 1.47 * ml, 4);
            return Math.Round(Math.Pow(10, mi), 0);
        }
    }
}
