// GET: Admin/Bloques/Copy
        public ActionResult Copy(int id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var bloque = db.Bloques.Include(s => s.TipoEjercicio).SingleOrDefault(s => s.BloqueId == id);

            if (bloque == null)
            {
                return(HttpNotFound());
            }

            BloquesCopyViewModel viewModel = new BloquesCopyViewModel();

            viewModel.Bloque = bloque;
            viewModel.InicializarDesplegables();

            return(View(viewModel));
        }
        public ActionResult Copy(BloquesCopyViewModel viewModel)
        {
            int id = viewModel.Bloque.BloqueId;

            viewModel.Bloque = db.Bloques.Include(s => s.TipoEjercicio)
                               .SingleOrDefault(s => s.BloqueId == id);
            if (viewModel.Bloque != null)
            {
                viewModel.Bloque.SubTemaId = viewModel.SubTemaCopiarId;
                viewModel.Bloque.AreaId    = viewModel.AreaCopiarId;

                db.Bloques.Add(viewModel.Bloque);
                db.SaveChanges();

                //Meter en nuevos clientes
                var clientes = db.ClienteSubTemas.Where(cb => cb.SubTemaId == viewModel.SubTemaCopiarId);
                foreach (var clienteBloque in clientes)
                {
                    db.ClienteBloques.Add(new ClienteBloque()
                    {
                        BloqueId  = viewModel.Bloque.BloqueId,
                        ClienteId = clienteBloque.ClienteId
                    });
                }
                db.SaveChanges();

                var bloque = db.Bloques.Include(s => s.TipoEjercicio)
                             .Include(s => s.Tests).Include(s => s.AudioSentenceExercises).Include(s => s.Skillwises).Include(s => s.MatchTheWords)
                             .Include(s => s.FillTheGaps).Include(s => s.WordByWords).Include(s => s.Crucigramas).Include(s => s.MatchThePictures)
                             .Include(s => s.TrueFalses).Include(s => s.FillTheBoxs)
                             .SingleOrDefault(s => s.BloqueId == id);


                if (bloque.Tests.Count > 0)
                {
                    foreach (Test test in bloque.Tests.ToList())
                    {
                        test.BloqueId  = viewModel.Bloque.BloqueId;
                        test.SubTemaId = viewModel.SubTemaCopiarId;
                        test.AreaId    = viewModel.AreaCopiarId;
                        db.Tests.Add(test);
                        db.SaveChanges();

                        if (!string.IsNullOrEmpty(test.UrlImagen))
                        {
                            string oldPathAndName = "~/media/upload/Test/" + test.UrlImagen;
                            if (System.IO.File.Exists(Server.MapPath(oldPathAndName)))
                            {
                                string newPathAndName = "~/media/upload/Test/" + test.Id + ".jpg";

                                if (System.IO.File.Exists(Server.MapPath(newPathAndName)))
                                {
                                    System.IO.File.Delete(Server.MapPath(newPathAndName));
                                }

                                System.IO.File.Copy(Server.MapPath(oldPathAndName), Server.MapPath(newPathAndName));

                                test.UrlImagen = test.Id + ".jpg";
                            }
                            else
                            {
                                test.UrlImagen = null;
                            }
                            db.Entry(test).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                }
                if (bloque.Skillwises.Count > 0)
                {
                    foreach (Skillwise skillwise in bloque.Skillwises.ToList())
                    {
                        skillwise.BloqueId  = viewModel.Bloque.BloqueId;
                        skillwise.SubTemaId = viewModel.SubTemaCopiarId;
                        skillwise.AreaId    = viewModel.AreaCopiarId;
                        db.Skillwises.Add(skillwise);
                        db.SaveChanges();
                    }
                }
                if (bloque.MatchTheWords.Count > 0)
                {
                    foreach (MatchTheWord MatchTheWord in bloque.MatchTheWords.ToList())
                    {
                        MatchTheWord.BloqueId  = viewModel.Bloque.BloqueId;
                        MatchTheWord.SubTemaId = viewModel.SubTemaCopiarId;
                        MatchTheWord.AreaId    = viewModel.AreaCopiarId;
                        db.MatchTheWords.Add(MatchTheWord);
                        db.SaveChanges();
                    }
                }
                if (bloque.TrueFalses.Count > 0)
                {
                    foreach (TrueFalse TrueFalse in bloque.TrueFalses.ToList())
                    {
                        TrueFalse.BloqueId  = viewModel.Bloque.BloqueId;
                        TrueFalse.SubTemaId = viewModel.SubTemaCopiarId;
                        TrueFalse.AreaId    = viewModel.AreaCopiarId;
                        db.TrueFalses.Add(TrueFalse);
                        db.SaveChanges();


                        if (!string.IsNullOrEmpty(TrueFalse.UrlImagen))
                        {
                            string oldPathAndName = "~/media/upload/TrueFalse/" + TrueFalse.UrlImagen;
                            if (System.IO.File.Exists(Server.MapPath(oldPathAndName)))
                            {
                                string newPathAndName = "~/media/upload/TrueFalse/" + TrueFalse.Id + ".jpg";

                                if (System.IO.File.Exists(Server.MapPath(newPathAndName)))
                                {
                                    System.IO.File.Delete(Server.MapPath(newPathAndName));
                                }

                                System.IO.File.Copy(Server.MapPath(oldPathAndName), Server.MapPath(newPathAndName));

                                TrueFalse.UrlImagen = TrueFalse.Id + ".jpg";
                            }
                            else
                            {
                                TrueFalse.UrlImagen = null;
                            }
                            db.Entry(TrueFalse).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                }

                if (bloque.MatchThePictures.Count > 0)
                {
                    foreach (MatchThePicture MatchThePicture in bloque.MatchThePictures.ToList())
                    {
                        MatchThePicture.BloqueId  = viewModel.Bloque.BloqueId;
                        MatchThePicture.SubTemaId = viewModel.SubTemaCopiarId;
                        MatchThePicture.AreaId    = viewModel.AreaCopiarId;
                        db.MatchThePictures.Add(MatchThePicture);
                        db.SaveChanges();


                        if (!string.IsNullOrEmpty(MatchThePicture.UrlImagen))
                        {
                            string oldPathAndName = "~/media/upload/MatchThePicture/" + MatchThePicture.UrlImagen;
                            if (System.IO.File.Exists(Server.MapPath(oldPathAndName)))
                            {
                                string newPathAndName = "~/media/upload/MatchThePicture/" + MatchThePicture.Id + ".jpg";

                                if (System.IO.File.Exists(Server.MapPath(newPathAndName)))
                                {
                                    System.IO.File.Delete(Server.MapPath(newPathAndName));
                                }

                                System.IO.File.Copy(Server.MapPath(oldPathAndName), Server.MapPath(newPathAndName));

                                MatchThePicture.UrlImagen = MatchThePicture.Id + ".jpg";
                            }
                            else
                            {
                                MatchThePicture.UrlImagen = null;
                            }
                            db.Entry(MatchThePicture).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                }

                if (bloque.FillTheBoxs.Count > 0)
                {
                    foreach (FillTheBox FillTheBox in bloque.FillTheBoxs.ToList())
                    {
                        FillTheBox.BloqueId  = viewModel.Bloque.BloqueId;
                        FillTheBox.SubTemaId = viewModel.SubTemaCopiarId;
                        FillTheBox.AreaId    = viewModel.AreaCopiarId;
                        db.FillTheBoxs.Add(FillTheBox);
                        db.SaveChanges();


                        if (!string.IsNullOrEmpty(FillTheBox.UrlImagen))
                        {
                            string oldPathAndName = "~/media/upload/imagen_fillthebox/" + FillTheBox.UrlImagen;
                            if (System.IO.File.Exists(Server.MapPath(oldPathAndName)))
                            {
                                string newPathAndName = "~/media/upload/imagen_fillthebox/" + FillTheBox.Id + ".jpg";

                                if (System.IO.File.Exists(Server.MapPath(newPathAndName)))
                                {
                                    System.IO.File.Delete(Server.MapPath(newPathAndName));
                                }

                                System.IO.File.Copy(Server.MapPath(oldPathAndName), Server.MapPath(newPathAndName));

                                FillTheBox.UrlImagen = FillTheBox.Id + ".jpg";
                            }
                            else
                            {
                                FillTheBox.UrlImagen = null;
                            }
                            db.Entry(FillTheBox).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        if (!string.IsNullOrEmpty(FillTheBox.FicheroAudio))
                        {
                            string oldPathAndName = "~/media/upload/audio_fillthebox/" + FillTheBox.FicheroAudio;
                            if (System.IO.File.Exists(Server.MapPath(oldPathAndName)))
                            {
                                string newPathAndName = "~/media/upload/audio_fillthebox/" + FillTheBox.Id + ".mp3";

                                if (System.IO.File.Exists(Server.MapPath(newPathAndName)))
                                {
                                    System.IO.File.Delete(Server.MapPath(newPathAndName));
                                }

                                System.IO.File.Copy(Server.MapPath(oldPathAndName), Server.MapPath(newPathAndName));

                                FillTheBox.FicheroAudio = FillTheBox.Id + ".mp3";
                            }
                            else
                            {
                                FillTheBox.FicheroAudio = null;
                            }
                            db.Entry(FillTheBox).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                }
                if (bloque.FillTheGaps.Count > 0)
                {
                    foreach (FillTheGap FillTheGap in bloque.FillTheGaps.ToList())
                    {
                        FillTheGap.BloqueId  = viewModel.Bloque.BloqueId;
                        FillTheGap.SubTemaId = viewModel.SubTemaCopiarId;
                        FillTheGap.AreaId    = viewModel.AreaCopiarId;
                        db.FillTheGaps.Add(FillTheGap);
                        db.SaveChanges();
                    }
                }
                if (bloque.WordByWords.Count > 0)
                {
                    foreach (WordByWord WordByWord in bloque.WordByWords.ToList())
                    {
                        WordByWord.BloqueId  = viewModel.Bloque.BloqueId;
                        WordByWord.SubTemaId = viewModel.SubTemaCopiarId;
                        WordByWord.AreaId    = viewModel.AreaCopiarId;
                        db.WordByWords.Add(WordByWord);
                        db.SaveChanges();
                    }
                }
                if (bloque.Crucigramas.Count > 0)
                {
                    foreach (Crucigrama Crucigrama in bloque.Crucigramas.ToList())
                    {
                        var crucigramaNuevo = db.Crucigramas.Include(cr => cr.CasillaCrucigramas).AsNoTracking().FirstOrDefault(cr => cr.Id == Crucigrama.Id);

                        crucigramaNuevo.BloqueId = viewModel.Bloque.BloqueId;

                        crucigramaNuevo.SubTemaId = viewModel.SubTemaCopiarId;
                        crucigramaNuevo.AreaId    = viewModel.AreaCopiarId;
                        db.Crucigramas.Add(crucigramaNuevo);
                        db.SaveChanges();
                    }
                }


                if (bloque.AudioSentenceExercises.Count > 0)
                {
                    foreach (AudioSentenceExercise AudioSentenceExercise in bloque.AudioSentenceExercises.ToList())
                    {
                        AudioSentenceExercise.BloqueId  = viewModel.Bloque.BloqueId;
                        AudioSentenceExercise.SubTemaId = viewModel.SubTemaCopiarId;
                        AudioSentenceExercise.AreaId    = viewModel.AreaCopiarId;
                        db.AudioSentenceExercises.Add(AudioSentenceExercise);
                        db.SaveChanges();

                        if (!string.IsNullOrEmpty(AudioSentenceExercise.FicheroAudio))
                        {
                            string oldPathAndName = "~/media/upload/audio_ejercicio/" + AudioSentenceExercise.FicheroAudio;
                            if (System.IO.File.Exists(Server.MapPath(oldPathAndName)))
                            {
                                string newPathAndName = "~/media/upload/audio_ejercicio/" + AudioSentenceExercise.Id + ".mp3";
                                if (System.IO.File.Exists(newPathAndName))
                                {
                                    System.IO.File.Delete(newPathAndName);
                                }

                                System.IO.File.Copy(Server.MapPath(oldPathAndName), Server.MapPath(newPathAndName));

                                AudioSentenceExercise.FicheroAudio = AudioSentenceExercise.Id + ".mp3";
                            }
                            else
                            {
                                AudioSentenceExercise.FicheroAudio = null;
                            }
                            db.Entry(AudioSentenceExercise).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                }


                return(RedirectToAction("Create", bloque.TipoEjercicio.Controlador, new { id = viewModel.Bloque.BloqueId }));
            }

            viewModel.InicializarDesplegables();
            return(View(viewModel));
        }