using FLY.Thick.Base.IService;
using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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>
        /// 找空气与膜均值的 中间位置
        /// </summary>
        /// <returns></returns>
        void FindFilm_Mean(int[] dat, int sensor_grid, int n2_grid, ref int borderbegin_grid, ref int borderend_grid) 
        {
            Range r1_grid = new Range();
            //算膜中间的平均值
            r1_grid.Begin = borderbegin_grid + sensor_grid + n2_grid;
            r1_grid.End = borderend_grid - (sensor_grid + n2_grid);
            int avg = MyMath.Avg(dat, r1_grid.Begin, r1_grid.End);

            //算空气的平均值
            Range r2_grid = new Range();
            r2_grid.Begin = borderbegin_grid - sensor_grid/3;
            r2_grid.End = borderend_grid + sensor_grid/3;
            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, r2_grid.Begin);
            int avg2 = MyMath.Avg(dat, r2_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 = (TempAD+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;
                    }
                }
            }
        }
        /// <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)
        {
            //把数据记录下来
            getViewReponse = new BorderSearchGetViewReponse()
            {
                direction = direction,
                posOfGrid = posOfGrid,
                gridBegin = gridBegin,
                dat = dat
            };
            UpdateTime = DateTime.Now;

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


            if (!FindFilm_Threshold(dat,
                validbegin_grid, validend_grid,
                out int borderbegin_grid, out int borderend_grid,
                out bool isBorderBeginOk, out bool isBorderEndOk))
            {
                return false;
            }

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

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

            FindFilm_Mean(dat, SensorWidth / posOfGrid, N2 / posOfGrid, ref borderbegin_grid, ref borderend_grid);

            int idx = (direction == DIRECTION.FORWARD) ? 0 : 1;
            var border = borders[idx];

            //borderbegin_grid, borderend_grid 是实际边界位置， 
            //border.value 是空气与膜的拐点
            //必须外扩

            //转为 脉冲
            border.value.Begin = (borderbegin_grid + gridBegin) * posOfGrid - SensorWidth / 2;
            border.value.End = (borderend_grid + gridBegin) * posOfGrid + SensorWidth / 2;
            border.isBeginOk = true;
            border.isEndOk = true;
            border.UpdateReal(SensorWidth);

            if (borders.All(b => b.value.IsValid))
            {
                Border.Begin = (int)borders.Average(b => b.real.Begin);
                Border.End = (int)borders.Average(b => b.real.End);
            }
            else if (border.value.IsValid)
            {
                Border.Copy(border.real);
            }

            getViewReponse.border = border.real;

            Width = Border.Width;
            Mid = Border.Mid;
            return true;
        }
    }
}
