using iBemsDataService.Controllers.Bems.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace iBemsDataService.Controllers.Bems.Formula
{
    public class ParameterBox
    {
        public DateTime DateTime { get ; set; }

        Box[] boxes;
        public ParameterBox( CalculationParameter[] parameters )
        {
            this.boxes = new Box[parameters.Length];
            for( int i = 0 ; i < parameters.Length ; i++ )
            {
                var box = this.boxes[i] = new Box();
                box.parameter = parameters[i];
            }
        }

        public bool NextStage()
        {
            for( var i = 0 ; i < this.boxes.Length ; i++ )
            {
                var box = this.boxes[i];
                box.Set = false;
                if( box.Next() == false )
                {
                    return false;
                }
            }
            return true;
        }

        public bool Sync()
        {
            if (this.boxes == null || this.boxes.Length == 0) return false;


            var value = this.boxes[0].GetValue();
            if( value == null )
                return false;

            var dateTime = value.DateTime;

        Loop:
            for( var i = 0 ; i < this.boxes.Length ; i++ )
            {
                var box = this.boxes[i];
                if( box.Set )
                {
                    continue;
                }

                switch( box.Sync( dateTime ) )
                {
                    case -1: // 없다. 더이상 계산이 필요 없다.
                        return false;
                    case 0:  // 동일한 것이 있다. 
                        box.Set = true;
                        break;
                    case 1:  // 초과한 시간이 있다. 
                        dateTime = box.GetValue().DateTime;
                        goto Loop;
                }
            }

            this.DateTime = dateTime;
            return true;
        }

        class Box
        {
            public enum IndexAtDateTimeResult
            {
                
            };
            public bool Set { get; set; }
            public int currentIndex = 0;
            public CalculationParameter parameter;
            public IndexAtDateTimeResult Result { get; set; }

            public Box()
            {
                Set = false;
            }

            public bool Next()
            {
                this.currentIndex++;
                return (this.currentIndex < parameter.Values.Length);
            }

            public int Sync( DateTime dateTime )
            {
                var values = parameter.Values;
                if( values != null )
                {
                    for( var i = currentIndex ; i < values.Length ; i++ )
                    {
                        if( values[i].DateTime == dateTime )
                        {
                            this.currentIndex = i;
                            this.Setup();
                            return 0;
                        }

                        if( dateTime < values[i].DateTime )
                        {
                            this.currentIndex = i;
                            return 1;
                        }
                    }
                }
                return -1;
            }

            public CalculationValue GetValue()
            {
                if( parameter.Values == null )
                    return null;
                if( parameter.Values.Length <= currentIndex )
                    return null;

                return parameter.Values[currentIndex];
            }
            public void Setup()
            {
                this.Set = true;
                this.parameter.SetValueFromIndex( currentIndex );
            }
        }
    }
}