using Misc;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;


namespace FLY.Thick.FilmCasting.WebAPI.Controllers
{
    /// <summary>
    /// 系统参数
    /// </summary>
    public class InitParamController : ApiController
    {
        FLY.Thick.Base.IService.IInitParamService initParamService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="initParamService"></param>
        public InitParamController(
            FLY.Thick.Base.IService.IInitParamService initParamService
            )
        {
            this.initParamService = initParamService;
        }


        /// <summary>
        /// 获取服务的数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Data GetValue()
        {

            return new Data()
            {
                PosLength = initParamService.PosLength,
                PosOfGrid = initParamService.PosOfGrid,
                AutoOrgInterval = initParamService.AutoOrgInterval,
                Encoder1_mmpp = initParamService.Encoder1_mmpp,
                Speed1Scale = initParamService.Speed1Scale,
                FilmVSrc = initParamService.FilmVSrc.ToString(),
                FilmVThreshold = initParamService.FilmVThreshold,
                Encoder2_mmpp = initParamService.Encoder2_mmpp,
                MmOfR = initParamService.MmOfR,
                DataValidSrc = initParamService.DataValidSrc.ToString(),
                AutoF1F3 =initParamService.AutoF1F3,
                ReStartDelay = initParamService.ReStartDelay,
                
                VScan = initParamService.VScan,
                VJOG = initParamService.VJOG,
                VAccuracy = initParamService.VAccuracy,
                SVelocity = initParamService.SVelocity,
                ATime = initParamService.ATime,
                DTime = initParamService.DTime,
                HVelocity1 = initParamService.HVelocity1,
                HVelocity2 = initParamService.HVelocity2,
            };
        }

        /// <summary>
        /// 获取服务的数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public void SetValue([FromBody]Data data) 
        {
            if (data.PosLength != null)
                initParamService.PosLength = (int)data.PosLength;
                if (data.AutoOrgInterval != null)
                initParamService.AutoOrgInterval = (int)data.AutoOrgInterval;
            if (data.Encoder1_mmpp != null)
                initParamService.Encoder1_mmpp = (double)data.Encoder1_mmpp;
            if (data.FilmVSrc != null)
            {
                try
                {
                    initParamService.FilmVSrc = (FLY.Thick.Base.Common.FilmVSRC)Enum.Parse(typeof(FLY.Thick.Base.Common.FilmVSRC), data.FilmVSrc);
                }
                catch { 
                
                }
            }
            if (data.FilmVThreshold != null)
                initParamService.FilmVThreshold = (int)data.FilmVThreshold;
            if (data.Encoder2_mmpp != null)
                initParamService.Encoder2_mmpp = (int)data.Encoder2_mmpp;
            if (data.MmOfR != null)
                initParamService.MmOfR = (double)data.MmOfR;
            if (data.DataValidSrc != null)
            {
                try
                {
                    initParamService.DataValidSrc = (FLY.Thick.Base.Common.DATAVALIDSRC)Enum.Parse(typeof(FLY.Thick.Base.Common.DATAVALIDSRC), data.DataValidSrc);
                }
                catch
                {

                }
            }
            if (data.AutoF1F3 != null)
                initParamService.AutoF1F3 = (bool)data.AutoF1F3;
            if (data.ReStartDelay != null)
                initParamService.ReStartDelay = (int)data.ReStartDelay;

            if (data.VScan != null)
                initParamService.VScan = (UInt32)data.VScan;
            if (data.VJOG != null)
                initParamService.VJOG = (UInt32)data.VJOG;
            if (data.VAccuracy != null)
                initParamService.VAccuracy = (UInt32)data.VAccuracy;
            if (data.SVelocity != null)
                initParamService.SVelocity = (UInt32)data.SVelocity;
            if (data.ATime != null)
                initParamService.ATime = (UInt32)data.ATime;
            if (data.DTime != null)
                initParamService.DTime = (UInt32)data.DTime;
            if (data.HVelocity1 != null)
                initParamService.HVelocity1 = (UInt32)data.HVelocity1;
            if (data.HVelocity2 != null)
                initParamService.HVelocity2 = (UInt32)data.HVelocity2;
        }


        /// <summary>
        /// 应该 及 保存
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public void Apply() 
        {
            initParamService.Apply();
        }
        /// <summary>
        /// 数据
        /// </summary>
        public class Data 
        {
            /// <summary>
            /// 扫描架长
            /// </summary>
            public int? PosLength { get; set; }

            /// <summary>
            /// 从 flyad7 获取的
            /// </summary>
            public int? PosOfGrid { get; set; }

            /// <summary>
            /// 自动归原点间距,次数
            /// </summary>
            public int? AutoOrgInterval { get; set; }


            /// <summary>
            /// 编码器1 mm/pulse
            /// </summary>
            public double? Encoder1_mmpp { get; set; }

            /// <summary>
            /// 设置的速度，与 实际速度比例 Speed1 = VScan*Speed1Scale
            /// </summary>
            public double? Speed1Scale { get; set; }

            /// <summary>
            /// 线速度来源 EN2, ROUND
            /// </summary>
            public string FilmVSrc { get; set; }

            /// <summary>
            /// 最小线速度 m/min
            /// </summary>
            public int? FilmVThreshold { get; set; }
            /// <summary>
            /// 线速度:编码器2 mm/pulse
            /// </summary>
            public double? Encoder2_mmpp { get; set; }

            /// <summary>
            /// 线速度:1圈多少mm
            /// </summary>
            public double? MmOfR { get; set; }



            /// <summary>
            /// 数据有效源 BYEN2,BYINPUT,VALID,INVALID
            /// </summary>
            public string DataValidSrc { get; set; }



            /// <summary>
            /// 当数据有效状态改变，自动按F1，F3
            /// </summary>
            public bool? AutoF1F3 { get; set; }

            /// <summary>
            /// 当数据有效状态  无效->有效 ，等待多久重新扫描 ，单位s
            /// </summary>
            public int? ReStartDelay { get; set; }



            /// <summary>
            /// 扫描时速度 Velocity Of Scan 
            /// </summary>
            public UInt32? VScan { get; set; }
            /// <summary>
            /// 调试时速度，向前走，向后走 Velocity Of JOG
            /// </summary>
            public UInt32? VJOG { get; set; }
            /// <summary>
            /// 精确速度 Velocity Of Accuracy  如: 机架修正, 样品取样, 机架信息获取
            /// </summary>
            public UInt32? VAccuracy { set; get; }
            /// <summary>
            /// 开始速度 Start Velocity
            /// </summary>
            public UInt32? SVelocity { set; get; }
            /// <summary>
            /// 加速时间
            /// </summary>
            public UInt32? ATime { set; get; }
            /// <summary>
            /// 减速时间
            /// </summary>
            public UInt32? DTime { set; get; }
            /// <summary>
            /// 归0速度1
            /// </summary>
            public UInt32? HVelocity1 { set; get; }
            /// <summary>
            /// 归0速度2
            /// </summary>
            public UInt32? HVelocity2 { set; get; }


        }
    }
}
