// Copyright © Selmo Technology GmbH. All Rights Reserved

using Novus.Connectivity.Common;
using Novus.Connectivity.Plc;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Shared.TestImpl
{
    public static class TestValuesForDemo
    {
        internal static IConnectionSettings GetSettingsForSimulation()
        {
            return new TestConnectionSettings();
        }

        internal static IEnumerable<IProcessValueInfo> CreateTestValueInfos(int count)
        {
            for (int i = 0; i < count; i++)
            {
                yield return Create($"rTestValue{i}", typeof(float));
            }
        }

        internal static IEnumerable<IProcessValue> CreateTestValues(int count)
        {
            for (int i = 0; i < count; i++)
            {
                yield return new TestProcessValue()
                {
                    ValueInfo = Create($"rTestValue{i}", typeof(float)),
                    Value = 0
                };
            }
        }

        internal static TestProcessValueInfo Create(string name, Type dataType)
        {
            return new()
            {
                Id = new(Guid.NewGuid()),
                Name = name,
                DataType = dataType,
                Display = name,
                Address = new TestValueConnectionAddress(),
            };
        }

        public class TestProcessValue : IProcessValue
        {
            public IProcessValueInfo ValueInfo { get; set; }

            public object Value { get; set; }

            public override bool Equals(object other)
            {
                if (other is TestProcessValue)
                {
                    return Equals((TestProcessValue)other);
                }

                return false;
            }

            public bool Equals(TestProcessValue other)
            {
                if (ValueInfo.Id != other.ValueInfo.Id)
                    return false;

                return (Value.ToString().Equals(other.Value.ToString()));
            }
        }

        public class TestProcessValueInfo : IProcessValueInfo
        {
            public ProcessValueId Id { get; set; }

            public string Name { get; set; }

            public string Display { get; set; }

            public Type DataType { get; set; }

            public TestValueConnectionAddress Address { get; set; }

            public object InitValue { get; set; }

            IValueConnectionAddress IProcessValueInfo.Address => Address;
        }

        public class TestValueConnectionAddress : IValueConnectionAddress
        {
            public string RelativePath { get; set; } = "Not needed for simulation";

            public string FullPath { get; set; } = "Not needed for simulation";

            public ConnectionType ConnectionType { get; set; } = ConnectionType.SimulatedFromFile;

            public DeviceId DeviceId => new(Guid.NewGuid());

            public string BasePath => "";
        }

        internal class TestConnectionSettings : IConnectionSettings
        {
            public int UpdateRate { get; set; } = 1000;

            public DeviceId DeviceId { get; set; } = new(Guid.NewGuid());

            public ConnectionType ConnectionType { get; set; } = ConnectionType.SimulatedFromFile;
        }
    }
}