using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Misc;
using System.IO;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace Flyad7_WPF
{
    public class DebugAppParam : INotifyPropertyChanged
    {
        

        public string EPStr { get; set; } = "192.168.251.10:20006";

        public bool HasTimeGrid { get; set; }


        public bool HasGrid { get; set; }


        public bool HasGridAdv { get; set; }

        public bool HasCRC { get; set; }


        public int FB_Pos1 { get; set; } = 1000;

        public int FB_Pos2 { get; set; } = 3000;


        public UInt32 Velocity { get; set; } = 3000;
        public UInt32 SVelocity { get; set; } = 100;
        public UInt32 ATime { get; set; } = 300;
        public UInt32 DTime { get; set; } = 300;
        public UInt32 HVelocity1 { get; set; } = 1000;
        public UInt32 HVelocity2 { get; set; } = 300;

        public int PosLen { get; set; } = 8900;
        public int ADLag { get; set; } = 0;
       

        public event PropertyChangedEventHandler PropertyChanged;

        public DebugAppParam() 
        {
            Load();
            this.PropertyChanged += AutoSave;
        }

        private bool isIgnoreSave;
        private async void AutoSave(object sender, PropertyChangedEventArgs e)
        {
            if (isIgnoreSave)
                return;

            //if (this.GetType().GetProperty(e.PropertyName).GetCustomAttributes(typeof(JsonPropertyAttribute), false).Count() > 0)
            {
                isIgnoreSave = true;
                await Task.Delay(5000);//Task.Yield();
                Save();
                isIgnoreSave = false;
            }
        }
        public void Save()
        {
            try
            {
                string json = JsonConvert.SerializeObject(this, Formatting.Indented);
                File.WriteAllText("param.json", json);
            }
            catch {
            
            }
        }

        public void Load()
        {
            try
            {
                string json = File.ReadAllText("param.json");
                isIgnoreSave = true;
                JsonConvert.PopulateObject(json, this);
                isIgnoreSave = false;
            }
            catch { 
            
            }
        }


    }


}
