using FObjBase;
using FObjSysTest.IService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FObjSysTest.Server
{
    public class ObjA : IObjAService
    {
        public DateTime Now { get; private set; }

        public double[] Numbers { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        System.Threading.Timer timer;
        public ObjA()
        {
            Random random = new Random();

            double[] numbers = new double[200000];
            for (int i = 0; i < numbers.Count(); i++)
                numbers[i] = random.NextDouble() * 10;
            for (int i = 0; i < 20; i++)
                numbers[i] = double.NaN;
            Numbers = numbers;
            Now = DateTime.Now;

            timer = new System.Threading.Timer(Tick, this, 10000, 10000); 
            
            
        }


        void Tick(object obj)
        {
            Now = DateTime.Now;
        } 
        public void GetNumbers(double offset, AsyncCBHandler asyncDelegate, object asyncState)
        {
            var numbers = (double[])Numbers.Clone();
            if (numbers != null)
            {
                for (int i = 0; i < numbers.Count(); i++)
                {
                    if (!double.IsNaN(numbers[i]) && !double.IsNaN(offset))
                        numbers[i] += offset;
                    else
                        numbers[i] = double.NaN;
                }
            }
            Pack_CallGetNumbersReponse p = new Pack_CallGetNumbersReponse() { numbers = numbers };
            asyncDelegate(asyncState, p);

        }

        public void Apply()
        {

        }
    }
}
