using FObjBase;
using FObjSysTest.Client;
using FObjSysTest.IService;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Client_ConsoleApp
{
    class Program
    {
        static ObjAServiceClient objaClient;
        static void Main(string[] args)
        {
        
            objaClient = new ObjAServiceClient(0x12345600);

            FObjSys.Current.Connect_to_Another_OBJSys(
                new IPEndPoint(IPAddress.Loopback, 12345), objaClient.ID);

            objaClient.PropertyChanged += ObjaClient_PropertyChanged;

            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 10;
            timer.AutoReset = true;
            timer.Elapsed += Timer_Elapsed;
            timer.Start();

            System.Timers.Timer timer2 = new System.Timers.Timer();
            timer2.Interval = 1000;
            timer2.AutoReset = true;
            timer2.Elapsed += Timer2_Elapsed;
            //timer2.Start();

            Console.WriteLine("client --------------------");

            while (true)
            {
                string cmd = Console.ReadLine();
                Console.WriteLine($"cmd = {cmd}");
                if (cmd == "SetNumbers")
                {
                    double[] numbers = new double[20000];
                    for (int i = 0; i < numbers.Count(); i++)
                        numbers[i] = i;
                    objaClient.Numbers = numbers;
                    objaClient.Apply();
                }
                else if (cmd == "GetNumbers")
                {
                    Timer2_Elapsed(null, null);
                }
            }
        }
        static double tick = 0;

        static Stopwatch stopwatch = new Stopwatch();
        private static void Timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (stopwatch.IsRunning)
                return;
            stopwatch.Restart();
            
            objaClient.GetNumbers(tick++, (obj, retdata) =>
            {
                stopwatch.Stop();
                var reponse = retdata as Pack_CallGetNumbersReponse;
                Console.WriteLine($"GetNumbers Reponse reponse.numbers.Count()={reponse.numbers.Count()} stopwatch.ElapsedMilliseconds = {stopwatch.ElapsedMilliseconds}ms");
            }, null);
        }

        static bool mylock;
        private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (mylock != true)//不能让它能重入
            {
                mylock = true;
                FObjBase.PollModule.Current.OnPoll();
                mylock = false;
            }
        }

        private static void ObjaClient_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            
            Console.WriteLine($"e.PropertyName = {e.PropertyName}");
            if (e.PropertyName == "Numbers")
            {
                Console.WriteLine($"objaClient.Numbers.Count() = {objaClient.Numbers.Count()}");
            }
        }
    }
}
