partial void DeleteSS_Product(SS_Product instance);
 partial void UpdateSS_Product(SS_Product instance);
 partial void InsertSS_Product(SS_Product instance);
		private void detach_SS_Products(SS_Product entity)
		{
			this.SendPropertyChanging();
			entity.SS_Brand = null;
		}
		private void attach_SS_Products(SS_Product entity)
		{
			this.SendPropertyChanging();
			entity.SS_Retailer = this;
		}
        public static void DownloadProducts()
        {
            ShopStyle api = new ShopStyle("uid8409-24047347-36", ShopStyle.UK_API_HOSTNAME);

            ProductQuery query = new ProductQuery().withCategory("jackets"); // Category jackets
            query.withFilter("b870"); // Brand filter id
            query.withFilter("r274");// Retailer filter id

            var productResponse = api.getProducts(query);

            ProductSearchMetadata metadata = productResponse.getMetadata();
            int total = metadata.getTotal(); // pageCounter = total/limit, these three values are being used to calculate paging.
            int limit = metadata.getLimit();
            int offset = metadata.getOffset();

            foreach (var product in productResponse.getProducts())
            {
                using (CatalogDataContext dc = new CatalogDataContext())
                {
                    if(dc.SS_Products.FirstOrDefault(x => x.product_id == product.getId()) != null)
                    {
                        continue;
                    }
                }

                using(CatalogDataContext dc = new CatalogDataContext())
                {
                    SS_Product ssProduct = new SS_Product()
                    {
                        id = System.Guid.NewGuid(),
                        brand_id = (int?)product.getBrand().getId(),
                        click_url = product.getClickUrl(),
                        currency = product.getCurrency().getCurrencyCode(),
                        description = product.getDescription(),
                        image_id = product.getImage().getId(),
                        in_stock = product.isInStock(),
                        locale = product.getLocale().getDisplayCountry(),
                        name = product.getName(),
                        price = (decimal?)product.getPrice(),
                        price_label = product.getPriceLabel(),
                        retailer_id = (int?)product.getRetailer().getId(),
                        product_id = (int)product.getId(),
                        sale_price = (decimal?)product.getSalePrice() == 0 ? null : (decimal?)product.getSalePrice(),
                        sale_price_label = product.getSalePriceLabel()
                    };
                    dc.SS_Products.InsertOnSubmit(ssProduct);
                    dc.SubmitChanges();

                    foreach (ImageSize imageSize in product.getImage().getSizes().values().toArray())
                    {
                        using(CatalogDataContext dc1 = new CatalogDataContext())
                        {
                            SS_Image image = new SS_Image()
                                                 {
                                                     id = System.Guid.NewGuid(),
                                                     size_name = imageSize.getSizeName().toString(),
                                                     image_id = ssProduct.image_id,
                                                     url = imageSize.getUrl(),
                                                     height = imageSize.getHeight(),
                                                     width = imageSize.getWidth()
                                                 };
                            dc1.SS_Images.InsertOnSubmit(image);
                            dc1.SubmitChanges();

                        }
                    }

                }

                //Insert categories and product-category mapping records
                foreach (var category in product.getCategories())
                {
                    using(CatalogDataContext dc = new CatalogDataContext())
                    {
                        SS_Product ssProduct = dc.SS_Products.FirstOrDefault(x => x.product_id == product.getId());

                        if(ssProduct != null)
                        {
                            SS_Product_Category_Mapping pcMapping = new SS_Product_Category_Mapping() { id = System.Guid.NewGuid(), product_id = ssProduct.product_id, cat_id = category.getId() };
                            dc.SS_Product_Category_Mappings.InsertOnSubmit(pcMapping);
                            dc.SubmitChanges();
                        }
                        else
                        {
                            throw new ArgumentNullException("Could not find the newly inserted product");
                        }

                    }

                }

                //Isert product_color_image_mapping records
                foreach (var color in product.getColors())
                {
                     using(CatalogDataContext dc = new CatalogDataContext())
                     {
                         SS_Product_Color_Image_Mapping pciMapping = new SS_Product_Color_Image_Mapping()
                         {
                             id = System.Guid.NewGuid(),
                             color_name = color.getName(),
                             image_id = color.getImage().getId(),
                             product_id = (int)product.getId()
                         };

                         foreach (ImageSize imageSize in color.getImage().getSizes().values().toArray())
                         {
                             using (CatalogDataContext dc1 = new CatalogDataContext())
                             {
                                 if(dc1.SS_Images.FirstOrDefault(x =>x.image_id == pciMapping.image_id) == null)
                                 {
                                     SS_Image image = new SS_Image()
                                     {
                                         id = System.Guid.NewGuid(),
                                         size_name = imageSize.getSizeName().toString(),
                                         image_id = pciMapping.image_id,
                                         url = imageSize.getUrl(),
                                         height = imageSize.getHeight(),
                                         width = imageSize.getWidth()
                                     };
                                     dc1.SS_Images.InsertOnSubmit(image);
                                     dc1.SubmitChanges();
                                 }
                             }
                         }

                         dc.SS_Product_Color_Image_Mappings.InsertOnSubmit(pciMapping);
                         dc.SubmitChanges();
                     }

                }

                //Insert size and product_size_mapping
                foreach (var size in product.getSizes())
                {
                    using(CatalogDataContext dc = new CatalogDataContext())
                    {
                        SS_Size result = dc.SS_Sizes.FirstOrDefault(x => x.name == size.getName());

                        if (result == null)
                        {
                            System.Guid sizeId;
                            using (CatalogDataContext dc1 = new CatalogDataContext())
                            {
                                SS_Size s = new SS_Size() { id = System.Guid.NewGuid(), name = size.getName() };
                                dc1.SS_Sizes.InsertOnSubmit(s);
                                dc1.SubmitChanges();

                                sizeId = s.id;
                            }

                            using (CatalogDataContext dc2 = new CatalogDataContext())
                            {
                                SS_Product_Size_Mapping psMapping = new SS_Product_Size_Mapping() { id = System.Guid.NewGuid(), product_id = (int)product.getId(), size_id = sizeId };
                                dc2.SS_Product_Size_Mappings.InsertOnSubmit(psMapping);
                                dc2.SubmitChanges();
                            }

                        }
                        else
                        {
                            SS_Product_Size_Mapping psMapping = new SS_Product_Size_Mapping() { id = System.Guid.NewGuid(), product_id = (int)product.getId(), size_id = result.id };
                            dc.SS_Product_Size_Mappings.InsertOnSubmit(psMapping);
                            dc.SubmitChanges();
                        }
                    }

                }

            }
        }