using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KendoUIInsert.Models;

namespace KendoUIInsert.Repository
{
    public static class TaskRepository
    {
        public static List<TaskModuleDto> GetModules()
        {
            var pools = GetWorkPoolList();
            var classes = GetSharedClassesList();

            var modules = HttpContext.Current.Session["Modules"] as List<TaskModuleDto>;

            if (modules == null)
            {
                modules = new List<TaskModuleDto>()
                              {
                                  new TaskModuleDto()
                                      {
                                          Id = Guid.NewGuid(),
                                          Name = "Module 1",
                                          Caption = "Module 1",
                                          Description = "Module 1",
                                          Version = 0,
                                          SharedComponentClassId = classes[0].Id,
                                          DefaultWorkPoolId = pools[0].Id
                                      },
                                  new TaskModuleDto()
                                      {
                                          Id = Guid.NewGuid(),
                                          Name = "Module 2",
                                          Caption = "Module 2",
                                          Description = "Module 2",
                                          Version = 0,
                                          SharedComponentClassId = classes[1].Id,
                                          DefaultWorkPoolId = pools[1].Id
                                      },
                                  new TaskModuleDto()
                                      {
                                          Id = Guid.NewGuid(),
                                          Name = "Module 3",
                                          Caption = "Module 3",
                                          Description = "Module 3",
                                          Version = 0,
                                          SharedComponentClassId = classes[2].Id,
                                          DefaultWorkPoolId = pools[2].Id
                                      },
                              };
                HttpContext.Current.Session["Modules"] = modules;
            }

            return modules;
        }


        public static void Create(TaskModuleDto module)
        {
            var modules = HttpContext.Current.Session["Modules"] as List<TaskModuleDto>;

            if (modules == null)
                modules = new List<TaskModuleDto>();

            modules.Add(module);
        }

        public static void Update(TaskModuleDto module)
        {
            var modules = HttpContext.Current.Session["Modules"] as List<TaskModuleDto>;

            if (modules == null)
                modules = new List<TaskModuleDto>();

            var existing = modules.FirstOrDefault(m => m.Id == module.Id);

            if (existing == null)
                return;

            existing.Name = module.Name;
            existing.Caption = module.Caption;
            existing.Description = module.Description;
            existing.Version = module.Version;
            existing.SharedComponentClassId = module.SharedComponentClassId;
            existing.DefaultWorkPoolId = module.DefaultWorkPoolId;
        }

        public static void Delete(Guid moduleId)
        {
            var modules = HttpContext.Current.Session["Modules"] as List<TaskModuleDto>;

            if (modules == null)
                modules = new List<TaskModuleDto>();

            var existing = modules.FirstOrDefault(m => m.Id == moduleId);

            if (existing == null)
                return;

            modules.Remove(existing);
        }


        public static List<SharedComponentClassList> GetSharedClassesList()
        {
            return new List<SharedComponentClassList>()
                       {
                           new SharedComponentClassList() {Id = Guid.NewGuid(), Name = "Class 1"},
                           new SharedComponentClassList() {Id = Guid.NewGuid(), Name = "Class 2"},
                           new SharedComponentClassList() {Id = Guid.NewGuid(), Name = "Class 3"},
                           new SharedComponentClassList() {Id = Guid.NewGuid(), Name = "Class 4"}
                       };
        }

        public static List<WorkPoolList> GetWorkPoolList()
        {
            return new List<WorkPoolList>()
                       {
                           new WorkPoolList() {Id = Guid.NewGuid(), Name = "Pool 1"},
                           new WorkPoolList() {Id = Guid.NewGuid(), Name = "Pool 2"},
                           new WorkPoolList() {Id = Guid.NewGuid(), Name = "Pool 3"},
                           new WorkPoolList() {Id = Guid.NewGuid(), Name = "Pool 4"}
                       };
        }
    }
}
