SetFacility() public method

public SetFacility ( Airline airline, AirlinerFacility facility ) : void
airline TheAirline.Models.Airlines.Airline
facility AirlinerFacility
return void
        public static List<AirlinerClass> GetAirlinerClasses(AirlinerType type)
        {
            var classes = new List<AirlinerClass>();

            var passengerType = type as AirlinerPassengerType;
            if (passengerType != null)
            {
                Configuration airlinerTypeConfiguration =
                    Configurations.GetConfigurations(Configuration.ConfigurationType.AirlinerType)
                                  .Find(
                                      c =>
                                      ((AirlinerTypeConfiguration) c).Airliner == type
                                      && ((AirlinerTypeConfiguration) c).Period.From <= GameObject.GetInstance().GameTime
                                      && ((AirlinerTypeConfiguration) c).Period.To > GameObject.GetInstance().GameTime);

                if (airlinerTypeConfiguration == null)
                {
                    int seatingDiff = 0;
                    AirlinerConfiguration configuration = null;

                    int numOfClasses = Rnd.Next(0, passengerType.MaxAirlinerClasses) + 1;

                    if (GameObject.GetInstance().GameTime.Year >= (int) AirlinerClass.ClassType.BusinessClass)
                    {
                        if (numOfClasses == 1)
                        {
                            configuration = (AirlinerConfiguration) Configurations.GetStandardConfiguration("200");
                        }
                        if (numOfClasses == 2)
                        {
                            configuration = (AirlinerConfiguration) Configurations.GetStandardConfiguration("202");
                        }
                        if (numOfClasses == 3)
                        {
                            configuration = (AirlinerConfiguration) Configurations.GetStandardConfiguration("203");
                        }
                    }
                    else
                    {
                        if (numOfClasses == 1)
                        {
                            configuration = (AirlinerConfiguration) Configurations.GetStandardConfiguration("200");
                        }
                        if (numOfClasses == 2)
                        {
                            configuration = (AirlinerConfiguration) Configurations.GetStandardConfiguration("201");
                        }
                        if (numOfClasses == 3)
                        {
                            configuration = (AirlinerConfiguration) Configurations.GetStandardConfiguration("201");
                        }
                    }

                    if (configuration != null)
                    {
                        foreach (AirlinerClassConfiguration aClass in configuration.Classes)
                        {
                            var airlinerClass = new AirlinerClass(aClass.Type, aClass.SeatingCapacity) {RegularSeatingCapacity = aClass.RegularSeatingCapacity};

                            foreach (AirlinerFacility facility in aClass.GetFacilities())
                            {
                                airlinerClass.SetFacility(null, facility);
                            }

                            foreach (
                                AirlinerFacility.FacilityType fType in Enum.GetValues(typeof (AirlinerFacility.FacilityType))
                                )
                            {
                                if (!aClass.Facilities.Exists(f => f.Type == fType))
                                {
                                    airlinerClass.SetFacility(null, AirlinerFacilities.GetBasicFacility(fType));
                                }
                            }

                            airlinerClass.SeatingCapacity =
                                Convert.ToInt16(
                                    Convert.ToDouble(airlinerClass.RegularSeatingCapacity)
                                    /airlinerClass.GetFacility(AirlinerFacility.FacilityType.Seat).SeatUses);

                            classes.Add(airlinerClass);
                        }

                        seatingDiff = passengerType.MaxSeatingCapacity - configuration.MinimumSeats;
                    }

                    AirlinerClass economyClass = classes.Find(c => c.Type == AirlinerClass.ClassType.EconomyClass);
                    economyClass.RegularSeatingCapacity += seatingDiff;

                    AirlinerFacility seatingFacility = economyClass.GetFacility(AirlinerFacility.FacilityType.Seat);

                    var extraSeats = (int) (seatingDiff/seatingFacility.SeatUses);

                    economyClass.SeatingCapacity += extraSeats;
                }
                else
                {
                    foreach (
                        AirlinerClassConfiguration aClass in
                            ((AirlinerTypeConfiguration) airlinerTypeConfiguration).Classes)
                    {
                        var airlinerClass = new AirlinerClass(aClass.Type, aClass.SeatingCapacity) {RegularSeatingCapacity = aClass.RegularSeatingCapacity};

                        foreach (AirlinerFacility facility in aClass.GetFacilities())
                        {
                            airlinerClass.SetFacility(null, facility);
                        }

                        airlinerClass.SeatingCapacity =
                            Convert.ToInt16(
                                Convert.ToDouble(airlinerClass.RegularSeatingCapacity)
                                /airlinerClass.GetFacility(AirlinerFacility.FacilityType.Seat).SeatUses);

                        classes.Add(airlinerClass);
                    }
                }
            }
            else if (type is AirlinerCargoType)
            {
                var cargoClass = new AirlinerClass(AirlinerClass.ClassType.EconomyClass, 0);
                classes.Add(cargoClass);
            }
            else if (type.TypeAirliner == AirlinerType.TypeOfAirliner.Helicopter)
            {
                var helicopterClass = new AirlinerClass(AirlinerClass.ClassType.EconomyClass, 0);
                classes.Add(helicopterClass);
            }
            return classes;
        }
        private void orderAirliners(double discount = 0)
        {
            DateTime deliveryDate = this.dpDeliveryDate.SelectedDate.HasValue
                ? this.dpDeliveryDate.SelectedDate.Value
                : this.Orders.DeliveryDate;

            Guid id = Guid.NewGuid();

            foreach (AirlinerOrderMVVM order in this.Orders.Orders)
            {
                for (int i = 0; i < order.Amount; i++)
                {
                    var airliner = new Airliner(
                        id.ToString(),
                        order.Type,
                        GameObject.GetInstance().HumanAirline.Profile.Country.TailNumbers.GetNextTailNumber(),
                        deliveryDate);
                    Models.Airliners.Airliners.AddAirliner(airliner);

                    var pType = FleetAirliner.PurchasedType.Bought;
                    GameObject.GetInstance().HumanAirline.AddAirliner(pType, airliner, order.Homebase);

                    airliner.ClearAirlinerClasses();

                    foreach (AirlinerClass aClass in order.Classes)
                    {
                        var tClass = new AirlinerClass(aClass.Type, aClass.SeatingCapacity);
                        tClass.RegularSeatingCapacity = aClass.RegularSeatingCapacity;

                        foreach (AirlinerFacility facility in aClass.GetFacilities())
                        {
                            tClass.SetFacility(GameObject.GetInstance().HumanAirline, facility);
                        }

                        airliner.AddAirlinerClass(tClass);
                    }

                    airliner.EngineType = order.Engine;

                }
            }

            int totalAmount = this.Orders.Orders.Sum(o => o.Amount);
            double price = this.Orders.Orders.Sum(o => o.getOrderPrice());

            double totalPrice = price * ((1 - GeneralHelpers.GetAirlinerOrderDiscount(totalAmount)))
                                * ((100 - discount) / 100);

            AirlineHelpers.AddAirlineInvoice(
                GameObject.GetInstance().HumanAirline,
                GameObject.GetInstance().GameTime,
                Invoice.InvoiceType.Purchases,
                -totalPrice);
        }
        //orders a number of airliners for an airline
        public static void OrderAirliners(Airline airline, List<AirlinerOrder> orders, Airport airport, DateTime deliveryDate, double discount)
        {
            Guid id = Guid.NewGuid();

            foreach (AirlinerOrder order in orders)
            {
                for (int i = 0; i < order.Amount; i++)
                {
                    var airliner = new Airliner(id.ToString(), order.Type, airline.Profile.Country.TailNumbers.GetNextTailNumber(), deliveryDate);
                    Airliners.AddAirliner(airliner);

                    const FleetAirliner.PurchasedType pType = FleetAirliner.PurchasedType.Bought;
                    airline.AddAirliner(pType, airliner, airport);

                    airliner.ClearAirlinerClasses();

                    foreach (AirlinerClass aClass in order.Classes)
                    {
                        var tClass = new AirlinerClass(aClass.Type, aClass.SeatingCapacity) {RegularSeatingCapacity = aClass.RegularSeatingCapacity};

                        foreach (AirlinerFacility facility in aClass.GetFacilities())
                            tClass.SetFacility(airline, facility);

                        airliner.AddAirlinerClass(tClass);
                    }
                }
            }

            int totalAmount = orders.Sum(o => o.Amount);
            double price = orders.Sum(o => o.Type.Price*o.Amount);

            double totalPrice = price*((1 - GeneralHelpers.GetAirlinerOrderDiscount(totalAmount)))*((100 - discount)/100);

            AddAirlineInvoice(airline, GameObject.GetInstance().GameTime, Invoice.InvoiceType.Purchases, -totalPrice);
        }
        private void btnOrder_Click(object sender, RoutedEventArgs e)
        {
            Boolean hasHomebases = true;
            foreach (AirlinerOrderMVVM order in this.Orders.Orders)
            {
                if (order.Homebase == null)
                {
                    hasHomebases = false;
                }
            }
            Boolean contractedOrder = false;
            Boolean tryOrder = true;
             DateTime deliveryDate = this.dpDeliveryDate.SelectedDate.HasValue
                ? this.dpDeliveryDate.SelectedDate.Value
                : this.Orders.DeliveryDate;

            if (!hasHomebases)
            {
                WPFMessageBox.Show(
                    Translator.GetInstance().GetString("MessageBox", "2002"),
                    Translator.GetInstance().GetString("MessageBox", "2002", "message"),
                    WPFMessageBoxButtons.Ok);
            }
            else
            {
                if (GameObject.GetInstance().HumanAirline.Contract != null)
                {
                    if (GameObject.GetInstance().HumanAirline.Contract.Manufacturer == this.Manufacturer)
                    {
                        contractedOrder = true;
                    }
                    else
                    {
                        double terminationFee = GameObject.GetInstance().HumanAirline.Contract.GetTerminationFee();
                        WPFMessageBoxResult result =
                            WPFMessageBox.Show(
                                Translator.GetInstance().GetString("MessageBox", "2010"),
                                string.Format(
                                    Translator.GetInstance().GetString("MessageBox", "2010", "message"),
                                    GameObject.GetInstance().HumanAirline.Contract.Manufacturer.Name,
                                    terminationFee),
                                WPFMessageBoxButtons.YesNo);

                        if (result == WPFMessageBoxResult.Yes)
                        {
                            AirlineHelpers.AddAirlineInvoice(
                                GameObject.GetInstance().HumanAirline,
                                GameObject.GetInstance().GameTime,
                                Invoice.InvoiceType.Purchases,
                                -terminationFee);
                            GameObject.GetInstance().HumanAirline.Contract = null;

                            this.Contract.Contracted = null;
                        }
                        tryOrder = result == WPFMessageBoxResult.Yes;
                    }
                }

                if (tryOrder)
                {
                    int totalAmount = this.Orders.Orders.Sum(o => o.Amount);
                    double price = this.Orders.Orders.Sum(o => o.getOrderPrice());

                    double totalPrice = price * ((1 - GeneralHelpers.GetAirlinerOrderDiscount(totalAmount)));

                    if (contractedOrder)
                    {
                        totalPrice = totalPrice
                                     * ((100 - GameObject.GetInstance().HumanAirline.Contract.Discount) / 100);
                    }

                    double downpaymentPrice = 0;

                    downpaymentPrice = totalPrice * (GameObject.GetInstance().Difficulty.PriceLevel / 10);

                    if (this.cbDownPayment.IsChecked.Value)
                    {
                        if (downpaymentPrice > GameObject.GetInstance().HumanAirline.Money)
                        {
                            WPFMessageBox.Show(
                                Translator.GetInstance().GetString("MessageBox", "2001"),
                                Translator.GetInstance().GetString("MessageBox", "2001", "message"),
                                WPFMessageBoxButtons.Ok);
                        }
                        else
                        {
                            WPFMessageBoxResult result =
                                WPFMessageBox.Show(
                                    Translator.GetInstance().GetString("MessageBox", "2009"),
                                    string.Format(
                                        Translator.GetInstance().GetString("MessageBox", "2009", "message"),
                                        totalPrice,
                                        downpaymentPrice),
                                    WPFMessageBoxButtons.YesNo);

                            if (result == WPFMessageBoxResult.Yes)
                            {
                                foreach (AirlinerOrderMVVM order in this.Orders.Orders)
                                {
                                    for (int i = 0; i < order.Amount; i++)
                                    {
                                        Guid id = Guid.NewGuid();

                                        var airliner = new Airliner(
                                            id.ToString(),
                                            order.Type,
                                            GameObject.GetInstance()
                                                .HumanAirline.Profile.Country.TailNumbers.GetNextTailNumber(),
                                            deliveryDate);

                                        airliner.ClearAirlinerClasses();

                                        foreach (AirlinerClass aClass in order.Classes)
                                        {
                                            var tClass = new AirlinerClass(aClass.Type, aClass.SeatingCapacity);
                                            tClass.RegularSeatingCapacity = aClass.RegularSeatingCapacity;

                                            foreach (AirlinerFacility facility in aClass.GetFacilities())
                                            {
                                                tClass.SetFacility(GameObject.GetInstance().HumanAirline, facility);
                                            }

                                            airliner.AddAirlinerClass(tClass);
                                        }

                                        airliner.EngineType = order.Engine;

                                        Models.Airliners.Airliners.AddAirliner(airliner);

                                        var pType = FleetAirliner.PurchasedType.BoughtDownPayment;
                                        GameObject.GetInstance()
                                            .HumanAirline.AddAirliner(pType, airliner, order.Homebase);

                                    }
                                }
                                if (contractedOrder)
                                {
                                    GameObject.GetInstance().HumanAirline.Contract.PurchasedAirliners +=
                                        this.Orders.Orders.Sum(o => o.Amount);
                                }

                                AirlineHelpers.AddAirlineInvoice(
                                    GameObject.GetInstance().HumanAirline,
                                    GameObject.GetInstance().GameTime,
                                    Invoice.InvoiceType.Purchases,
                                    -downpaymentPrice);

                                var tab_main = UIHelpers.FindChild<TabControl>(this.Tag as Page, "tabMenu");

                                if (tab_main != null)
                                {
                                    TabItem matchingItem =
                                        tab_main.Items.Cast<TabItem>()
                                            .Where(item => item.Tag.ToString() == "Order")
                                            .FirstOrDefault();

                                    tab_main.SelectedItem = matchingItem;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (totalPrice > GameObject.GetInstance().HumanAirline.Money)
                        {
                            WPFMessageBox.Show(
                                Translator.GetInstance().GetString("MessageBox", "2001"),
                                Translator.GetInstance().GetString("MessageBox", "2001", "message"),
                                WPFMessageBoxButtons.Ok);
                        }
                        else
                        {
                            if (this.Orders.Orders.Sum(o => o.Amount) > 0)
                            {
                                WPFMessageBoxResult result =
                                    WPFMessageBox.Show(
                                        Translator.GetInstance().GetString("MessageBox", "2008"),
                                        string.Format(
                                            Translator.GetInstance().GetString("MessageBox", "2008", "message"),
                                            totalPrice),
                                        WPFMessageBoxButtons.YesNo);

                                if (result == WPFMessageBoxResult.Yes)
                                {
                                    this.orderAirliners(
                                        contractedOrder ? GameObject.GetInstance().HumanAirline.Contract.Discount : 0);

                                    var tab_main = UIHelpers.FindChild<TabControl>(this.Tag as Page, "tabMenu");

                                    if (tab_main != null)
                                    {
                                        TabItem matchingItem =
                                            tab_main.Items.Cast<TabItem>()
                                                .Where(item => item.Tag.ToString() == "Order")
                                                .FirstOrDefault();

                                        tab_main.SelectedItem = matchingItem;
                                    }
                                }

                                if (contractedOrder)
                                {
                                    GameObject.GetInstance().HumanAirline.Contract.PurchasedAirliners +=
                                        this.Orders.Orders.Sum(o => o.Amount);
                                }
                            }
                        }
                    }
                }
            }
        }