using System;
using System.Collections.Generic;

namespace Txgy.EWS.Client.FocalMechanism.Model
{
    public class FmGrid
    {
        public List<FMStation> StationList { get; set; }
        public int StationCount { get; set; }
        public double xmin { get; set; }
        public double xmax { get; set; }
        public double ymin { get; set; }
        public double ymax { get; set; }

        public int Xmin { get; set; }
        public int Xmax { get; set; }
        public int Ymin { get; set; }
        public int Ymax { get; set; }
        public int GridLength { get; set; }
        public int LineCount { get; set; }

        public double[,] distance { get; set; }

        //Summary
        //initalize the grid length and line number, make sure use zero as center
        //
        public void initZeroCenter()
        {
            double max = Math.Max(xmax, 0 - xmin);
            max = Math.Max(max, ymax);
            max = Math.Max(max, 0 - ymin);

            GridLength = 10;
            while (max / GridLength > 5)
            {
                GridLength *= 2;
                if (max / GridLength > 10) GridLength *= 5;
            }

            LineCount = (int)max / GridLength + 1;
            Xmax = Ymax = GridLength * LineCount;
            Xmin = Ymin = 0 ;
        }

        public void computeDistance()
        {
            this.distance = new double[180, this.StationCount];
            for (int i = 0; i < this.StationCount; i++)
            {
                if (this.StationList[i] != null)
                {
                    distance[0, i] = this.StationList[i].Y;
                    distance[90, i] = 0 - this.StationList[i].X;
                }
            }
            for (int i = 1; i < 180; i++)
            {
                if (i == 90) continue;
                double angle = Math.PI * i / 180;
                double tan = Math.Tan(angle);
                double cos = Math.Cos(angle);
                for (int j = 0; j < this.StationCount; j++)
                {
                    if (this.StationList[j] == null) continue;
                    double ydis = this.StationList[j].Y - this.StationList[j].X * tan;
                    this.distance[i, j] = ydis * cos;
                }
            }
        }
    }
}
