using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FLY.Thick.Base.UI.Converter;

using System.Net;

using FLY.Thick.Base.Common;
using FLY.Thick.Base.Client;
using FLY.Thick.Base.IService;
using Unity;
using System.ComponentModel;
using GalaSoft.MvvmLight.Command;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Page_Initparam.xaml 的交互逻辑
    /// </summary>
    public partial class PgInitparam : Page
    {
        PgInitParamVm viewModel;
        public PgInitparam()
        {
            InitializeComponent();
        }
        [InjectionMethod]
        public void Init(IInitParamService initParamService) 
        {
            viewModel = new PgInitParamVm();
            viewModel.Init(initParamService);
            this.DataContext = viewModel;
        }
    }
    public class PgInitParamVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region 参数
        /// <summary>
        /// 扫描架长
        /// </summary>
        public int PosLength { get; set; }

        public bool IsTimeGridAdvEnabled { get; set; }
        public int ADLag { 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>
        /// 线速度来源
        /// </summary>
        public FilmVSRC FilmVSrc { get; set; }

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

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



        /// <summary>
        /// 数据有效源
        /// </summary>
        public DATAVALIDSRC DataValidSrc { get; set; }



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

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

        /// <summary>
        /// 有按样标定硬件
        /// </summary>
        public bool HasProfileSample { get; set; }

        /// <summary>
        /// 有小托辊
        /// </summary>
        public bool HasHold { get; set; }

        #region 速度

        /// <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; }

        #endregion

        /// <summary>
        /// 数据库保存的月数
        /// </summary>
        public int DBKeepMonth { get; set; }
        /// <summary>
        /// 数据库路径
        /// </summary>
        public string DbPath { get; set; }
        #endregion

        #region Command
        public RelayCommand ApplyCmd { get; }
        public RelayCommand SelectDbPathCmd { get; }
        #endregion


        IInitParamService initParamService;

        public PgInitParamVm()
        {
            ApplyCmd = new RelayCommand(Apply);
            SelectDbPathCmd = new RelayCommand(SelectDbPath);
        }


        public void Init(IInitParamService initParamService)
        {
            this.initParamService = initParamService;

            Misc.BindingOperations.SetBinding(initParamService, "PosLength", this, "PosLength");
            Misc.BindingOperations.SetBinding(initParamService, "IsTimeGridAdvEnabled", this, "IsTimeGridAdvEnabled");
            Misc.BindingOperations.SetBinding(initParamService, "ADLag", this, "DLag");
            Misc.BindingOperations.SetBinding(initParamService, "AutoOrgInterval", this, "AutoOrgInterval");
            Misc.BindingOperations.SetBinding(initParamService, "Encoder1_mmpp", this, "Encoder1_mmpp");
            Misc.BindingOperations.SetBinding(initParamService, "Speed1Scale", this, "Speed1Scale");
            Misc.BindingOperations.SetBinding(initParamService, "FilmVSrc", this, "FilmVSrc");
            Misc.BindingOperations.SetBinding(initParamService, "FilmVThreshold", this, "FilmVThreshold");
            Misc.BindingOperations.SetBinding(initParamService, "Encoder2_mmpp", this, "Encoder2_mmpp");
            Misc.BindingOperations.SetBinding(initParamService, "Encoder2_comp", this, "Encoder2_comp");
            Misc.BindingOperations.SetBinding(initParamService, "MmOfR", this, "MmOfR");
            Misc.BindingOperations.SetBinding(initParamService, "DataValidSrc", this, "DataValidSrc");
            Misc.BindingOperations.SetBinding(initParamService, "AutoF1F3", this, "AutoF1F3");
            Misc.BindingOperations.SetBinding(initParamService, "ReStartDelay", this, "ReStartDelay");
            Misc.BindingOperations.SetBinding(initParamService, "HasProfileSample", this, "HasProfileSample");
            Misc.BindingOperations.SetBinding(initParamService, "HasHold", this, "HasHold");
            Misc.BindingOperations.SetBinding(initParamService, "VScan", this, "VScan");
            Misc.BindingOperations.SetBinding(initParamService, "VJOG", this, "VJOG");
            Misc.BindingOperations.SetBinding(initParamService, "VAccuracy", this, "VAccuracy");
            Misc.BindingOperations.SetBinding(initParamService, "SVelocity", this, "SVelocity");
            Misc.BindingOperations.SetBinding(initParamService, "ATime", this, "ATime");
            Misc.BindingOperations.SetBinding(initParamService, "DTime", this, "DTime");
            Misc.BindingOperations.SetBinding(initParamService, "HVelocity1", this, "HVelocity1");
            Misc.BindingOperations.SetBinding(initParamService, "HVelocity2", this, "HVelocity2");
            Misc.BindingOperations.SetBinding(initParamService, "DBKeepMonth", this, "DBKeepMonth");
            Misc.BindingOperations.SetBinding(initParamService, "DbPath", this, "DbPath");

        }

    private void Apply()
        {
            if (!WdPassword.Authorize("InitParam"))
                return;

            initParamService.PosLength=this.PosLength;
            initParamService.IsTimeGridAdvEnabled=this.IsTimeGridAdvEnabled;
            initParamService.ADLag=this.ADLag;
            initParamService.AutoOrgInterval=this.AutoOrgInterval;
            initParamService.Encoder1_mmpp=this.Encoder1_mmpp;
            initParamService.Speed1Scale=this.Speed1Scale;
            initParamService.FilmVSrc=this.FilmVSrc;
            initParamService.FilmVThreshold=this.FilmVThreshold;
            initParamService.Encoder2_mmpp=this.Encoder2_mmpp;
            initParamService.Encoder2_comp=this.Encoder2_comp;
            initParamService.MmOfR=this.MmOfR;
            initParamService.DataValidSrc=this.DataValidSrc;
            initParamService.AutoF1F3=this.AutoF1F3;
            initParamService.ReStartDelay=this.ReStartDelay;
            initParamService.HasProfileSample=this.HasProfileSample;
            initParamService.HasHold=this.HasHold;
            initParamService.VScan=this.VScan;
            initParamService.VJOG=this.VJOG;
            initParamService.VAccuracy=this.VAccuracy;
            initParamService.SVelocity=this.SVelocity;
            initParamService.ATime=this.ATime;
            initParamService.DTime=this.DTime;
            initParamService.HVelocity1=this.HVelocity1;
            initParamService.HVelocity2=this.HVelocity2;
            initParamService.DBKeepMonth=this.DBKeepMonth;
            initParamService.DbPath = this.DbPath;
            initParamService.Apply();
            FLY.ControlLibrary.Window_Tip.Show("应用成功",
                null,
                TimeSpan.FromSeconds(2));
            
        }

        private void SelectDbPath()
        {
            System.Windows.Forms.FileDialog open = new System.Windows.Forms.OpenFileDialog();
            open.Filter = "sqlite3文件|*.sqlite3|所有文件|*.*";
            open.Title = "打开音乐文件";

            if (System.IO.Path.IsPathRooted(DbPath))
                open.InitialDirectory = System.IO.Path.GetDirectoryName(DbPath);
            else
                open.InitialDirectory = System.Environment.CurrentDirectory;

            if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string currentDirectory = System.Environment.CurrentDirectory.ToLower();
                string filename = open.FileName.ToLower();

                if (filename.StartsWith(currentDirectory))
                {
                    //CurrentDirectory没有最后的\ 需要+1
                    DbPath = open.FileName.Substring(System.Environment.CurrentDirectory.Length + 1);
                }
                else
                {
                    DbPath = open.FileName;
                }
            }
        }
    }
}
