using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZionRobot.Models.Process
{
    public class Workflow
    {
        private Models.Workflow.RoadMap workflow;
        private List<ZionSchematics.Schematics> blueprints;
        private List<Models.Items.Folder> folders;
        private Persistence.Queues queues;

        private List<Models.Source.Origin> inputConfigurations;
        private List<Models.Source.Origin> outputConfigurations;
        private List<Models.Source.Origin> syncConfigurations;

        public Workflow(Models.Workflow.RoadMap schema)
        {
            workflow = schema;
        }

        internal Models.Workflow.RoadMap Flow { get => workflow; }

        internal Persistence.Queues Queues
        {
            get
            {
                if (queues is null)
                    queues = new Persistence.Queues();
                return queues;
            }
        }
        internal List<Models.Items.Folder> Folders
        {
            get
            {
                if (folders is null)
                    folders = new List<Models.Items.Folder>();
                return folders;
            }

            set
            {
                folders = value;
            }
        }

        internal List<ZionSchematics.Schematics> Blueprints
        {
            get
            {
                if (blueprints is null)
                {
                    blueprints = new List<ZionSchematics.Schematics>();
                    string location = Persistence.Basic.BlueprintsFolder;
                    workflow.InputSettings.Configurations.ForEach(x =>
                    {
                        if (System.IO.File.Exists(string.Format(@"{0}.bprt", System.IO.Path.Combine(location, x.Blueprint))))
                        {
                            blueprints.Add(new ZionSchematics.Schematics(string.Format(@"{0}.bprt", System.IO.Path.Combine(location, x.Blueprint))));
                        }
                    });
                }

                return blueprints;
            }

            set
            {
                blueprints = value;
            }
        }

        internal List<Models.Source.Origin> InputConfigurations
        {
            get 
            {
                if (inputConfigurations is null)
                {
                    inputConfigurations = new List<Models.Source.Origin>();

                    workflow.InputSettings.Configurations.ForEach(config =>
                    {
                        inputConfigurations.Add(
                            new Models.Source.Origin(
                                System.IO.Path.Combine(Persistence.Basic.ConfigurationFolder, config.Source + @".xml")));
                    });
                }

                return inputConfigurations;
            }
        }

        internal List<Models.Source.Origin> OutputConfigurations
        {
            get
            {
                if (outputConfigurations is null)
                {
                    outputConfigurations = new List<Models.Source.Origin>();

                    workflow.OutputSettings.Configurations.ForEach(config =>
                    {
                        outputConfigurations.Add(
                            new Models.Source.Origin(
                                System.IO.Path.Combine(Persistence.Basic.ConfigurationFolder, config.Source + @".xml")));
                    });
                }

                return outputConfigurations;
            }
        }


        internal List<Models.Source.Origin> SyncConfigurations
        {
            get
            {
                if (syncConfigurations is null)
                {
                    syncConfigurations = new List<Models.Source.Origin>();

                    workflow.SyncSettings.Configurations.ForEach(config =>
                    {
                        syncConfigurations.Add(
                            new Models.Source.Origin(
                                System.IO.Path.Combine(Persistence.Basic.ConfigurationFolder, config.Source + @".xml")));
                    });
                }

                return syncConfigurations;
            }
        }


        private Tasks.Queue queue;

        internal Tasks.Queue Queue
        {
            get
            {
                if (queue == null)
                {
                    queue = new Tasks.Queue(this);
                }
                return queue;
            }
        }

        /*private Tasks.Input input;
        private Tasks.Queue queue;
        private Tasks.Extractor extractor;
        private Tasks.Output output;

        internal Tasks.Input Input
        {
            get
            {
                if (input == null)
                {
                    input = new Tasks.Input(this);
                }
                return input;
            }
        }

        internal Tasks.Queue Queue
        {
            get
            {
                if (queue == null)
                {
                    queue = new Tasks.Queue(this);
                }
                return queue;
            }
        }

        internal Tasks.Extractor Extractor
        {
            get
            {
                if (extractor == null)
                {
                    extractor = new Tasks.Extractor(this);
                }
                return extractor;
            }
        }

        internal Tasks.Output Output
        {
            get
            {
                if (output == null)
                {
                    output = new Tasks.Output(this);
                }
                return output;
            }
        }

        internal bool isAlive()
        {
            return Queue.isRunning ||
                Extractor.isRunning ||
                Output.isRunning;
        }*/
    }
}
