using StubAP.ViewModels;
using StubCore.Entities;
using StubCore.Configuration;
using StubAP.Plugins.Binders;
using System.Collections.Generic;
using StubCore.Plugins.DataCollector;

namespace StubAP.Models
{
    public class DashboardModel
    {
        public readonly string _title = "Dashboard";

        public DashboardViewModel Init()
        {
            var _view = new DashboardViewModel();
            _view._profiles = CreateList();

            return _view;
        }

        public List<ProfileItem> CreateList()
        {
            var _binder = new ProfileBinder();

            var _list = new List<ProfileItem>();
            var _profiles = GetProfiles();

            foreach(Profile _profile in _profiles)
            {
                var files = GetFilesCount(_profile.id);
                var nodes = GetNodes(_profile.id);
                var values = GetValues(nodes);

                _list.Add(_binder.CreateProfileItem(_profile, files, nodes.Count, values));
            }

            return _list;
        }

        internal List<Profile> GetProfiles()
        {
            var config = ConfigManager.GetConfig("Profiles");
            var _profiles = new ProfilesCollector(config.file, config.folder);

            return _profiles.GetFiles<Profile>();
        } 

        internal int GetFilesCount(int id)
        {
            var config = ConfigManager.GetConfig("Files");
            var _files = new FilesCollector(config.file, config.folder);

            return _files.GetFilesCount(id);
        }
        
        internal List<Node> GetNodes(int id)
        {
            var config = ConfigManager.GetConfig("Nodes");
            var _nodes = new NodesCollector(config.file, config.folder);

            return _nodes.GetNodesforProfile(id);
        }

        internal int GetValues(List<Node> nodes)
        {
            var nodeIDs = new List<int>();

            foreach(Node node in nodes)
            {
                nodeIDs.Add(node.id);
            }

            var config = ConfigManager.GetConfig("Nodes");
            var values = new NodeValuesCollector(config.file, config.folder);

            var _values = values.GetAllValues(nodeIDs);

            return _values.Count;
        }
    }
}