using EDAViewer.Singleton.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EDAViewer.Singleton.Helper
{

    [Serializable]
    public class Common
    {

        public ModuleInstanceTypeName? ObjectType { get; set; }
        public string ConfigurationState { get; set; }
        public string ConfigurationProductiveState { get; set; }
        public string UnitName { get; set; }
        public string ContainerName { get; set; }
        //
        public string StoragePath { get; set; }
        public string StartTimeFormat { get; set; }
        public string Filename { get; set; }
        //
        public string Source { get; set; }
        public string LogisticsEquipmentAlias { get; set; }
        public List<string[]> LogisticsAttributes { get; set; }
        public List<string[]> LogisticsColumns { get; set; }
        public List<string[]> Parameters { get; set; }
        public string ParametersAsCsv { get; set; }
        public List<string[]> GeneralTriggers { get; set; }
        public List<string[]> StartTriggersDCP { get; set; }
        public List<string[]> LogisticsTriggers { get; set; }
        public Dictionary<int, string[]> LogisticsTriggersKeysKeyMapping { get; set; }
        public Dictionary<int, List<string[]>> LogisticsTriggersCallDefinitionAttributes { get; set; }
        public List<string[]> StopTriggersDCP { get; set; }

        [Obsolete("Only for DeserializeObject")]
        public Common()
        {
            ObjectType = null;
            ConfigurationState = string.Empty;
            ConfigurationProductiveState = string.Empty;
            UnitName = string.Empty;
            ContainerName = string.Empty;
            CommonLogic();
        }

        public Common(ModuleInstanceTypeName objectType, object unitName, object containerName, object configurationState, object configurationProductiveState)
        {
            ObjectType = objectType;
            ConfigurationState = configurationState.ToString();
            ConfigurationProductiveState = configurationProductiveState.ToString();
            UnitName = unitName.ToString();
            ContainerName = containerName.ToString();
            CommonLogic(configurationState.ToString(), configurationProductiveState.ToString(), unitName.ToString(), containerName.ToString());
        }

        private void CommonLogic(string configurationState = "", string configurationProductiveState = "", string unitName = "", string containerame = "")
        {
            LogisticsAttributes = new List<string[]>();
            LogisticsColumns = new List<string[]>();
            Parameters = new List<string[]>();
            ParametersAsCsv = string.Empty;
            GeneralTriggers = new List<string[]>();
            StartTriggersDCP = new List<string[]>();
            LogisticsTriggers = new List<string[]>();
            LogisticsTriggersKeysKeyMapping = new Dictionary<int, string[]>();
            LogisticsTriggersCallDefinitionAttributes = new Dictionary<int, List<string[]>>();
            StopTriggersDCP = new List<string[]>();
        }

        public void Update(PDSFConfiguration configuration)
        {
            StoragePath = configuration.Settings.StoragePath;
            StartTimeFormat = configuration.Settings.StartTimeFormat;
            Filename = configuration.Settings.Filename;
            //
            Source = configuration.DataCollection.Source;
            LogisticsEquipmentAlias = configuration.DataCollection.Logistics.EquipmentAlias;
            //if (LogisticsEquipmentAlias == "R47-PLC")
            //{ }
            foreach (PDSFConfigurationDataCollectionLogisticsAttribute item in (from l in configuration.DataCollection.Logistics.Attributes orderby l.Use descending, l.Order select l))
                LogisticsAttributes.Add(new string[] { item.Use.ToString(), item.Order.ToString("000"), item.Key, item.Placeholder });
            foreach (PDSFConfigurationDataCollectionLogisticsColumn item in configuration.DataCollection.Logistics.Columns)
                LogisticsColumns.Add(new string[] { item.ID.ToString(), item.Prefix });
            foreach (PDSFConfigurationDataCollectionParameter1 item in (from l in configuration.DataCollection.VirtualParameters orderby l.Use descending, l.Order select l))
            {
                if (string.IsNullOrEmpty(item.Alias) && !string.IsNullOrEmpty(item.Conditions.ConditionModel.Name))
                {
                    if (item.Use)
                        Parameters.Add(new string[] { item.Use.ToString(), item.Order.ToString("000"), item.FullName, item.Conditions.ConditionModel.Name, item.HardWareId, item.Description, item.Conditions.ConditionModel.Formula, true.ToString(), (configuration.DataCollection.Logistics.Columns.Length + 1 + item.Order).ToString("000") });
                    else
                        Parameters.Add(new string[] { item.Use.ToString(), item.Order.ToString("000"), item.FullName, item.Conditions.ConditionModel.Name, item.HardWareId, item.Description, string.Empty, true.ToString(), (configuration.DataCollection.Logistics.Columns.Length + 1 + item.Order).ToString("000") });
                }
                else
                {
                    if (item.Use)
                        Parameters.Add(new string[] { item.Use.ToString(), item.Order.ToString("000"), item.FullName, item.Alias, item.HardWareId, item.Description, item.Conditions.ConditionModel.Formula, true.ToString(), (configuration.DataCollection.Logistics.Columns.Length + 1 + item.Order).ToString("000") });
                    else
                        Parameters.Add(new string[] { item.Use.ToString(), item.Order.ToString("000"), item.FullName, item.Alias, item.HardWareId, item.Description, string.Empty, true.ToString(), (configuration.DataCollection.Logistics.Columns.Length + 1 + item.Order).ToString("000") });
                }
            }
            foreach (PDSFConfigurationDataCollectionParameter item in (from l in configuration.DataCollection.Parameters orderby l.Use descending, l.Order select l))
            {
                if (string.IsNullOrEmpty(item?.Alias) && !string.IsNullOrEmpty(item?.Conditions?.ConditionModel?.Name))
                {
                    if (item.Use)
                        Parameters.Add(new string[] { item.Use.ToString(), item.Order.ToString("000"), item.FullName, item.Conditions.ConditionModel.Name, item.HardWareId, item.Description, item.Conditions.ConditionModel.Formula, false.ToString(), (configuration.DataCollection.Logistics.Columns.Length + 1 + item.Order).ToString("000") });
                    else
                        Parameters.Add(new string[] { item.Use.ToString(), item.Order.ToString("000"), item.FullName, item.Conditions.ConditionModel.Name, item.HardWareId, item.Description, string.Empty, false.ToString(), (configuration.DataCollection.Logistics.Columns.Length + 1 + item.Order).ToString("000") });
                }
                else
                {
                    if (item.Use)
                        Parameters.Add(new string[] { item.Use.ToString(), item.Order.ToString("000"), item.FullName, item.Alias, item.HardWareId, item.Description, item.Conditions.ConditionModel.Formula, false.ToString(), (configuration.DataCollection.Logistics.Columns.Length + 1 + item.Order).ToString("000") });
                    else
                        Parameters.Add(new string[] { item.Use.ToString(), item.Order.ToString("000"), item.FullName, item.Alias, item.HardWareId, item.Description, string.Empty, false.ToString(), (configuration.DataCollection.Logistics.Columns.Length + 1 + item.Order).ToString("000") });
                }
            }
            Parameters = (from l in Parameters orderby l[0] descending, l[1] select l).ToList();
            StringBuilder text = new StringBuilder();
            //text.AppendLine("\"AliasName\";\"Condition\";\"EventId\";\"ExceptionId\";\"Formula\";\"HardwareId\";\"OrderId\";\"ParameterName\";\"Remark\";\"ReportName\";\"SourceId\";\"Use\"");
            //"Test";"";"";"";"";"";"1";"MICROSCOPE01/MET08MESMICROSCOPE/Test";"";"MICROSCOPE01/MET08MESMICROSCOPE/FileRead";"";"True""
            text.AppendLine("\"Use\";\"OrderId\";\"ReportName\";\"ParameterName\";\"AliasName\";\"HardwareId\";\"Remark\";\"Formula\"");
            foreach (string[] item in Parameters)
            {
                for (int i = 0; i < 7; i++)
                    text.Append("\"").Append(item[i]).Append("\";");
                text.Remove(text.Length - 1, 1);
                text.AppendLine();
            }
            ParametersAsCsv = text.ToString();
            foreach (PDSFConfigurationDataCollectionGeneralTriggers item in configuration.DataCollection.GeneralTriggers)
            {
                foreach (PDSFConfigurationDataCollectionGeneralTriggersVariableModel gv in item.GlobalVariables)
                    GeneralTriggers.Add(new string[] { item.Name, gv.Name, gv.ParameterName, gv.Formula });
            }
            {
                PDSFConfigurationDataCollectionStartTriggersDCP item = configuration.DataCollection.StartTriggers.DCP;
                if (!(item is null))
                    StartTriggersDCP.Add(new string[] { item.Name, item.Rule, item.ResolveGlobalVariableBeforeTrigger.ToString() });
            }
            if (!(configuration.DataCollection.Logistics.Triggers.UpdateTrigger is null))
            {
                foreach (PDSFConfigurationDataCollectionLogisticsTriggersUpdateTriggerLogisticRequest item in configuration.DataCollection.Logistics.Triggers.UpdateTrigger.LogisticRequest)
                {
                    LogisticsTriggers.Add(new string[] { item.Name.ToString(), item.LogisticsColumn.Fixed.ToString(), item.Rule, item.Keys.Scenario, item.Keys.DefaultJobIndex.ToString(), item.Keys.DefaultCarrierIndex.ToString(), item.Keys.DefaultSlotIndex.ToString(), item.DataPool.ToString() });
                    if (!(item.Keys.KeyMapping is null))
                        LogisticsTriggersKeysKeyMapping.Add(item.Name, new string[] { item.Keys.KeyMapping.KeyName, item.Keys.KeyMapping.ParameterName, item.Keys.KeyMapping.Formula });
                    LogisticsTriggersCallDefinitionAttributes.Add(item.Name, new List<string[]>());
                    foreach (PDSFConfigurationDataCollectionLogisticsTriggersUpdateTriggerLogisticRequestCallDefinitionLogisticCallDefinitionAttribute att in item.CallDefinition.Attributes)
                        LogisticsTriggersCallDefinitionAttributes[item.Name].Add(new string[] { att.LogisticsKey, att.Source, att.MappedParameterName, att.Formula });
                }
            }
            {
                PDSFConfigurationDataCollectionStopTriggersDCP item = configuration.DataCollection.StopTriggers.DCP;
                if (!(item is null))
                    StopTriggersDCP.Add(new string[] { item.Name, item.Rule, item.ResolveGlobalVariableBeforeTrigger.ToString(), item.ResetGlobalVariablesAfterTrigger.ToString() });
            }
        }

    }

}