private void ShowAddNewVendorNode(VendorManagerLeaf serviceNode)
        {
            var service = serviceNode.LeafeObject as IntegrationServiceType;
            var clickToAddNewVendorNode =
                new VendorManagerLeaf
            {
                LeafText     = $"Add new vendor to ({service.Name}) service",
                LeafeObject  = new IntegrationVendor(),
                ForeColor    = Color.Blue,
                LeafImageKey = "AddVendor.png",
                Tooltip      = $"Add new vendor to {service.Name} [IntegrationVendor]"
            };

            serviceNode.Children.Add(clickToAddNewVendorNode);
        }
        private void ShowAddNewServiceNode()
        {
            var clickToAddNewServiceNode = new VendorManagerLeaf
            {
                LeafText    = $"Add new service",
                LeafeObject = new IntegrationServiceType {
                    IntegrationServiceTypeId = 0
                },
                ForeColor    = Color.Black,
                LeafImageKey = "AddService.png",
                Tooltip      = $"Add new service to iMP [IntegrationServiceTypeId]"
            };

            Services.Add(clickToAddNewServiceNode);
        }
        private void ShowAddNewVendorProductNode(VendorManagerLeaf vendorNode)
        {
            var vendor         = vendorNode.LeafeObject as IntegrationVendor;
            var addproductNode =
                new VendorManagerLeaf
            {
                LeafText =
                    $"Add new product to ({vendor.Name}) vendor",     //{(vendorNode.Parent.Tag as IntegrationServiceType).Name}
                LeafeObject  = new IntegrationVendorProduct(),
                ForeColor    = Color.Green,
                LeafImageKey = "AddProduct.png",
                Tooltip      = $"Add new vendor product to {vendor.Name} [IntegrationVendorProduct]"
            };

            vendorNode.Children.Add(addproductNode);
        }
 private void AppendServices()
 {
     Services = new List <VendorManagerLeaf>();
     foreach (var service in AllServices)
     {
         var newServiceNode =
             new VendorManagerLeaf
         {
             LeafText               = $"{service.IntegrationServiceTypeId}-{service.Name}",
             LeafeObject            = service,
             IntegrationServiceType =
                 service,     //To avoid reflection of type casting if you already know the type of the leaf
             Tooltip =
                 $"IntegrationServiceTypeId = {service.IntegrationServiceTypeId} is {service.Descr} [IntegrationServiceType]",
             LeafImageKey = "blank.png"
         };
         Services.Add(newServiceNode);
     }
     ShowAddNewServiceNode();
 }
        private void AppendVendors(VendorManagerLeaf serviceNode)
        {
            var currentService = serviceNode.IntegrationServiceType;

            if (currentService == null || currentService.IntegrationServiceTypeId == 0)
            {
                return;
            }

            var relatedProducts =
                AllProducts.Where(x => x.IntegrationServiceTypeId == currentService.IntegrationServiceTypeId)
                .Select(x => x.IntegrationProductId).ToList();
            var relatedVendorProducts = AllVendorProducts
                                        .Where(x => relatedProducts.Contains(x.IntegrationProductId.Value))
                                        .ToList();

            var thisServiceVendors = AllVendors.Where(p =>
                                                      relatedVendorProducts.Select(x => x.IntegrationVendorId).Contains(p.IntegrationVendorId));

            foreach (var vendor in thisServiceVendors) //add vendors
            {
                var vendorNode =
                    new VendorManagerLeaf

                {
                    LeafText          = $"{vendor.IntegrationVendorId}-{vendor.Name}",
                    LeafeObject       = vendor,
                    IntegrationVendor =
                        vendor,     //to avoid type casting in case you already know the type of the leaf
                    ForeColor    = Color.Blue,
                    LeafImageKey = "V.jpg",
                    Tooltip      =
                        $"IntegrationVendorId = {vendor.IntegrationVendorId} is {vendor.Descr} using Scheme {vendor.SchemaType} [IntegrationVendor]"
                };

                serviceNode.Children.Add(vendorNode);
                ShowAddNewVendorProductNode(vendorNode);
            }

            ShowAddNewVendorNode(serviceNode);
        }
        private void AddVendorProducts()
        {
            foreach (var serviceNode in Services)
            {
                foreach (var vendorNode in serviceNode.Children)
                {
                    //Get products for this vendor and insert them above Add new product node
                    var vendor = vendorNode.IntegrationVendor;
                    if (vendor == null)
                    {
                        continue;
                    }
                    var thisVendorProducts = AllVendorProducts
                                             .Where(x => x.IntegrationVendorId == vendor.IntegrationVendorId).ToList();

                    foreach (var vendorProduct in thisVendorProducts) //Add products
                    {
                        var productNode =
                            new VendorManagerLeaf
                        {
                            LeafText =
                                $"{vendorProduct.IntegrationProductId}-{AllProducts.First(p => p.IntegrationProductId == vendorProduct.IntegrationProductId).Name}",
                            LeafeObject = vendorProduct,
                            IntegrationVendorProduct =
                                vendorProduct, //to avoid type casting in case you already know the type of the leaf
                            IntegrationProduct = AllProducts.FirstOrDefault(x => x.IntegrationProductId == vendorProduct.IntegrationProductId),
                            LeafImageKey       = "P.png",
                            Tooltip            =
                                $"IntegrationVendorProductId = {vendorProduct.IntegrationVendorProductId} has IntegrationHelperFQN of {vendorProduct.IntegrationHelperFQN} [IntegrationVendorProduct]"
                        };
                        vendorNode.Children.Insert(0,
                                                   productNode); //Vendor --> Product, ex. Appraisal -> West VM -> Appraisal
                    }
                }
            }
        }
        private void AddVendorSettings()
        {
            var vendorSource       = AllSources.FirstOrDefault(x => x.Name == "INTEGRATION_VENDOR");
            var vendorSourceFields = AllSourceFields.Where(x =>
                                                           x.IntegrationSourceObjectTypeId == vendorSource.IntegrationSourceObjectTypeId).ToList();

            var vendorSettings = AllSettings.Where(x =>
                                                   vendorSourceFields.Select(p => p.IntegrationSettingTypeSourceObjectId)
                                                   .Contains(x.IntegrationSettingTypeSourceObjectId.Value)).ToList(); //This is the trickest part because SettingSourceObjectId is a varchar and it could mean a vendor/Product/Environment/Credential etc...


            foreach (var serviceNode in Services)
            {
                foreach (var vendorNode in serviceNode.Children)
                {
                    var thisVendor = vendorNode.IntegrationVendor;

                    if (thisVendor == null || thisVendor.IntegrationVendorId == 0)
                    {
                        continue;
                    }
                    //if (serviceNode.IntegrationServiceType.IntegrationServiceTypeId == 20) Debugger.Break();


                    //Now get this vendor settings - Many settings
                    var thisVendorSettings = vendorSettings.Where(x =>
                                                                  x.SettingSourceObjectId ==
                                                                  thisVendor.IntegrationVendorId
                                                                  .ToString()); //This is the trickest part because SettingSourceObjectId is a varchar and it could mean a vendor/Product/Environment/Credential etc...

                    //Now we will go back and get only SourceFields for this vendor - Many fields
                    //Many fields
                    var thisVendorSourceFields = AllSourceFields.Where(x =>
                                                                       thisVendorSettings.Select(p => p.IntegrationSettingTypeSourceObjectId)
                                                                       .Contains(x.IntegrationSettingTypeSourceObjectId)).ToList();

                    //Actual fields so we can display the names
                    var fields = AllFieldTypes.Where(x =>
                                                     thisVendorSourceFields.Select(p => p.IntegrationSettingTypeId).Contains(x.IntegrationSettingTypeId)).ToList();

                    //First add a settings Node to host all settings
                    var settingsNode =
                        new VendorManagerLeaf
                    {
                        LeafText    = $"Settings [{fields.Count()}]",
                        LeafeObject = new IntegrationSourceObjectType {
                            Name = "INTEGRATION_VENDOR"
                        },
                        ForeColor    = Color.Blue,
                        LeafImageKey = "SettingsVendor.png",
                        Tooltip      = $"Settings for vendor {thisVendor.Name} [IntegrationSourceObjectType]"
                    };


                    foreach (var field in fields)
                    {
                        var vendorSourceField = thisVendorSourceFields.FirstOrDefault(x => x.IntegrationSettingTypeId == field.IntegrationSettingTypeId); //get this field
                        var setting           = AllSettings.FirstOrDefault(x =>
                                                                           x.SettingSourceObjectId == thisVendor.IntegrationVendorId.ToString() &&
                                                                           x.IntegrationSettingTypeSourceObjectId ==
                                                                           vendorSourceField.IntegrationSettingTypeSourceObjectId);

                        var actualSettingNode =
                            new VendorManagerLeaf
                        {
                            LeafText               = $"{field.IntegrationSettingTypeId}-{field.Name}",
                            LeafeObject            = field,
                            IntegrationSettingType =
                                field,     //to avoid type casting in case you already know the type of the leaf,
                            IntegrationSetting = setting,
                            ForeColor          = Color.Blue,
                            BackColor          = Color.Snow,
                            LeafImageKey       = "SettingsVendor.png",
                            Tooltip            =
                                $"IntegrationSettingTypeId = {field.IntegrationSettingTypeId} is {field.Descr} of type {field.DataEntryType} and has a default value of {field.DefatulValue} [IntegrationSettingType]"
                        };

                        settingsNode.Children.Add(actualSettingNode);
                    }


                    var addnewVendorSetting =
                        new VendorManagerLeaf
                    {
                        LeafText     = $"Add new vendor setting to ({thisVendor.Name}) vendor",
                        LeafeObject  = new IntegrationSetting(),
                        ForeColor    = Color.Blue,
                        BackColor    = Color.Snow,
                        LeafImageKey = "AddVendor.png",
                        Tooltip      = $"Add new vendor setting to {thisVendor.Name} [IntegrationSetting]"
                    };     //Add product setting this should be a settings object with some ids added to it
                    settingsNode.Children.Add(addnewVendorSetting);

                    vendorNode.Children.Add(settingsNode);
                }
            }
        }
        private void AddVendorProductSettings()
        {
            var vendorProductSource = AllSources.FirstOrDefault(x => x.Name == "INTEGRATION_VENDOR_PRODUCT");
            var vpFields            = AllSourceFields.Where(x =>
                                                            x.IntegrationSourceObjectTypeId == vendorProductSource.IntegrationSourceObjectTypeId).ToList();

            var vpSettings = AllSettings.Where(x =>
                                               vpFields.Select(p => p.IntegrationSettingTypeSourceObjectId)
                                               .Contains(x.IntegrationSettingTypeSourceObjectId.Value)).ToList(); //This is the trickest part because SettingSourceObjectId is a varchar and it could mean a vendor/Product/Environment/Credential etc...
            //Actual fields so we can display the names
            var vendorProductFields = AllFieldTypes.Where(x =>
                                                          vpFields.Select(p => p.IntegrationSettingTypeId).Contains(x.IntegrationSettingTypeId)).ToList();

            foreach (var serviceNode in Services)
            {
                foreach (var vendorNode in serviceNode.Children)
                {
                    foreach (var vendorProductNode in vendorNode.Children)
                    {
                        var thisVendorProduct = vendorProductNode.IntegrationVendorProduct;
                        if (thisVendorProduct == null || thisVendorProduct.IntegrationVendorProductId == 0)
                        {
                            continue;
                        }


                        var thisvpSettings = vpSettings.Where(x =>
                                                              x.SettingSourceObjectId == vendorProductNode.IntegrationVendorProduct.IntegrationVendorProductId.ToString());
                        //Now we will go back and get only SourceFields for this vendor
                        var thisVendorSourceFields = vpFields.Where(x =>
                                                                    thisvpSettings.Select(p => p.IntegrationSettingTypeSourceObjectId)
                                                                    .Contains(x.IntegrationSettingTypeSourceObjectId)).ToList();

                        //Actual fields so we can display the names
                        var fields = vendorProductFields.Where(x =>
                                                               thisVendorSourceFields.Select(p => p.IntegrationSettingTypeId).Contains(x.IntegrationSettingTypeId)).ToList();

                        //First add a settings Node to host all settings
                        var settingsNode =
                            new VendorManagerLeaf
                        {
                            LeafText    = $"Settings [{fields.Count}]",
                            LeafeObject = new IntegrationSourceObjectType {
                                Name = "INTEGRATION_VENDOR_PRODUCT"
                            },
                            ForeColor    = Color.Green,
                            LeafImageKey = "SettingsProduct.png",
                            Tooltip      =
                                $"Settings for vendor {AllVendors.First(x => x.IntegrationVendorId == thisVendorProduct.IntegrationVendorId).Name} for the product {AllProducts.First(x => x.IntegrationProductId == thisVendorProduct.IntegrationProductId).Name} [IntegrationSourceObjectType]"
                        }; //Add product setting this should be a settings object with some ids added to it


                        foreach (var field in fields)
                        {
                            var vendorSourceField = thisVendorSourceFields.FirstOrDefault(x => x.IntegrationSettingTypeId == field.IntegrationSettingTypeId);  //get this field
                            var setting           = AllSettings.FirstOrDefault(x =>
                                                                               x.SettingSourceObjectId == thisVendorProduct.IntegrationVendorProductId.ToString() &&
                                                                               x.IntegrationSettingTypeSourceObjectId ==
                                                                               vendorSourceField.IntegrationSettingTypeSourceObjectId);

                            var actualSettingNode =
                                new VendorManagerLeaf
                            {
                                LeafText               = $"{field.IntegrationSettingTypeId}-{field.Name}",
                                LeafeObject            = field,
                                IntegrationSetting     = setting, // add the correct integration setting here so you can edit it easier in the future
                                IntegrationSettingType =
                                    field,                        //to avoid type casting in case you already know the type of the field
                                ForeColor    = Color.Green,
                                BackColor    = Color.Snow,
                                LeafImageKey = "SettingsProduct.png",
                                Tooltip      =
                                    $"IntegrationSettingTypeId = {field.IntegrationSettingTypeId} is {field.Descr} of type {field.DataEntryType} and has a default value of {field.DefatulValue} [IntegrationSettingType]"
                            };

                            settingsNode.Children.Add(actualSettingNode);
                        }


                        var addnewVendorSetting =
                            new VendorManagerLeaf
                        {
                            LeafText =
                                $"Add new vendor product setting to ({AllVendors.First(x => x.IntegrationVendorId == thisVendorProduct.IntegrationVendorId).Name}) vendor -  ({AllProducts.First(x => x.IntegrationProductId == thisVendorProduct.IntegrationProductId).Name}) product",
                            LeafeObject  = new IntegrationSetting(),
                            ForeColor    = Color.Green,
                            BackColor    = Color.Snow,
                            LeafImageKey = "AddProduct.png",
                            Tooltip      =
                                $"Add new setting {AllVendors.First(x => x.IntegrationVendorId == thisVendorProduct.IntegrationVendorId).Name} for the product {AllProducts.First(x => x.IntegrationProductId == thisVendorProduct.IntegrationProductId).Name} [IntegrationSetting]"
                        }; //Add product setting this should be a settings object with some ids added to it
                        settingsNode.Children.Add(addnewVendorSetting);

                        vendorProductNode.Children.Add(settingsNode);
                        OnNotification(new NotificationEventAgrs {
                            Message = $"Vendor Product Settings modeled for {vendorNode.IntegrationVendor.Name}/{AllProducts.First(x=> x.IntegrationProductId == vendorProductNode.IntegrationVendorProduct.IntegrationProductId).Name} {fields.Count} Settings added"
                        });
                    }
                }
            }
        }