using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using System.IO;
using Txgy.EWS.Client.FocalMechanism.Model;
using Txgy.Microseismic.BaseLib.Models;
using FmGrid = Txgy.EWS.Client.FocalMechanism.Model.FmGrid;

namespace Txgy.EWS.Client.FocalMechanism.Core
{
    public class ProcessCore
    {
        public Model.FmGrid grid { get; set; }
        public delegate void dshowProgress(int current, int total);
        public event dshowProgress OnShowProgress;
        public ProcessCore(FmGrid g)
        {
            grid = g;
            //canvas = new CanvasFactory(grid);
        }

        public ComputationResult ComputeResult(FMMap map)
        {
            //check null point
            for (int j = 0; j < map.posStation.Count; j++)
            {
                int fj = grid.StationList.ToList<Model.FMStation>().FindIndex(fi => fi.Name==map.posStation[j]);
                if (fj == -1)
                {
                    map.posStation.RemoveAt(j);
                    j--;
                }
            }
            for (int j = 0; j < map.negStation.Count; j++)
            {
                int fj = grid.StationList.ToList<Model.FMStation>().FindIndex(fi => fi.Name == map.negStation[j]);
                if (fj == -1)
                {
                    map.negStation.RemoveAt(j);
                    j--;
                }
            }
           
            //识别震源机制性质
            if (map.posStation.Count != 0 || map.negStation.Count != 0)
            {
                LineFactory lf = new LineFactory(this.grid);
              
                //判断是否为走滑
                ComputationResult l = lf.generateResult(map);
                if (l != null)
                {
                   return l;
                }
                //判断是否为倾滑
                CrossLineFactory cf = new CrossLineFactory(this.grid);
                ComputationResult c = cf.generateResult(map);
                if (c != null)
                {
                     return c;
                }
                //判断是否为张性
                PolygonFactory pf = new PolygonFactory(this.grid);
                ComputationResult poly = pf.generateResult(map);
                if (poly != null)
                {
                    return poly;
                }
            }
            //无法识别返回张性震源,NE45
            ComputationResult polyUnknown = new ComputationResult();
            polyUnknown.Direction = 0;
            polyUnknown.FocalType = FocalMechanismType.unknown;
            map.T = FocalMechanismType.unknown;
            return polyUnknown;
          
        }
        public BatchOutputResult BatchOutputForTxt(string path, string prefix, int start, int end, List<FMMap> maps)
        {
            //ExcelEdit editor = new ExcelEdit();
            BatchOutputResult re = new BatchOutputResult();
            //Microsoft.Office.Interop.Excel.Worksheet ws = null;
            //Microsoft.Office.Interop.Excel.Worksheet wsUnhandled = null;
            StreamWriter sw;
            string waveTypeStr = "";
            //try
            {
                sw = new StreamWriter(path + "\\" + prefix + "震源机制数据.txt", false, Encoding.Default);
                sw.WriteLine("序号\t裂缝性质\t裂缝走向");
                
                //path = path + "\\" + prefix;
                //if (!Directory.Exists(path))
                //{
                //    Directory.CreateDirectory(path);
                //}
                //editor.Create();
                //ws = editor.GetSheet("Sheet1");
                //wsUnhandled = editor.AddSheet("Final Result");
                //editor.SetCellValue(ws, 1, 1, "序号");
                //editor.SetCellValue(ws, 1, 2, "裂缝性质");
                //editor.SetCellValue(ws, 1, 3, "裂缝走向");
                //editor.SetCellValue(ws, 1, 4, "倾滑方向");
                //editor.SetCellValue(ws, 1, 5, "X");
                //editor.SetCellValue(ws, 1, 6, "Y");
                //editor.SetCellValue(ws, 1, 7, "R");
                for (int i = start; i <= end; i++)
                {
                    if (i==28)
                    {
                        Console.WriteLine(1);
                    }
                    ComputationResult r = this.ComputeResult(maps[i]);
                    //r.bmp.Save(path + "\\" + prefix + "_" + (i + 1) + ".png", ImageFormat.Png);
                    //editor.SetCellValue(ws, i - start + 2, 1, i + 1);
                    //editor.SetCellValue(ws, i - start + 2, 5, maps[i].X);
                    //editor.SetCellValue(ws, i - start + 2, 6, maps[i].Y);
                    //editor.SetCellValue(ws, i - start + 2, 7, maps[i].R);
                    switch (r.FocalType)
                    {
                        case FocalMechanismType.dipSlip:
                            waveTypeStr = "倾滑";
                            //editor.SetCellValue(ws, i - start + 2, 2, "倾滑");
                            re.dipSlipCount++;
                            //if (r.angle < 90) editor.SetCellValue(ws, i - start + 2, 4, "NW" + r.angle);
                            //else editor.SetCellValue(ws, i - start + 2, 4, "NE" + (180 - r.angle));

                            break;
                        case FocalMechanismType.strikeSlip:
                            waveTypeStr = "走滑";
                            //editor.SetCellValue(ws, i - start + 2, 2, "走滑");
                            re.strikeSlipCount++;
                            break;
                        case FocalMechanismType.tensile:
                            waveTypeStr = "张性";
                            //editor.SetCellValue(ws, i - start + 2, 2, "张性");
                            re.tensileCount++;
                            break;
                        default:
                            waveTypeStr = "无法识别";
                            //editor.SetCellValue(ws, i - start + 2, 2, "张性");
                            re.unknownCount++;
                            //editor.SetCellValue(wsUnhandled, 4 + re.unknownCount, 1, i+1);
                            //re.unknownCount++;
                            continue;
                    }
                    //if (r.angle < 90) editor.SetCellValue(ws, i - start + 2, 3, "NE" + (90 - r.angle));
                    //else editor.SetCellValue(ws, i - start + 2, 3, "NW" + (r.angle - 90));

                    sw.WriteLine((i + 1).ToString() + "\t" + waveTypeStr + "\t" + ((int)r.FocalType).ToString() + "\t" + r.Direction.ToString());
                    if (OnShowProgress != null) this.OnShowProgress(i - start + 1, end - start + 1);
                }
                sw.Close();
                sw.Dispose();

                //editor.SetCellValue(wsUnhandled, 1, 1, "走滑");
                //editor.SetCellValue(wsUnhandled, 1, 2, "倾滑");
                //editor.SetCellValue(wsUnhandled, 1, 3, "张性");
                //editor.SetCellValue(wsUnhandled, 1, 4, "未处理");
                //editor.SetCellValue(wsUnhandled, 3, 1, "未处理:");
                //editor.SetCellValue(wsUnhandled, 2, 1, re.strikeSlipCount);
                //editor.SetCellValue(wsUnhandled, 2, 2, re.dipSlipCount);
                //editor.SetCellValue(wsUnhandled, 2, 3, re.tensileCount);
                //editor.SetCellValue(wsUnhandled, 2, 4, re.unknownCount);
                //editor.SaveAs(path + "\\" + prefix + "_Final");
            }
            //finally
            //{
            //    //editor.Close();
            //}
            return re;
        }
        /// <summary>
        /// 导出到Excel
        /// </summary>
        /// <param name="path"></param>
        /// <param name="prefix"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="maps"></param>
        /// <returns></returns>
        public BatchOutputResult BatchOutputForExcel(string path, string prefix, int start, int end, List<FMMap> maps)
        {
            BatchOutputResult re = new BatchOutputResult();
            //ExcelEdit editor = new ExcelEdit();
            //Microsoft.Office.Interop.Excel.Worksheet ws = null;
            //Microsoft.Office.Interop.Excel.Worksheet wsUnhandled = null;
            //StreamWriter sw;
            //string waveTypeStr = "";
            //try
            //{
            //    sw = new StreamWriter(path + "\\" + prefix + "震源机制数据.txt", false, Encoding.Default);
            //    sw.WriteLine("序号\t裂缝性质\t裂缝走向");

            //    path = path + "\\" + prefix;
            //    if (!Directory.Exists(path))
            //    {
            //        Directory.CreateDirectory(path);
            //    }
            //    editor.Create();
            //    ws = editor.GetSheet("Sheet1");
            //    wsUnhandled = editor.AddSheet("Final Result");
            //    editor.SetCellValue(ws, 1, 1, "序号");
            //    editor.SetCellValue(ws, 1, 2, "裂缝性质");
            //    editor.SetCellValue(ws, 1, 3, "裂缝走向");
            //    editor.SetCellValue(ws, 1, 4, "倾滑方向");
            //    editor.SetCellValue(ws, 1, 5, "X");
            //    editor.SetCellValue(ws, 1, 6, "Y");
            //    editor.SetCellValue(ws, 1, 7, "R");
            //    for (int i = start; i <= end; i++)
            //    {
            //        ComputationResult r = this.ComputeResult(maps[i]);
            //        r.bmp.Save(path + "\\" + prefix + "_" + (i + 1) + ".png", ImageFormat.Png);
            //        editor.SetCellValue(ws, i - start + 2, 1, i + 1);
            //        editor.SetCellValue(ws, i - start + 2, 5, maps[i].X);
            //        editor.SetCellValue(ws, i - start + 2, 6, maps[i].Y);
            //        editor.SetCellValue(ws, i - start + 2, 7, maps[i].R);
            //        switch (r.type)
            //        {
            //            case WaveType.dipSlip:
            //                waveTypeStr = "倾滑";
            //                editor.SetCellValue(ws, i - start + 2, 2, "倾滑");
            //                re.dipSlipCount++;
            //                if (r.angle < 90) editor.SetCellValue(ws, i - start + 2, 4, "NW" + r.angle);
            //                else editor.SetCellValue(ws, i - start + 2, 4, "NE" + (180 - r.angle));
                            
            //                break;
            //            case WaveType.strikeSlip:
            //                waveTypeStr = "走滑";
            //                editor.SetCellValue(ws, i - start + 2, 2, "走滑");
            //                re.strikeSlipCount++;
            //                break;
            //            case WaveType.tensile:
            //                waveTypeStr = "张性";
            //                editor.SetCellValue(ws, i - start + 2, 2, "张性");
            //                re.tensileCount++;
            //                break;
            //            default:
            //                waveTypeStr = "张性";
            //                editor.SetCellValue(ws, i - start + 2, 2, "张性");
            //                re.tensileCount++;
            //                //editor.SetCellValue(wsUnhandled, 4 + re.unknownCount, 1, i+1);
            //                //re.unknownCount++;
            //                continue;
            //        }
            //        if (r.angle < 90) editor.SetCellValue(ws, i - start + 2, 3, "NE" + (90 - r.angle));
            //        else editor.SetCellValue(ws, i - start + 2, 3, "NW" + (r.angle - 90));

            //        sw.WriteLine((i + 1).ToString() + "\t"+waveTypeStr+"\t" + r.angle.ToString());
            //        if(OnShowProgress!=null) this.OnShowProgress(i - start + 1, end - start + 1);
            //    }
            //    sw.Close();
            //    sw.Dispose();

            //    editor.SetCellValue(wsUnhandled, 1, 1, "走滑");
            //    editor.SetCellValue(wsUnhandled, 1, 2, "倾滑");
            //    editor.SetCellValue(wsUnhandled, 1, 3, "张性");
            //    editor.SetCellValue(wsUnhandled, 1, 4, "未处理");
            //    editor.SetCellValue(wsUnhandled, 3, 1, "未处理:");
            //    editor.SetCellValue(wsUnhandled, 2, 1, re.strikeSlipCount);
            //    editor.SetCellValue(wsUnhandled, 2, 2, re.dipSlipCount);
            //    editor.SetCellValue(wsUnhandled, 2, 3, re.tensileCount);
            //    editor.SetCellValue(wsUnhandled, 2, 4, re.unknownCount);
            //    editor.SaveAs(path + "\\" + prefix + "_Final");
            //}
            //finally
            //{
            //    editor.Close();                
            //}
            return re;
        }
    }
}
