using AsbCloudDb.Model;
using System.Linq;
using System;

namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
{


    /// <summary>
    /// Статический замер телесистемы
    /// </summary>
    internal class DetectorStaticSurveying: DetectorAbstract
    {
        public DetectorStaticSurveying()
        : base(WellOperationCategory.IdStaticSurveying) { }

        protected override bool DetectBegin(DetectableTelemetry[] telemetry, int position, DetectedOperation? previousOperation)
        {
            var point0 = telemetry[position];

            if (point0.Pressure < 15)
                return false;

            var delta = point0.WellDepth - point0.BitDepth;
            if (delta > 2.5d || delta < 0.15d)
                return false;

            if (point0.RotorSpeed > 30)
                return false;

            if (ContainsDeviation(telemetry, t => t.BlockPosition, position, 120, 0.03))
                return false;

            if (ContainsDeviation(telemetry, t => t.Pressure, position, 60, 10))
                return false;

            return true;
        }

        protected override int DetectEnd(DetectableTelemetry[] telemetry, int position, DetectedOperation? previousOperation)
        {
            var point0 = telemetry[position];

            var delta = point0.WellDepth - point0.BitDepth;
            if (delta > 2.5d )
                return IdReasonOfEnd_DeltaDepthIsHi;

            if (point0.RotorSpeed > 30)
                return IdReasonOfEnd_RotorSpeedIsHi;

            if (RisesFromBegin(telemetry, t => t.Pressure, position, 10, 15))
                return IdReasonOfEnd_PressureIsRising;

            if (ContainsDeviation(telemetry, t => t.BlockPosition, position, 10, 0.05))
                return IdReasonOfEnd_BlockPositionDeviates;

            return IdReasonOfEnd_NotDetected;
        }

        protected override bool IsValid(DetectableTelemetry[] telemetry, int begin, int end)
            => IsValidByWellDepthDoesNotChange(telemetry, begin, end);

        protected override double CalcValue(DetectableTelemetry[] telemetry, int begin, int end)
            => CalcDeltaMinutes(telemetry, begin, end);
    }


}