using Misc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FLY.Thick.Base.Server
{
    public class BorderSearchPlastic:BorderSearch
    {
        public BorderSearchPlastic() : base()
        {

        }
        public BorderSearchPlastic(string param_path) : base(param_path)
        {

        }
        /// <summary>
        /// 放入grid数据，分析边界
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="posOfGrid"></param>
        /// <param name="gridBegin"></param>
        /// <param name="dat"></param>
        /// <returns></returns>
        public override bool FindFilm(DIRECTION direction, int posOfGrid, int gridBegin, int[] dat)
        {
            //把数据记录下来
            if ((Datas == null) || (Datas.Length < (dat.Length + gridBegin)))
            {
                int len = dat.Length + gridBegin;
                Datas = new int[len];
            }
            for (int i = 0; i < Datas.Length; i++)
            {
                if (i < gridBegin)//清空前面
                    Datas[i] = Misc.MyBase.NULL_VALUE;
                else if (i < (gridBegin + dat.Length))
                    Datas[i] = dat[i - gridBegin];
                else//清空后面
                    Datas[i] = Misc.MyBase.NULL_VALUE;
            }
            NotifyPropertyChanged("Datas");

            //这些是AD值
            //转为 dat 的序号
            int validbegin_grid = Valid.Begin / posOfGrid - gridBegin;
            int validend_grid = Valid.End / posOfGrid - gridBegin;

            
            int borderbegin_grid;//validbegin_grid;
            int borderend_grid;//validend_grid;

            if (!FindFilm_Threshold(dat, validbegin_grid, validend_grid, TempAD, TempRange, out borderbegin_grid, out borderend_grid))
                return false;

            //N2 只是用来判断一下膜够不够宽而已，没用
            int N2_grid = (N2 * 3 + SensorWidth) / posOfGrid;

            if ((borderend_grid - borderbegin_grid) < N2_grid)//膜太短
                return false;

            Range r1_grid = new Range();
            //算膜中间的平均值
            r1_grid.Begin = borderbegin_grid+(SensorWidth + N2) / posOfGrid;
            r1_grid.End = borderend_grid-(SensorWidth + N2) / posOfGrid;
            int avg = MyMath.Avg(dat, r1_grid.Begin, r1_grid.End);

            //算空气的平均值
            Range r2_grid = new Range();
            r2_grid.Begin = borderbegin_grid - (SensorWidth) / posOfGrid;
            r2_grid.End = borderend_grid + (SensorWidth) / posOfGrid;
            if (r2_grid.Begin < 0)
                r2_grid.Begin = 0;
            if (r2_grid.End > dat.Length - 1)
                r2_grid.End = dat.Length - 1;

            int avg1 = MyMath.Avg(dat, 0,r1_grid.Begin);
            int avg2 = MyMath.Avg(dat, r1_grid.End, dat.Length-1);
            int threshold;
            if ((Misc.MyBase.ISVALIDATA(avg1)) && (Misc.MyBase.ISVALIDATA(avg2)))
            {
                threshold = (avg + (avg1 + avg2) / 2) / 2;
            }
            else if (Misc.MyBase.ISVALIDATA(avg1))
            {
                threshold = (avg + avg1) / 2;
            }
            else if (Misc.MyBase.ISVALIDATA(avg2))
            {
                threshold = (avg + avg2) / 2;
            }
            else
            {
                threshold = avg / 2;
            }

            //左边界
            for (int i = r1_grid.Begin; i >= r2_grid.Begin; i--)
            {
                if (Misc.MyBase.ISVALIDATA(dat[i]))
                {
                    if (dat[i] < threshold)
                    {
                        borderbegin_grid = i;
                    }
                }
            }
            //右边界
            for (int i = r1_grid.End; i < r2_grid.End; i++)
            {
                if (Misc.MyBase.ISVALIDATA(dat[i]))
                {
                    if (dat[i] < threshold)
                    {
                        borderend_grid = i;
                    }
                }
            }

            int idx = 0;
            if (direction == DIRECTION.BACKWARD)
                idx = 1;
            //转为 脉冲
            border[idx].Begin = (borderbegin_grid + gridBegin) * posOfGrid - SensorWidth / 2;
            border[idx].End = (borderend_grid + gridBegin) * posOfGrid + SensorWidth / 2;

            if (Misc.MyBase.ISVALIDATA(border[0].Begin) && Misc.MyBase.ISVALIDATA(border[1].Begin))
            {
                Border.Begin = (border[0].Begin + border[1].Begin) / 2 + SensorWidth / 2;
                Border.End = (border[0].End + border[1].End) / 2 - SensorWidth / 2;

                Width = Border.Width;
                Mid = Border.Mid;
            }
            else if (Misc.MyBase.ISVALIDATA(border[0].Begin))
            {
                Border.Copy(border[0]);

                Width = Border.Width;
                Mid = Border.Mid;
            }
            else if (Misc.MyBase.ISVALIDATA(border[1].Begin))
            {
                Border.Copy(border[1]);

                Width = Border.Width;
                Mid = Border.Mid;
            }
            //膜宽

            return true;
        }
    }
}
