public void AddProducts(ref Style_ViewModel model, ref Style style)
        {
            var products     = new List <Product>();
            var SKUStyleName = style.Name.Replace(" ", "");//replace spaces with nothing and then when listing products under a kit you don't see this name part cuz its already in the kit title

            //added for ordering sizes
            model.Sizes   = model.Sizes.Where(s => s.IsSelectedByStyle == true).ToList();
            model.Colors  = model.Colors.Where(s => s.IsSelectedByStyle == true).ToList();  //this won't work, they will have to be turned into integers if possible, sorted, and then turned back into strings?
            model.Genders = model.Genders.Where(s => s.IsSelectedByStyle == true).ToList(); //this won't work, they will have to be turned into integers if possible, sorted, and then turned back into strings?

            //edn added

            foreach (var size in model.Sizes.Where(c => c.IsSelectedByStyle == true))
            {
                foreach (var gender in model.Genders.Where(g => g.IsSelectedByStyle == true))
                {
                    foreach (var color in model.Colors.Where(s => s.IsSelectedByStyle == true))//switched sizes and colors here so size comes first in the ordering
                    {
                        products.Add(new Product
                        {
                            Name               = SKUStyleName + "-" + size.Name.ToString() + "-" + gender.Name.ToString() + "-" + color.Name.ToString(),//perhaps should also just save size, color and gender names here as well, or at least size name, for ordering on event details within kits
                            StyleId            = style.Id,
                            TotalQuantity      = 0,
                            AvailableQuantity  = 0,
                            CheckedOutQuantity = 0,
                            SizeId             = size.Id,
                            ColorId            = color.Id,
                            GenderId           = gender.Id,
                            SKU = ""//sku just set to nothing if you make or remake a style, these are only added in product edit later along with the total quantity
                        });
                    }
                }
            }
            _context.Product.AddRange(products);
        }
        public async Task <IActionResult> Create(Style_ViewModel model)
        {
            var style = new Style();

            style.IsActive      = model.IsActive;
            style.Name          = model.Name;
            style.ProductTypeId = model.ProductTypeId;
            //dont think i need the product type name here because it can pretty much be inferred from the style name, but maybe i will regret
            //that and want it later in which case see create post on statescontroller for how territory name is done

            if (ModelState.IsValid)
            {
                _context.Add(style);
                await _context.SaveChangesAsync();

                _styleService.AddStyleToColorGenderSizeEntries(ref model, ref style);
                await _context.SaveChangesAsync();

                _styleService.AddProducts(ref model, ref style);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(style));
        }
        // GET: Styles/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var style = await _context.Style
                        .SingleOrDefaultAsync(m => m.Id == id);

            if (style == null)
            {
                return(NotFound());
            }

            Style_ViewModel model = new Style_ViewModel();

            model.ProductTypeId   = style.ProductTypeId;
            model.ProductTypeName = _context.ProductType.FirstOrDefault(p => p.Id == style.ProductTypeId).Name;//null ref
            model.Name            = style.Name;

            model.Products = _context.Product.Where(p => p.StyleId == style.Id).ToList();
            model.Products = model.Products.OrderBy(p => p.SizeId).ToList();

            return(View(model));
        }
        public Style_ViewModel EditViewModel(Style style, int id)
        {
            var model = new Style_ViewModel();

            model.Id            = style.Id;//is this necessary?
            model.Name          = style.Name;
            model.IsActive      = style.IsActive;
            model.ProductTypeId = style.ProductTypeId;

            model.Genders = new List <Gender_ViewModel>();
            var genders = _context.Gender.Where(g => g.IsActive == true).ToList();

            foreach (var gender in genders)
            {
                var genderCurrentlySelected = _context.Style_Gender.FirstOrDefault(sg => sg.StyleId == id && sg.GenderId == gender.Id);
                var isSelected = genderCurrentlySelected == null ? false : true;

                model.Genders.Add(new Gender_ViewModel
                {
                    Name = gender.Name,
                    IsSelectedByStyle = isSelected,
                    Id = gender.Id
                });
            }

            model.Sizes = new List <Size_ViewModel>();
            var sizes = _context.Size.Where(g => g.IsActive == true).ToList();

            foreach (var size in sizes)
            {
                var sizeCurrentlySelected = _context.Style_Size.FirstOrDefault(ss => ss.StyleId == id && ss.SizeId == size.Id);
                var isSelected            = sizeCurrentlySelected == null ? false : true;

                model.Sizes.Add(new Size_ViewModel
                {
                    Name = size.Name,
                    IsSelectedByStyle = isSelected,
                    Id = size.Id
                });
            }

            model.Colors = new List <Color_ViewModel>();
            var colors = _context.Color.Where(g => g.IsActive == true).ToList();

            foreach (var color in colors)
            {
                var colorCurrentlySelected = _context.Style_Color.FirstOrDefault(sc => sc.StyleId == id && sc.ColorId == color.Id);
                var isSelected             = colorCurrentlySelected == null ? false : true;

                model.Colors.Add(new Color_ViewModel
                {
                    Name = color.Name,
                    IsSelectedByStyle = isSelected,
                    Id = color.Id
                });
            }
            return(model);
        }
        public Style_ViewModel CreateViewModel()
        {
            var model = new Style_ViewModel();

            model.Genders = new List <Gender_ViewModel>();
            var genders = _context.Gender.Where(g => g.IsActive == true).ToList();

            foreach (var gender in genders)
            {
                model.Genders.Add(new Gender_ViewModel
                {
                    Name = gender.Name,
                    IsSelectedByStyle = false,
                    Id = gender.Id
                });
            }

            model.Sizes = new List <Size_ViewModel>();
            var sizes = _context.Size.Where(g => g.IsActive == true).ToList();

            foreach (var size in sizes)
            {
                model.Sizes.Add(new Size_ViewModel
                {
                    Name = size.Name,
                    IsSelectedByStyle = false,
                    Id = size.Id
                });
            }

            model.Colors = new List <Color_ViewModel>();
            var colors = _context.Color.Where(g => g.IsActive == true).ToList();

            foreach (var color in colors)
            {
                model.Colors.Add(new Color_ViewModel
                {
                    Name = color.Name,
                    IsSelectedByStyle = false,
                    Id = color.Id
                });
            }

            return(model);
        }
        public void AddStyleToColorGenderSizeEntries(ref Style_ViewModel model, ref Style style)
        {
            bool ColorSelectionMade = false;

            foreach (var color in model.Colors)
            {
                if (color.IsSelectedByStyle)
                {
                    _context.Style_Color.Add(new Style_Color
                    {
                        ColorId = color.Id,
                        StyleId = style.Id
                    });
                    ColorSelectionMade = true;
                }
            }
            if (!ColorSelectionMade)
            {
                var defaultColor = model.Colors.FirstOrDefault(s => s.Name == "Colorless");
                if (defaultColor != null)
                {
                    model.Colors.FirstOrDefault(s => s.Name == "Colorless").IsSelectedByStyle = true;

                    _context.Style_Color.Add(new Style_Color
                    {
                        ColorId = defaultColor.Id,
                        StyleId = style.Id
                    });
                }
                else//if colorless doesn't even exist then create it and then add it to the new style_color entry
                {
                    _context.Color.Add(new Color
                    {
                        Name     = "Colorless",
                        IsActive = true,
                    });

                    //no race conditions with normal save changes
                    _context.SaveChanges();

                    var newColorEntry = _context.Color.FirstOrDefault(s => s.Name == "Colorless");

                    _context.Style_Color.Add(new Style_Color
                    {
                        ColorId = newColorEntry.Id,
                        StyleId = style.Id
                    });

                    model.Colors.Add(new Color_ViewModel
                    {
                        Name = newColorEntry.Name,
                        Id   = newColorEntry.Id,
                        IsSelectedByStyle = true
                    });
                }
            }
            //end first

            //begin second
            bool SizeSelectionMade = false;

            foreach (var size in model.Sizes)
            {
                if (size.IsSelectedByStyle)
                {
                    _context.Style_Size.Add(new Style_Size
                    {
                        SizeId  = size.Id,
                        StyleId = style.Id
                    });
                    SizeSelectionMade = true;
                }
            }
            if (!SizeSelectionMade)
            {
                var defaultSize = model.Sizes.FirstOrDefault(s => s.Name == "One Size Fits All");
                if (defaultSize != null)
                {
                    model.Sizes.FirstOrDefault(s => s.Name == "One Size Fits All").IsSelectedByStyle = true;

                    _context.Style_Size.Add(new Style_Size
                    {
                        SizeId  = defaultSize.Id,
                        StyleId = style.Id
                    });
                }
                else//if colorless doesn't even exist then create it and then add it to the new style_color entry
                {
                    _context.Size.Add(new Size
                    {
                        Name     = "One Size Fits All",
                        IsActive = true,
                    });

                    //no race conditions with normal save changes
                    _context.SaveChanges();

                    var newSizeEntry = _context.Size.FirstOrDefault(s => s.Name == "One Size Fits All");

                    _context.Style_Size.Add(new Style_Size
                    {
                        SizeId  = newSizeEntry.Id,
                        StyleId = style.Id
                    });

                    model.Sizes.Add(new Size_ViewModel
                    {
                        Name = newSizeEntry.Name,
                        Id   = newSizeEntry.Id,
                        IsSelectedByStyle = true
                    });
                }
            }
            //end second


            //begin third
            bool GenderSelectionMade = false;

            foreach (var gender in model.Genders)
            {
                if (gender.IsSelectedByStyle)
                {
                    _context.Style_Gender.Add(new Style_Gender
                    {
                        GenderId = gender.Id,
                        StyleId  = style.Id
                    });
                    GenderSelectionMade = true;
                }
            }
            if (!GenderSelectionMade)
            {
                var defaultGender = model.Genders.FirstOrDefault(s => s.Name == "Unisex");
                if (defaultGender != null)
                {
                    model.Genders.FirstOrDefault(s => s.Name == "Unisex").IsSelectedByStyle = true;

                    _context.Style_Gender.Add(new Style_Gender
                    {
                        GenderId = defaultGender.Id,
                        StyleId  = style.Id
                    });
                }
                else//if colorless doesn't even exist then create it and then add it to the new style_color entry
                {
                    _context.Gender.Add(new Gender
                    {
                        Name     = "Unisex",
                        IsActive = true,
                    });

                    //no race conditions with normal save changes
                    _context.SaveChanges();

                    var newGenderEntry = _context.Gender.FirstOrDefault(s => s.Name == "Unisex");

                    _context.Style_Gender.Add(new Style_Gender
                    {
                        GenderId = newGenderEntry.Id,
                        StyleId  = style.Id
                    });

                    model.Genders.Add(new Gender_ViewModel
                    {
                        Name = newGenderEntry.Name,
                        Id   = newGenderEntry.Id,
                        IsSelectedByStyle = true
                    });
                }
            }
            //end third
        }
        public async Task <IActionResult> Edit(int id, Style_ViewModel model)
        {
            if (id != model.Id)
            {
                return(NotFound());
            }

            var style = _context.Style.FirstOrDefault(s => s.Id == id);

            style.IsActive      = model.IsActive;
            style.Name          = model.Name;
            style.ProductTypeId = model.ProductTypeId;

            //begin pii
            var        currentProductEntries = _context.Product.Where(p => p.StyleId == id).ToList();//moved up here to find instance identifers to remove
            List <int> productIds            = new List <int>();

            foreach (var product in currentProductEntries)
            {
                productIds.Add(product.Id);
            }
            var instanceIdentifiers = _context.ProductInstanceIdentifier.Where(i => productIds.Contains((int)i.ProductId)).ToList();//AAAAAAAAAAAAAAAAAAAAAAAA

            _context.ProductInstanceIdentifier.RemoveRange(instanceIdentifiers);
            //end pii

            //added remove ProductInstances and ProductInstance_Customer entries here as well if a style is edited, yeah pretty much have to, it becomes inaccessible anyway
            //
            var productKits = _context.ProductKit.Where(m => m.StyleId == id).ToList();

            foreach (var productKit in productKits)
            {
                var        productInstances   = _context.ProductInstance.Where(i => i.ProductKitId == productKit.Id).ToList();
                List <int> productInstanceIds = new List <int>();
                foreach (var instance in productInstances)
                {
                    productInstanceIds.Add(instance.Id);
                }
                var customer_Instances = _context.ProductInstance_Customer.Where(pic => productInstanceIds.Contains(pic.ProductInstanceId)).ToList();

                //below and probably this whole section can be factored out and re-used a lot, these product instances could have changed as product kits got
                //edited? when a style is edited and all its products removed instances identifiers need to be removed by product id
                //should be deleting PII by product id and not instance id?
                //do product kit identifiers need to be removed? no editing a style does not get rid of kits only changes their products and deletes their instances
                _context.ProductInstance.RemoveRange(productInstances);
                _context.ProductInstance_Customer.RemoveRange(customer_Instances);
                //deleted the product kit removal here, don't need that anymore, want to keep the product kits they will just be empty and have no corresponsingd instances
                await _context.SaveChangesAsync();
            }
            //
            //end added

            if (ModelState.IsValid)
            {
                _context.Update(style);
                await _context.SaveChangesAsync();

                //remove old style_gender/color/size mapping entries:
                var currentColorStyleEntries = _context.Style_Color.Where(sc => sc.StyleId == id).ToList();//potential null ref risk if you start making styles without there being any colors? that probably won't happen though.
                _context.Style_Color.RemoveRange(currentColorStyleEntries);
                var currentGenderStyleEntries = _context.Style_Gender.Where(sc => sc.StyleId == id).ToList();
                _context.Style_Gender.RemoveRange(currentGenderStyleEntries);
                var currentSizeStyleEntries = _context.Style_Size.Where(sc => sc.StyleId == id).ToList();
                _context.Style_Size.RemoveRange(currentSizeStyleEntries);
                await _context.SaveChangesAsync();

                //add style_gender/color/size mapping entries:
                _styleService.AddStyleToColorGenderSizeEntries(ref model, ref style);
                await _context.SaveChangesAsync();

                //remove current products:
                //var currentProductEntries = _context.Product.Where(p => p.StyleId == id).ToList();
                _context.Product.RemoveRange(currentProductEntries);
                //add new products
                _styleService.AddProducts(ref model, ref style);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));

            #region old
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(style);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!StyleExists(style.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(style));

            #endregion
        }