// GET: /FinishedProductMaster/Create

        public ActionResult Create(int id, bool sample)//ProductTypeId
        {
            FinishedProductViewModel vm = new FinishedProductViewModel();

            vm.IsSample        = sample;
            ViewBag.Sample     = sample;
            vm.IsActive        = true;
            vm.ProductTypeId   = id;
            vm.ProductTypeName = new ProductTypeService(_unitOfWork).Find(id).ProductTypeName;
            vm.DivisionId      = (int)System.Web.HttpContext.Current.Session["DivisionId"];
            return(View("Create", vm));
        }
        // GET: /ProductMaster/Edit/5

        public ActionResult Edit(int id)
        {
            FinishedProduct          pt = _FinishedProductService.Find(id);
            FinishedProductViewModel vm = AutoMapper.Mapper.Map <FinishedProduct, FinishedProductViewModel>(pt);
            var         pstid           = (int)ProductSizeTypeConstants.StandardSize;
            ProductSize tem             = new ProductSizeService(_unitOfWork).FindProductSize(pstid, pt.ProductId);
            ProductType typ             = new ProductTypeService(_unitOfWork).GetTypeForProduct(id);

            vm.ProductTypeId   = typ.ProductTypeId;
            vm.ProductTypeName = typ.ProductTypeName;
            ViewBag.Sample     = vm.IsSample;
            if (tem != null)
            {
                vm.SizeId = tem.SizeId;
            }
            if (pt == null)
            {
                return(HttpNotFound());
            }
            return(View("Create", vm));
        }
        public ActionResult Post(FinishedProductViewModel vm)
        {
            if (ModelState.IsValid)
            {
                if (vm.ProductId <= 0)
                {
                    FinishedProduct pt = AutoMapper.Mapper.Map <FinishedProductViewModel, FinishedProduct>(vm);
                    pt.CreatedDate  = DateTime.Now;
                    pt.ModifiedDate = DateTime.Now;
                    pt.CreatedBy    = User.Identity.Name;
                    pt.ModifiedBy   = User.Identity.Name;
                    pt.UnitId       = UnitConstants.Pieces;
                    pt.ObjectState  = Model.ObjectState.Added;
                    _FinishedProductService.Create(pt);

                    if (!(vm.SizeId <= 0))
                    {
                        ProductSize ps    = new ProductSize();
                        var         pstid = (int)ProductSizeTypeConstants.StandardSize;
                        ps.SizeId            = vm.SizeId;
                        ps.ProductId         = vm.ProductId;
                        ps.ProductSizeTypeId = pstid;
                        ps.CreatedBy         = User.Identity.Name;
                        ps.ModifiedBy        = User.Identity.Name;
                        ps.CreatedDate       = DateTime.Now;
                        ps.ModifiedDate      = DateTime.Now;
                        new ProductSizeService(_unitOfWork).Create(ps);
                    }
                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        return(View("Create", vm));
                    }



                    #region

                    //Saving Images if any uploaded after UnitOfWorkSave

                    if (Request.Files[0] != null && Request.Files[0].ContentLength > 0)
                    {
                        //For checking the first time if the folder exists or not-----------------------------
                        string uploadfolder;
                        int    MaxLimit;
                        int.TryParse(ConfigurationManager.AppSettings["MaxFileUploadLimit"], out MaxLimit);
                        var x = (from iid in db.Counter
                                 select iid).FirstOrDefault();
                        if (x == null)
                        {
                            uploadfolder = System.Guid.NewGuid().ToString();
                            Counter img = new Counter();
                            img.ImageFolderName = uploadfolder;
                            img.ModifiedBy      = User.Identity.Name;
                            img.CreatedBy       = User.Identity.Name;
                            img.ModifiedDate    = DateTime.Now;
                            img.CreatedDate     = DateTime.Now;
                            new CounterService(_unitOfWork).Create(img);
                            _unitOfWork.Save();
                        }

                        else
                        {
                            uploadfolder = x.ImageFolderName;
                        }


                        //For checking if the image contents length is greater than 100 then create a new folder------------------------------------

                        if (!Directory.Exists(System.Web.HttpContext.Current.Request.MapPath("~/Uploads/" + uploadfolder)))
                        {
                            Directory.CreateDirectory(System.Web.HttpContext.Current.Request.MapPath("~/Uploads/" + uploadfolder));
                        }

                        int count = Directory.GetFiles(System.Web.HttpContext.Current.Request.MapPath("~/Uploads/" + uploadfolder)).Length;

                        if (count >= MaxLimit)
                        {
                            uploadfolder = System.Guid.NewGuid().ToString();
                            var u = new CounterService(_unitOfWork).Find(x.CounterId);
                            u.ImageFolderName = uploadfolder;
                            new CounterService(_unitOfWork).Update(u);
                            _unitOfWork.Save();
                        }


                        //Saving Thumbnails images:
                        Dictionary <string, string> versions = new Dictionary <string, string>();

                        //Define the versions to generate
                        versions.Add("_thumb", "maxwidth=100&maxheight=100");  //Crop to square thumbnail
                        versions.Add("_medium", "maxwidth=200&maxheight=200"); //Fit inside 400x400 area, jpeg

                        string temp2    = "";
                        string filename = System.Guid.NewGuid().ToString();
                        foreach (string filekey in System.Web.HttpContext.Current.Request.Files.Keys)
                        {
                            HttpPostedFile pfile = System.Web.HttpContext.Current.Request.Files[filekey];
                            if (pfile.ContentLength <= 0)
                            {
                                continue;                           //Skip unused file controls.
                            }
                            temp2 = Path.GetExtension(pfile.FileName);

                            string uploadFolder = System.Web.HttpContext.Current.Request.MapPath("~/Uploads/" + uploadfolder);
                            if (!Directory.Exists(uploadFolder))
                            {
                                Directory.CreateDirectory(uploadFolder);
                            }

                            string filecontent = Path.Combine(uploadFolder, vm.ProductName + "_" + filename);

                            //pfile.SaveAs(filecontent);
                            ImageBuilder.Current.Build(new ImageJob(pfile, filecontent, new Instructions(), false, true));


                            //Generate each version
                            foreach (string suffix in versions.Keys)
                            {
                                if (suffix == "_thumb")
                                {
                                    string tuploadFolder = System.Web.HttpContext.Current.Request.MapPath("~/Uploads/" + uploadfolder + "/Thumbs");
                                    if (!Directory.Exists(tuploadFolder))
                                    {
                                        Directory.CreateDirectory(tuploadFolder);
                                    }

                                    //Generate a filename (GUIDs are best).
                                    string tfileName = Path.Combine(tuploadFolder, vm.ProductName + "_" + filename);

                                    //Let the image builder add the correct extension based on the output file type
                                    //fileName = ImageBuilder.Current.Build(file, fileName, new ResizeSettings(versions[suffix]), false, true);
                                    ImageBuilder.Current.Build(new ImageJob(pfile, tfileName, new Instructions(versions[suffix]), false, true));
                                }
                                else if (suffix == "_medium")
                                {
                                    string tuploadFolder = System.Web.HttpContext.Current.Request.MapPath("~/Uploads/" + uploadfolder + "/Medium");
                                    if (!Directory.Exists(tuploadFolder))
                                    {
                                        Directory.CreateDirectory(tuploadFolder);
                                    }

                                    //Generate a filename (GUIDs are best).
                                    string tfileName = Path.Combine(tuploadFolder, vm.ProductName + "_" + filename);

                                    //Let the image builder add the correct extension based on the output file type
                                    //fileName = ImageBuilder.Current.Build(file, fileName, new ResizeSettings(versions[suffix]), false, true);
                                    ImageBuilder.Current.Build(new ImageJob(pfile, tfileName, new Instructions(versions[suffix]), false, true));
                                }
                            }

                            //var tempsave = _FinishedProductService.Find(pt.ProductId);

                            pt.ImageFileName   = pt.ProductName + "_" + filename + temp2;
                            pt.ImageFolderName = uploadfolder;
                            pt.ObjectState     = Model.ObjectState.Modified;
                            _FinishedProductService.Update(pt);
                            _unitOfWork.Save();
                        }
                    }

                    #endregion



                    return(RedirectToAction("Create", new { id = vm.ProductTypeId, sample = vm.IsSample }).Success("Data saved successfully"));
                }
                else
                {
                    FinishedProduct temp = _FinishedProductService.Find(vm.ProductId);
                    temp.ProductName             = vm.ProductName;
                    temp.ProductCode             = vm.ProductCode;
                    temp.ProductGroupId          = vm.ProductGroupId;
                    temp.ProductCategoryId       = vm.ProductCategoryId;
                    temp.ProductCollectionId     = vm.ProductCollectionId;
                    temp.SampleId                = vm.SampleId;
                    temp.CounterNo               = vm.CounterNo;
                    temp.ProductQualityId        = vm.ProductQualityId;
                    temp.OriginCountryId         = vm.OriginCountryId;
                    temp.ProductDesignId         = vm.ProductDesignId;
                    temp.ProductInvoiceGroupId   = vm.ProductInvoiceGroupId;
                    temp.ProductDesignPatternId  = vm.ProductDesignPatternId;
                    temp.DrawBackTariffHeadId    = vm.DrawBackTariffHeadId;
                    temp.ProductStyleId          = vm.ProductStyleId;
                    temp.DescriptionOfGoodsId    = vm.DescriptionOfGoodsId;
                    temp.ColourId                = vm.ColourId;
                    temp.StandardCost            = vm.StandardCost;
                    temp.ProductManufacturerId   = vm.ProductManufacturerId;
                    temp.StandardWeight          = vm.StandardWeight;
                    temp.ProcessSequenceHeaderId = vm.ProcessSequenceHeaderId;
                    temp.CBM                  = vm.CBM;
                    temp.ContentId            = vm.ContentId;
                    temp.Tags                 = vm.Tags;
                    temp.FaceContentId        = vm.FaceContentId;
                    temp.ProductSpecification = vm.ProductSpecification;
                    temp.IsActive             = vm.IsActive;

                    temp.ModifiedDate = DateTime.Now;
                    temp.ModifiedBy   = User.Identity.Name;
                    temp.ObjectState  = Model.ObjectState.Modified;
                    _FinishedProductService.Update(temp);

                    if (!(vm.SizeId <= 0))
                    {
                        var         pstid = (int)ProductSizeTypeConstants.StandardSize;;
                        ProductSize ps    = new ProductSizeService(_unitOfWork).FindProductSize(pstid, vm.ProductId);
                        if (ps != null)
                        {
                            ps.SizeId       = vm.SizeId;
                            ps.ProductId    = vm.ProductId;
                            ps.ModifiedDate = DateTime.Now;
                            ps.ModifiedBy   = User.Identity.Name;
                            ps.ObjectState  = Model.ObjectState.Modified;
                            new ProductSizeService(_unitOfWork).Update(ps);
                        }
                        else
                        {
                            ProductSize pss  = new ProductSize();
                            var         psid = (int)ProductSizeTypeConstants.StandardSize;
                            pss.ProductId         = vm.ProductId;
                            pss.SizeId            = vm.SizeId;
                            pss.ProductSizeTypeId = psid;
                            pss.CreatedBy         = User.Identity.Name;
                            pss.ModifiedBy        = User.Identity.Name;
                            pss.CreatedDate       = DateTime.Now;
                            pss.ModifiedDate      = DateTime.Now;
                            new ProductSizeService(_unitOfWork).Create(pss);
                        }
                    }
                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        return(View("Create", vm));
                    }


                    #region

                    //Saving Image if file is uploaded
                    if (Request.Files[0] != null && Request.Files[0].ContentLength > 0)
                    {
                        string uploadfolder = temp.ImageFolderName;
                        string tempfilename = temp.ImageFileName;
                        if (uploadfolder == null)
                        {
                            var x = (from iid in db.Counter
                                     select iid).FirstOrDefault();
                            if (x == null)
                            {
                                uploadfolder = System.Guid.NewGuid().ToString();
                                Counter img = new Counter();
                                img.ImageFolderName = uploadfolder;
                                img.ModifiedBy      = User.Identity.Name;
                                img.CreatedBy       = User.Identity.Name;
                                img.ModifiedDate    = DateTime.Now;
                                img.CreatedDate     = DateTime.Now;
                                new CounterService(_unitOfWork).Create(img);
                                _unitOfWork.Save();
                            }
                            else
                            {
                                uploadfolder = x.ImageFolderName;
                            }
                        }
                        //Deleting Existing Images

                        var xtemp = System.Web.HttpContext.Current.Server.MapPath("~/Uploads/" + uploadfolder + "/" + tempfilename);
                        if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath("~/Uploads/" + uploadfolder + "/" + tempfilename)))
                        {
                            System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath("~/Uploads/" + uploadfolder + "/" + tempfilename));
                        }

                        //Deleting Thumbnail Image:

                        if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath("~/Uploads/" + uploadfolder + "/Thumbs/" + tempfilename)))
                        {
                            System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath("~/Uploads/" + uploadfolder + "/Thumbs/" + tempfilename));
                        }

                        //Deleting Medium Image:
                        if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath("~/Uploads/" + uploadfolder + "/Medium/" + tempfilename)))
                        {
                            System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath("~/Uploads/" + uploadfolder + "/Medium/" + tempfilename));
                        }

                        //Saving Thumbnails images:
                        Dictionary <string, string> versions = new Dictionary <string, string>();

                        //Define the versions to generate
                        versions.Add("_thumb", "maxwidth=100&maxheight=100");  //Crop to square thumbnail
                        versions.Add("_medium", "maxwidth=200&maxheight=200"); //Fit inside 400x400 area, jpeg

                        string temp2    = "";
                        string filename = System.Guid.NewGuid().ToString();
                        foreach (string filekey in System.Web.HttpContext.Current.Request.Files.Keys)
                        {
                            HttpPostedFile pfile = System.Web.HttpContext.Current.Request.Files[filekey];
                            if (pfile.ContentLength <= 0)
                            {
                                continue;                           //Skip unused file controls.
                            }
                            temp2 = Path.GetExtension(pfile.FileName);

                            string uploadFolder = System.Web.HttpContext.Current.Request.MapPath("~/Uploads/" + uploadfolder);
                            if (!Directory.Exists(uploadFolder))
                            {
                                Directory.CreateDirectory(uploadFolder);
                            }

                            string filecontent = Path.Combine(uploadFolder, temp.ProductName + "_" + filename);

                            //pfile.SaveAs(filecontent);

                            ImageBuilder.Current.Build(new ImageJob(pfile, filecontent, new Instructions(), false, true));

                            //Generate each version
                            foreach (string suffix in versions.Keys)
                            {
                                if (suffix == "_thumb")
                                {
                                    string tuploadFolder = System.Web.HttpContext.Current.Request.MapPath("~/Uploads/" + uploadfolder + "/Thumbs");
                                    if (!Directory.Exists(tuploadFolder))
                                    {
                                        Directory.CreateDirectory(tuploadFolder);
                                    }

                                    //Generate a filename (GUIDs are best).
                                    string tfileName = Path.Combine(tuploadFolder, temp.ProductName + "_" + filename);

                                    //Let the image builder add the correct extension based on the output file type
                                    //fileName = ImageBuilder.Current.Build(file, fileName, new ResizeSettings(versions[suffix]), false, true);
                                    ImageBuilder.Current.Build(new ImageJob(pfile, tfileName, new Instructions(versions[suffix]), false, true));
                                }
                                else if (suffix == "_medium")
                                {
                                    string tuploadFolder = System.Web.HttpContext.Current.Request.MapPath("~/Uploads/" + uploadfolder + "/Medium");
                                    if (!Directory.Exists(tuploadFolder))
                                    {
                                        Directory.CreateDirectory(tuploadFolder);
                                    }

                                    //Generate a filename (GUIDs are best).
                                    string tfileName = Path.Combine(tuploadFolder, temp.ProductName + "_" + filename);

                                    //Let the image builder add the correct extension based on the output file type
                                    //fileName = ImageBuilder.Current.Build(file, fileName, new ResizeSettings(versions[suffix]), false, true);
                                    ImageBuilder.Current.Build(new ImageJob(pfile, tfileName, new Instructions(versions[suffix]), false, true));
                                }
                            }
                        }
                        var temsave = _FinishedProductService.Find(temp.ProductId);
                        temsave.ImageFileName   = temsave.ProductName + "_" + filename + temp2;
                        temsave.ImageFolderName = uploadfolder;
                        _FinishedProductService.Update(temsave);
                        _unitOfWork.Save();
                    }

                    #endregion



                    return(RedirectToAction("Index", new { id = vm.ProductTypeId, sample = vm.IsSample }).Success("Data saved successfully"));
                }
            }
            return(View("Create", vm));
        }