using Panuon.WPF.UI;
using SWRIS.Core;
using SWRIS.Enums;
using SWRIS.Models;
using SWRIS.Models.ViewModel;
using System;
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Linq;
using System.Windows.Markup;

namespace SWRIS.Pages
{
    /// <summary>
    /// SettingDialog.xaml 的交互逻辑
    /// </summary>
    public partial class SettingDialog : WindowX, IComponentConnector
    {
        public SettingViewModel SettingView { get; set; }
        public bool IsAutoStart { get; set; }
        public SettingDialog()
        {
            InitializeComponent();
            SettingView = new SettingViewModel()
            {
                AppName = App.Config.AppName,
                Version = App.Config.Version,
                Copyright = App.Config.Copyright,
                Crane = App.Config.Crane,
                IpAddress = App.Config.IpAddress,
                Port = App.Config.Port,
                AlarmValidCount = App.Config.AlarmValidCount,
                DamageExtent = App.Config.DamageExtent,
                DataSaveDays = App.Config.DataSaveDays,
                ShowDebugMessage = App.Config.ShowDebugMessage,
                SpeedVariationThreshold = App.Config.SpeedVariationThreshold,
                SoundRiskLevel = (int)App.Config.SoundRiskLevel,
                CleanScheduledTime = App.Config.CleanScheduledTime,
                DamageNearPointCount = App.Config.DamageNearPointCount,
            };
            SettingView.Calibration = new CalibrationDataModel
            {
                ModuleType = (int)App.Config.Calibration.ModuleType,
                Limits = new ObservableCollection<LimitDataModel>()
            };

            SettingView.Calibration.Tcp = new TcpDataModel
            {
                IpAddress = App.Config.Calibration.Tcp.IpAddress,
                Port = App.Config.Calibration.Tcp.Port,
                Station = App.Config.Calibration.Tcp.Station
            };

            SettingView.Calibration.Serial = new SerialDataModel
            {
                PortName = App.Config.Calibration.Serial.PortName,
                BaudRate = App.Config.Calibration.Serial.BaudRate,
                DataBits = App.Config.Calibration.Serial.DataBits,
                Parity = App.Config.Calibration.Serial.Parity.ToString(),
                StopBits = (int)App.Config.Calibration.Serial.StopBits,
                Station = App.Config.Calibration.Serial.Station
            };

            foreach (var limit in App.Config.Calibration.Limits)
            {
                var limitModel = new LimitDataModel
                {
                    Address = limit.Address,
                    IsReverse = limit.IsReverse,
                    Name = limit.Name,
                    Position = limit.Position,
                    IsEnable = limit.IsEnable,
                    RelatedRopes = App.Config.Equipments.Select(c => new RopeDataModel
                    {
                        RopeName = c.RopeName,
                        RopeNumber = c.RopeNumber,
                        IsSelected = limit.RelatedRopes.Contains(c.RopeNumber)
                    }).ToList()
                };
                SettingView.Calibration.Limits.Add(limitModel);
            }
            DataContext = SettingView;
        }

        private void Save_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            App.Config.Crane = SettingView.Crane;

            App.Config.IpAddress = SettingView.IpAddress;
            App.Config.Port = SettingView.Port;

            App.Config.CleanScheduledTime = SettingView.CleanScheduledTime;
            App.Config.DataSaveDays = SettingView.DataSaveDays;

            App.Config.SoundRiskLevel = (RiskLevel)SettingView.SoundRiskLevel;
            App.Config.SpeedVariationThreshold = SettingView.SpeedVariationThreshold;

            App.Config.DamageExtent = SettingView.DamageExtent;
            App.Config.AlarmValidCount = SettingView.AlarmValidCount;

            App.Config.Calibration.ModuleType = (ModuleType)SettingView.Calibration.ModuleType;
            if (SettingView.Calibration.ModuleType == (int)ModuleType.ModbusRtu)
            {
                App.Config.Calibration.Serial.PortName = SettingView.Calibration.Serial.PortName;
                App.Config.Calibration.Serial.BaudRate = SettingView.Calibration.Serial.BaudRate;
                App.Config.Calibration.Serial.DataBits = SettingView.Calibration.Serial.DataBits;
                App.Config.Calibration.Serial.Parity = (Parity)Enum.Parse(typeof(Parity), SettingView.Calibration.Serial.Parity);
                App.Config.Calibration.Serial.StopBits = (StopBits)SettingView.Calibration.Serial.StopBits;
                App.Config.Calibration.Serial.Station = SettingView.Calibration.Serial.Station;
            }
            else
            {
                App.Config.Calibration.Tcp.IpAddress = SettingView.Calibration.Tcp.IpAddress;
                App.Config.Calibration.Tcp.Port = SettingView.Calibration.Tcp.Port;
                App.Config.Calibration.Tcp.Station = SettingView.Calibration.Tcp.Station;
            }
            for (int i = 0; i < 2; i++)
            {
                var sourceLimit = SettingView.Calibration.Limits[i];
                var targetLimit = App.Config.Calibration.Limits[i];

                targetLimit.Name = sourceLimit.Name;
                targetLimit.Address = sourceLimit.Address;
                targetLimit.Position = sourceLimit.Position;
                targetLimit.IsEnable = sourceLimit.IsEnable;
                targetLimit.IsReverse = sourceLimit.IsReverse;
                targetLimit.RelatedRopes = sourceLimit.RelatedRopes
                    .Where(c => c.IsSelected)
                    .Select(c => c.RopeNumber)
                    .ToList();
            }

            if (ConfigHelper.Set(App.Config))
            {
                IsAutoStart = SettingView.IsAutoStart;
                DialogResult = true;
            }
            else
            {
                DialogResult = false;
            }

        }
    }
}
