using SWRIS.Dtos;
using SWRIS.Enums;
using SWRIS.Models;
using SWRIS.Models.Data;
using SWRIS.Models.ViewModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace SWRIS
{
    public static class Mapping
    {
        public static void ToEquipmentModel(this EquipmentSettingViewModel equipmentSettingViewModel, EquipmentModel equipmentModel)
        {
            if (equipmentSettingViewModel == null || equipmentModel == null)
                return;

            // 赋值基本属性
            equipmentModel.RopeNumber = equipmentSettingViewModel.RopeNumber;
            equipmentModel.RopeName = equipmentSettingViewModel.RopeName;
            equipmentModel.IpAddress = equipmentSettingViewModel.IpAddress;
            equipmentModel.SerialNo = equipmentSettingViewModel.SerialNo;
            equipmentModel.Supplier = equipmentSettingViewModel.Supplier;
            equipmentModel.LayType = (LayType)equipmentSettingViewModel.LayType;
            equipmentModel.LiftHightRatio = equipmentSettingViewModel.LiftHightRatio;
            equipmentModel.SoundRiskLevel = equipmentSettingViewModel.SoundRiskLevel;
            equipmentModel.WireMaterialType = (WireMaterialType)equipmentSettingViewModel.WireMaterialType;
            equipmentModel.WireSurfaceType = (WireSurfaceType)equipmentSettingViewModel.WireSurfaceType;
            equipmentModel.RopeCoreType = (RopeCoreType)equipmentSettingViewModel.RopeCoreType;
            equipmentModel.DisableAlarm = equipmentSettingViewModel.DisableAlarm;
            // 如果源Parameter为null，确保目标Parameter不为null
            if (equipmentSettingViewModel.Parameter == null)
            {
                equipmentModel.Parameter = new ParameterModel();
                return;
            }

            // 如果目标Parameter为Null，创建新实例
            if (equipmentModel.Parameter == null)
            {
                equipmentModel.Parameter = new ParameterModel();
            }

            // 赋值Parameter属性
            var sourceParam = equipmentSettingViewModel.Parameter;
            var targetParam = equipmentModel.Parameter;

            targetParam.SensorCount = sourceParam.SensorCount;
            targetParam.SamplingStep = sourceParam.SamplingStep;
            targetParam.MainBoardSoftwareVersion = sourceParam.MainBoardSoftwareVersion;
            targetParam.FrequencyDivisionFactor = sourceParam.FrequencyDivisionFactor;
            targetParam.TimeDomainFrequency = sourceParam.TimeDomainFrequency;
            targetParam.DamageThreshold = sourceParam.DamageThreshold;
            targetParam.ScrapUpperLimit = sourceParam.ScrapUpperLimit;
            targetParam.BackMagnetLength = sourceParam.BackMagnetLength;
            targetParam.ValueCoefficient = sourceParam.ValueCoefficient;
            targetParam.FrontMagnetLength = sourceParam.FrontMagnetLength;
            targetParam.EffectiveStrokeLength = sourceParam.EffectiveStrokeLength;
            targetParam.ZeroPositionCorrectionOffset = sourceParam.ZeroPositionCorrectionOffset;
            targetParam.SoundLightAlarmAutoResetMode = sourceParam.SoundLightAlarmAutoResetMode;
            targetParam.ZeroPositionCorrectionDuration = sourceParam.ZeroPositionCorrectionDuration;
            targetParam.SystemTime = sourceParam.SystemTime;
            targetParam.AlarmValue = sourceParam.AlarmValue;
            targetParam.TwistFactor = sourceParam.TwistFactor;
            targetParam.WarningValue = sourceParam.WarningValue;
            targetParam.WireRopeType = sourceParam.WireRopeType;
            targetParam.WireRopeCount = sourceParam.WireRopeCount;
            targetParam.WireRopeLength = sourceParam.WireRopeLength;
            targetParam.WireRopeDiameter = sourceParam.WireRopeDiameter;
            targetParam.EncoderDirection = sourceParam.EncoderDirection;
            targetParam.WireRopeStrandCount = sourceParam.WireRopeStrandCount;
            targetParam.WireRopeStrandWireCount = sourceParam.WireRopeStrandWireCount;
            targetParam.FirstCalibrationPositiont = sourceParam.FirstCalibrationPositiont;
            targetParam.SecondCalibrationPositiont = sourceParam.SecondCalibrationPositiont;
        }

        // 可选：创建新实例的扩展方法
        public static EquipmentModel ToEquipmentModel(this EquipmentSettingViewModel equipmentSettingViewModel)
        {
            var equipmentModel = new EquipmentModel();
            equipmentSettingViewModel.ToEquipmentModel(equipmentModel);
            return equipmentModel;
        }
        public static EquipmentSettingViewModel ToEquipmentSettingViewModel(this EquipmentModel equipmentModel, RunningStatus runningStatus)
        {
            var equipmentSettingViewModel = new EquipmentSettingViewModel();
            equipmentModel.ToEquipmentSettingViewModel(equipmentSettingViewModel, runningStatus);
            return equipmentSettingViewModel;
        }
        public static void ToEquipmentSettingViewModel(this EquipmentModel equipmentModel, EquipmentSettingViewModel equipmentSettingViewModel, RunningStatus runningStatus)
        {
            if (equipmentModel == null || equipmentSettingViewModel == null)
                return;

            // 赋值基本属性
            equipmentSettingViewModel.RopeNumber = equipmentModel.RopeNumber;
            equipmentSettingViewModel.RopeName = equipmentModel.RopeName;
            equipmentSettingViewModel.IpAddress = equipmentModel.IpAddress;
            equipmentSettingViewModel.SerialNo = equipmentModel.SerialNo;
            equipmentSettingViewModel.Supplier = equipmentModel.Supplier;
            equipmentSettingViewModel.RunningStatus = runningStatus;
            equipmentSettingViewModel.LayType = (int)equipmentModel.LayType;
            equipmentSettingViewModel.LiftHightRatio = equipmentModel.LiftHightRatio;
            equipmentSettingViewModel.SoundRiskLevel = equipmentModel.SoundRiskLevel;
            equipmentSettingViewModel.WireMaterialType = (int)equipmentModel.WireMaterialType;
            equipmentSettingViewModel.WireSurfaceType = (int)equipmentModel.WireSurfaceType;
            equipmentSettingViewModel.RopeCoreType = (int)equipmentModel.RopeCoreType;
            equipmentSettingViewModel.DisableAlarm = equipmentModel.DisableAlarm;
            // 如果源Parameter为null，确保目标Parameter不为null
            if (equipmentModel.Parameter == null)
            {
                equipmentSettingViewModel.Parameter = new ParameterData();
                return;
            }

            // 如果目标Parameter为null，创建新实例
            if (equipmentSettingViewModel.Parameter == null)
            {
                equipmentSettingViewModel.Parameter = new ParameterData();
            }

            // 赋值Parameter属性
            var sourceParam = equipmentModel.Parameter;
            var targetParam = equipmentSettingViewModel.Parameter;

            targetParam.MainBoardSoftwareVersion = sourceParam.MainBoardSoftwareVersion;
            targetParam.SensorCount = sourceParam.SensorCount;
            targetParam.SamplingStep = sourceParam.SamplingStep;
            targetParam.FrequencyDivisionFactor = sourceParam.FrequencyDivisionFactor;
            targetParam.TimeDomainFrequency = sourceParam.TimeDomainFrequency;
            targetParam.DamageThreshold = sourceParam.DamageThreshold;
            targetParam.ScrapUpperLimit = sourceParam.ScrapUpperLimit;
            targetParam.FrontMagnetLength = sourceParam.FrontMagnetLength;
            targetParam.BackMagnetLength = sourceParam.BackMagnetLength;
            targetParam.ValueCoefficient = sourceParam.ValueCoefficient;
            targetParam.EffectiveStrokeLength = sourceParam.EffectiveStrokeLength;
            targetParam.ZeroPositionCorrectionDuration = sourceParam.ZeroPositionCorrectionDuration;
            targetParam.ZeroPositionCorrectionOffset = sourceParam.ZeroPositionCorrectionOffset;
            targetParam.WarningValue = sourceParam.WarningValue;
            targetParam.AlarmValue = sourceParam.AlarmValue;
            targetParam.SoundLightAlarmAutoResetMode = sourceParam.SoundLightAlarmAutoResetMode;
            targetParam.WireRopeType = sourceParam.WireRopeType;
            targetParam.WireRopeCount = sourceParam.WireRopeCount;
            targetParam.WireRopeLength = sourceParam.WireRopeLength;
            targetParam.WireRopeDiameter = sourceParam.WireRopeDiameter;
            targetParam.WireRopeStrandCount = sourceParam.WireRopeStrandCount;
            targetParam.WireRopeStrandWireCount = sourceParam.WireRopeStrandWireCount;
            targetParam.EncoderDirection = sourceParam.EncoderDirection;
            targetParam.SystemTime = sourceParam.SystemTime;
            targetParam.TwistFactor = sourceParam.TwistFactor;
            targetParam.FirstCalibrationPositiont = sourceParam.FirstCalibrationPositiont;
            targetParam.SecondCalibrationPositiont = sourceParam.SecondCalibrationPositiont;
        }
        public static void ToEquipmentDataModel(this EquipmentModel equipmentModel, EquipmentDataModel equipmentDataModel)
        {
            if (equipmentModel == null || equipmentDataModel == null)
                return;

            // 赋值基本属性
            equipmentDataModel.RopeNumber = equipmentModel.RopeNumber;
            equipmentDataModel.RopeName = equipmentModel.RopeName;
            equipmentDataModel.IpAddress = equipmentModel.IpAddress;
            equipmentDataModel.SerialNo = equipmentModel.SerialNo;
            equipmentDataModel.LiftHightRatio = equipmentModel.LiftHightRatio;
            equipmentDataModel.DisableAlarm = equipmentModel.DisableAlarm;
            // 如果有Parameter数据，设置相关属性
            if (equipmentModel.Parameter != null)
            {
                equipmentDataModel.SensorCount = equipmentModel.Parameter.SensorCount;
                equipmentDataModel.RopeLength = equipmentModel.Parameter.WireRopeLength;
                equipmentDataModel.SamplingStep = equipmentModel.Parameter.SamplingStep;
            }
        }
        public static EquipmentDataModel ToEquipmentDataModel(this EquipmentModel equipmentModel)
        {
            var equipmentDataModel = new EquipmentDataModel();
            equipmentModel.ToEquipmentDataModel(equipmentDataModel);
            return equipmentDataModel;
        }

        // 可选：批量转换方法
        public static ObservableCollection<EquipmentDataModel> ToEquipmentDataModels(this ObservableCollection<EquipmentModel> equipmentModels)
        {
            var result = new ObservableCollection<EquipmentDataModel>();
            if (equipmentModels == null) return result;

            foreach (var equipmentModel in equipmentModels)
            {
                result.Add(equipmentModel.ToEquipmentDataModel());
            }

            return result;
        }
        public static SimpleRecordDto ToSimpleRecordDto(this RecordDto record)
        {
            if (record == null)
                return null;
            var simpleRecord = new SimpleRecordDto
            {
                Id = record.Id ?? 0,
                RopeName = record.RopeName,
                Time = record.StartTime,
                RiskLevel = record.RiskLevel,
                Description = $"检测出{record.DamageCount}处损伤",
                DetectionLength = record.DetectionLength
            };
            return simpleRecord;
        }
        public static ObservableCollection<SimpleRecordDto> ToSimpleRecordDtos(this List<RecordDto> records)
        {
            var result = new ObservableCollection<SimpleRecordDto>();
            if (records == null) return result;

            foreach (var record in records)
            {
                result.Add(record.ToSimpleRecordDto());
            }

            return result;
        }
        public static TTarget AssignFrom<TSource, TTarget>(this TTarget target, TSource source)
           where TTarget : class
           where TSource : class
        {
            if (source == null || target == null)
            {
                return target;
            }
            var sourceProperties = typeof(TSource).GetProperties();
            var targetProperties = typeof(TTarget).GetProperties();
            foreach (var sourceProp in sourceProperties)
            {
                var targetProp = targetProperties.FirstOrDefault(p => p.Name == sourceProp.Name && p.PropertyType == sourceProp.PropertyType);
                if (targetProp != null && targetProp.CanWrite)
                {
                    var value = sourceProp.GetValue(source);
                    targetProp.SetValue(target, value);
                }
            }
            return target;
        }
        public static TTarget CloneTo<TSource, TTarget>(this TSource source)
            where TTarget : class, new()
            where TSource : class
        {
            if (source == null)
            {
                return null;
            }
            var target = new TTarget();
            return target.AssignFrom(source);
        }

    }
}
