/* Award AddAward(int playId, int festivalId, int categoryId, int userId);

         List<AwardMixedDTO> SearchAwards(AwardsSearchingCriteria criteria, int pageNr, int pageAmount)

         public ListResponse<ModuleDto> GetModules(SingleRequest request)
         {
             //Warning: unbound list read from db, do not do this at home!
             return ListResponse.Create(request,
                 InTransaction(tc => tc.Entities.ModuleInstances.Include(mi => mi.ModuleDef).ToList()
                                                         .Select(_moduleAssembler.ToSimpleDto).ToList()));
         }
         */

        //public string GetData(int value)
        //{
        //    return string.Format("You entered: {0}", value);
        //}

        //public CompositeType GetDataUsingDataContract(CompositeType composite)
        //{
        //    if (composite == null)
        //    {
        //        throw new ArgumentNullException("composite");
        //    }
        //    if (composite.BoolValue)
        //    {
        //        composite.StringValue += "Suffix";
        //    }
        //    return composite;
        //}

        #region Awards
        public SingleItemResponse<AwardDataDTO> AddAward(AwardDataDTO newAward)
        {
            var newAwardFull = new Award()
            {
                CategoryId = newAward.CategoryId,
                PlayId = newAward.PlayId,
                EditedBy = GetUserId(),
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                try
                {
                    context.Awards.Add(newAwardFull);
                    context.SaveChanges();
                    int id = newAwardFull.AwardId;
                    return GetAward(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        static void Main(string[] args)
        {

            string pass = "";
            int id=0;
            ICryptoService cryptoService = new PBKDF2();
            const string pepper = "50.L1`(f761OJdG6fc835M(5(+Ju2!P6,4330_N*/%xz<j7(N15KC'8l997'0c0CEg";

            Console.WriteLine("Select user by id:");
            if (int.TryParse(Console.ReadLine(),out id))
            {
                using (var context = new AF_Context())
                {
                    try
                    {
                        User user = context.Users.First(u => u.UserId == id);
                        if (user != null)
                        {
                            while (string.IsNullOrEmpty(pass))
                            {
                                Console.WriteLine("Input Password:");
                                pass = Console.ReadLine();
                            }
                            user.Salt = cryptoService.GenerateSalt();
                            user.Password = cryptoService.Compute(cryptoService.Compute(pass, user.Salt), pepper);

                        }
                        context.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        throw;
                    }
                }
            }



        }
 public ActionResult Create([Bind(Include = "FestivalId,Year,BeginningDate,EndDate")] FestivalDTO updateData)
 {
     if (ModelState.IsValid)
     {
         var updateDataFull = new Festival()
         {
             //FestivalId = updateData.FestivalId,
             Year = updateData.Year,
             BeginningDate = updateData.BeginningDate,
             EndDate = updateData.EndDate,
             EditedBy = 1,//GetUserId(),
             EditDate = DateTime.Now
         };
         using (var context = new AF_Context())
         {
             context.Festivals.Add(updateDataFull);
             context.SaveChanges();
             return RedirectToAction("Index");
         }
     }
     //ViewBag.EditedBy = new SelectList(db.Users, "UserId", "Login", play.EditedBy);
     //ViewBag.FestivalId = new SelectList(db.Festivals, "FestivalId", "FestivalId", play.FestivalId);
     return View(updateData);
 }
        public SingleItemResponse<AwardDataDTO> UpdateAward(AwardDataDTO updateData)
        {
            var updateDataFull = new Award()
            {
                AwardId = updateData.AwardId,
                CategoryId = updateData.CategoryId,
                PlayId = updateData.PlayId,
                EditedBy = GetUserId(),
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                try
                {
                    Award awa = context.Awards.First(a => a.AwardId == updateData.AwardId);
                    context.Entry(awa).CurrentValues.SetValues(updateDataFull);
                    context.SaveChanges();
                    int id = updateData.AwardId;
                    return GetAward(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<PositionDTO> UpdatePosition(PositionDTO updateData)
        {
            var updateDataFull = new Position()
            {
                PositionId = updateData.PositionId,
                PositionTitle = updateData.PositionTitle,
                Section = updateData.Section,
                Order = updateData.Order,
                EditedBy = GetUserId(),
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                try
                {
                    Position pos = context.Positions.First(p => p.PositionId == updateData.PositionId);
                    context.Entry(pos).CurrentValues.SetValues(updateDataFull);
                    context.SaveChanges();
                    int id = updateData.PositionId;
                    return GetPosition(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<PositionDTO> AddPosition(PositionDTO newPosition)
        {
            var newPositionFull = new Position()
            {
                PositionTitle = newPosition.PositionTitle,
                Section = newPosition.Section,
                Order = newPosition.Order,
                EditedBy = GetUserId(),
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                try
                {
                    context.Positions.Add(newPositionFull);
                    context.SaveChanges();
                    int id = newPositionFull.PositionId;
                    return GetPosition(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<PlayDataDTO> UpdatePlay(PlayDataDTO updateData)
        {
            var updateDataFull = new Play()
            {
                PlayId = updateData.PlayId,
                Title = updateData.Title,
                Author = updateData.Author,
                FestivalId = updateData.FestivalId,
                Day = updateData.Day,
                Order = updateData.Order,
                PlayedBy = updateData.PlayedBy,
                Motto = updateData.Motto,
                EditedBy = GetUserId(),
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                try
                {
                    Play pla = context.Plays.First(p => p.PlayId == updateData.PlayId);
                    context.Entry(pla).CurrentValues.SetValues(updateDataFull);
                    context.SaveChanges();
                    int id = updateData.PlayId;
                    return GetPlay(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<JobDTO> UpdateJob(JobDTO updateData)
        {
            var updateDataFull = new Job()
            {
                JobId = updateData.JobId,
                JobTitle = updateData.JobTitle,
                EditedBy = GetUserId(),
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                try
                {
                    Job jo = context.Jobs.First(j => j.JobId == updateData.JobId);
                    context.Entry(jo).CurrentValues.SetValues(updateDataFull);
                    context.SaveChanges();
                    int id = updateData.JobId;
                    return GetJob(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<PlayDataDTO> AddPlay(PlayDataDTO newPlay)
        {
            var newPlayFull = new Play()
            {
                Title = newPlay.Title,
                Author = newPlay.Author,
                FestivalId = newPlay.FestivalId,
                Day = newPlay.Day,
                Order = newPlay.Order,
                PlayedBy = newPlay.PlayedBy,
                Motto = newPlay.Motto,
                EditedBy = GetUserId(),
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                try
                {
                    context.Plays.Add(newPlayFull);
                    context.SaveChanges();
                    int id = newPlayFull.PlayId;
                    return GetPlay(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<FestivalDTO> UpdateFestival(FestivalDTO updateData)
        {
            var updateDataFull = new Festival()
            {
                FestivalId = updateData.FestivalId,
                Year = updateData.Year,
                BeginningDate = updateData.BeginningDate,
                EndDate = updateData.EndDate,
                EditedBy = GetUserId(),
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                try
                {
                    Festival fes = context.Festivals.First(f => f.FestivalId == updateData.FestivalId);
                    context.Entry(fes).CurrentValues.SetValues(updateDataFull);
                    context.SaveChanges();
                    int id = updateData.FestivalId;
                    return GetFestival(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<JobDTO> AddJob(JobDTO newJob)
        {
            var newJobFull = new Job()
            {
                JobTitle = newJob.JobTitle,
                EditedBy = GetUserId(),
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                try
                {
                    context.Jobs.Add(newJobFull);
                    context.SaveChanges();
                    int id = newJobFull.JobId;
                    return GetJob(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<FestivalDTO> AddFestival(FestivalDTO newFestival)
        {
            var newFestivalFull = new Festival()
            {
                Year = newFestival.Year,
                BeginningDate = newFestival.BeginningDate,
                EndDate = newFestival.EndDate,
                EditedBy = GetUserId(),
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                try
                {
                    context.Festivals.Add(newFestivalFull);
                    context.SaveChanges();
                    int id = newFestivalFull.FestivalId;
                    return GetFestival(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<CategoryDTO> UpdateCategory(CategoryDTO updateData)
        {
            var updateDataFull = new Category
            {
                CategoryId = updateData.CategoryId,
                Title = updateData.Title,
                EditDate = DateTime.Now,
                EditedBy = GetUserId(),
                Group = updateData.Group,
                Order = updateData.Order
            };

            using (var context = new AF_Context())
            {
                try
                {
                    Category cat = context.Categories.First(c => c.CategoryId == updateData.CategoryId);
                    context.Entry(cat).CurrentValues.SetValues(updateDataFull);
                    context.SaveChanges();
                    int id = updateData.CategoryId;
                    return GetCategory(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<CategoryDTO> AddCategory(CategoryDTO newCategory)
        {
            var newCategoryFull = new Category
            {
                Title = newCategory.Title,
                EditDate = DateTime.Now,
                EditedBy = GetUserId(),
                Group = newCategory.Group,
                Order = newCategory.Order
            };

            using (var context = new AF_Context())
            {
                try
                {
                    context.Categories.Add(newCategoryFull);
                    context.SaveChanges();
                    int id = newCategoryFull.CategoryId;
                    return GetCategory(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public ActionResult Edit([Bind(Include = "PlayId,Title,Author,FestivalId,Day,Order,PlayedBy,Motto")] PlayDataDTO updateData)
        {
            if (ModelState.IsValid)
            {
            var updateDataFull = new Play()
            {
                PlayId = updateData.PlayId,
                Title = updateData.Title,
                Author = updateData.Author,
                FestivalId = updateData.FestivalId,
                Day = updateData.Day,
                Order = updateData.Order,
                PlayedBy = updateData.PlayedBy,
                Motto = updateData.Motto,
                EditedBy = 1,
                //EditedBy = GetUserId(),    //???????
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                    Play pla = context.Plays.Find(updateData.PlayId);// First(p => p.PlayId == updateData.PlayId);
                    string return_s = "Details/" + pla.FestivalId;
                    context.Entry(pla).CurrentValues.SetValues(updateDataFull); //check for substituding only edited
                    //context.Entry(updateDataFull).State = EntityState.Modified;
                    context.SaveChanges();
                    //int id = updateData.PlayId;
                    //return Redirect(Request.UrlReferrer.ToString()); //RedirectToAction("Index")
                    //return RedirectToAction(return_s);
                    return RedirectToAction(return_s, "Festival");
                }
            }
            //ViewBag.EditedBy = new SelectList(db.Users, "UserId", "Login", festival.EditedBy);
            return View(updateData);
        }
 public ActionResult Create([Bind(Include = "PlayId,Title,Author,FestivalId,Day,Order,PlayedBy,Motto")] PlayDataDTO updateData)
 {
     if (ModelState.IsValid)
     {
         var updateDataFull = new Play()
         {
             //PlayId = updateData.PlayId,
             Title = updateData.Title,
             Author = updateData.Author,
             FestivalId = updateData.FestivalId,
             Day = updateData.Day,
             Order = updateData.Order,
             PlayedBy = updateData.PlayedBy,
             Motto = updateData.Motto,
             EditedBy = 1,
             //EditedBy = GetUserId(),    //???????
             EditDate = DateTime.Now
         };
         using (var context = new AF_Context())
         {
             context.Plays.Add(updateDataFull);
             string return_s = "Details/" + updateDataFull.FestivalId;
             context.SaveChanges();
             return RedirectToAction(return_s, "Festival"); //TODO: Correct for many and for old id
         }
     }
         //ViewBag.EditedBy = new SelectList(db.Users, "UserId", "Login", updateData.EditedBy);
         return View(updateData);
     
     }