public static void QueryMechAssemblyPopup(SimGameState s, MechDef d, Action onClose = null)
        {
            if (GetNumPartsForAssembly(s, d) < s.Constants.Story.DefaultMechPartMax)
            {
                onClose?.Invoke();
                return;
            }
            IEnumerable <MechDef> mechs = GetAllAssemblyVariants(s, d);
            string desc             = $"Yang: Concerning the [[DM.MechDefs[{d.Description.Id}],{d.Chassis.Description.UIName} {d.Chassis.VariantName}]]: {d.Chassis.YangsThoughts}\n\n We have Parts of the following {d.GetMechOmniVehicle()} variants. What should I build?\n";
            GenericPopupBuilder pop = GenericPopupBuilder.Create($"Assemble {d.GetMechOmniVehicle()}?", desc);

            pop.AddButton("-", delegate
            {
                onClose?.Invoke();
            }, true, null);
            foreach (MechDef m in mechs)
            {
                MechDef var   = m; // new var to keep it for lambda
                int     count = s.GetItemCount(var.Description.Id, "MECHPART", SimGameState.ItemCountType.UNDAMAGED_ONLY);
                int     com   = GetNumberOfMechsOwnedOfType(s, m);
                int     cost  = m.GetMechSellCost(s);
                if (count <= 0 && !CheckOmniKnown(s, d, m))
                {
                    if (Settings.ShowAllVariantsInPopup)
                    {
                        pop.Body += $"no parts: [[DM.MechDefs[{m.Description.Id}],{m.Chassis.Description.UIName} {m.Chassis.VariantName}]] ({com} Complete)\n";
                    }
                    continue;
                }
                if (GetNumPartsForAssembly(s, var) >= s.Constants.Story.DefaultMechPartMax)
                {
                    pop.AddButton(string.Format("{0}", var.Chassis.VariantName), delegate
                    {
                        PerformMechAssemblyStorePopup(s, var, onClose);
                    }, true, null);
                }
                pop.Body += $"[[DM.MechDefs[{m.Description.Id}],{m.Chassis.Description.UIName} {m.Chassis.VariantName}]] ({count} Parts/{com} Complete) ({SimGameState.GetCBillString(cost)})\n";
            }
            pop.AddFader(new UIColorRef?(LazySingletonBehavior <UIManager> .Instance.UILookAndColorConstants.PopupBackfill), 0f, true);
            pop.Render();
        }
        public static string GetAssembleNotEnoughPartsText(SimGameState s, MechDef d)
        {
            IEnumerable <MechDef> mechs = GetAllAssemblyVariants(s, d);
            string desc = $"Yang: I do not have enough parts to assemble a {d.GetMechOmniVehicle()} out of it.";

            if (!Settings.ShowAllVariantsInPopup)
            {
                return(desc);
            }
            desc += "\nKeep your eyes open for the following Variants:\n";
            foreach (MechDef m in mechs)
            {
                int count = s.GetItemCount(m.Description.Id, "MECHPART", SimGameState.ItemCountType.UNDAMAGED_ONLY);
                int com   = GetNumberOfMechsOwnedOfType(s, m);
                if (count <= 0 && !CheckOmniKnown(s, d, m))
                {
                    desc += $"no parts: [[DM.MechDefs[{m.Description.Id}],{m.Chassis.Description.UIName} {m.Chassis.VariantName}]] ({com} Complete)\n";
                    continue;
                }
                desc += $"[[DM.MechDefs[{m.Description.Id}],{m.Chassis.Description.UIName} {m.Chassis.VariantName}]] ({count} Parts/{com} Complete)\n";
            }
            return(desc);
        }
        public static void PerformMechAssemblyStorePopup(SimGameState s, MechDef d, Action onClose)
        {
            WwiseManager.PostEvent(AudioEventList_ui.ui_sim_popup_newChassis, WwiseManager.GlobalAudioObject, null, null);
            MechDef toAdd = PerformMechAssembly(s, d);
            int     mechbay;

            if (toAdd.IsVehicle())
            {
                mechbay = CUIntegration.GetFirstFreeMechBay(s, d); // vehicle bay, +100 or something similar
            }
            else
            {
                mechbay = s.GetFirstFreeMechBay();
            }
            GenericPopupBuilder pop = GenericPopupBuilder.Create($"{d.GetMechOmniVehicle()} Assembled", $"Yang: [[DM.MechDefs[{d.Description.Id}],{d.Chassis.Description.UIName} {d.Chassis.VariantName}]] finished!\n{d.Chassis.YangsThoughts}\n\n");

            pop.AddButton("storage", delegate
            {
                StoreMech(s, toAdd);
                CallMessages(s, toAdd);
                Log.Log("direct storage");
                onClose?.Invoke();
            }, true, null);
            if (mechbay < 0) // no space - direct storage
            {
                pop.Body += $"We have no space for a new {d.GetMechOmniVehicle()}, so it goes into storage.";
            }
            else
            {
                pop.Body += "Should I put it into storage or ready it for combat?";
                pop.AddButton("ready it", delegate
                {
                    if (Settings.AssembledMechsNeedReadying)
                    {
                        ReadyMech(s, toAdd, mechbay);
                        CallMessages(s, toAdd);
                    }
                    else
                    {
                        s.AddMech(mechbay, toAdd, true, false, false);
                        CallMessages(s, toAdd);
                    }
                    Log.Log("added to bay " + mechbay);
                    onClose?.Invoke();
                }, true, null);
            }
            if (s.IsSellingAllowed())
            {
                int cost = toAdd.GetMechSellCost(s);
                pop.Body += $"\n\nDarius: We could also sell it for {SimGameState.GetCBillString(cost)}, although Yang would certanly not like it.";
                pop.AddButton("sell it", delegate
                {
                    s.AddFunds(cost, "Store", true, true);
                    Log.Log("sold for " + cost);
                    s.CompanyStats.ModifyStat("Mission", 0, "COMPANY_MechsAdded", StatCollection.StatOperation.Int_Add, 1, -1, true);
                    CallMessages(s, toAdd);
                    onClose?.Invoke();
                }, true, null);
            }
            pop.AddFader(new UIColorRef?(LazySingletonBehavior <UIManager> .Instance.UILookAndColorConstants.PopupBackfill), 0f, true);
            pop.Render();
        }
        public static void ReadyMech(SimGameState s, MechDef d, int baySlot, bool donotoverridetime = false)
        {
            int mechReadyTime = s.Constants.Story.MechReadyTime;

            if (Settings.AssembledMechsReadyingFlatCost > 0 && !donotoverridetime)
            {
                mechReadyTime = Settings.AssembledMechsReadyingFlatCost + Settings.AssembledMechsReadyingPerNonFixedComponentCost * d.Inventory.Where((a) => !a.IsFixed).Count();
            }
            WorkOrderEntry_ReadyMech workOrderEntry_ReadyMech = new WorkOrderEntry_ReadyMech(string.Format("ReadyMech-{0}", d.GUID), $"Readying {d.GetMechOmniVehicle()} - {d.Chassis.Description.Name}",
                                                                                             mechReadyTime, baySlot, d, string.Format(s.Constants.Story.MechReadiedWorkOrderCompletedText, new object[]
            {
                d.Chassis.Description.Name
            }));

            s.MechLabQueue.Add(workOrderEntry_ReadyMech);
            s.ReadyingMechs[baySlot] = d;
            s.RoomManager.AddWorkQueueEntry(workOrderEntry_ReadyMech);
            s.UpdateMechLabWorkQueue(false);
            AudioEventManager.PlayAudioEvent("audioeventdef_simgame_vo_barks", "workqueue_readymech", WwiseManager.GlobalAudioObject, null);
            s.CompanyStats.ModifyStat("Mission", 0, "COMPANY_MechsAdded", StatCollection.StatOperation.Int_Add, 1, -1, true);
        }
        public static bool Prefix(MechBayChassisInfoWidget __instance, ChassisDef ___selectedChassis, MechBayPanel ___mechBay)
        {
            if (___selectedChassis == null)
            {
                return(true);
            }
            int     bay = ___mechBay.Sim.GetFirstFreeMechBay();
            MechDef d   = ___selectedChassis.GetMainMechDef(___mechBay.Sim.DataManager);

            if (___selectedChassis.MechPartCount > 0) // this is actually a part that gets assembled
            {
                int p = SimpleMechAssembly_Main.GetNumPartsForAssembly(___mechBay.Sim, d);
                if (p < ___mechBay.Sim.Constants.Story.DefaultMechPartMax)
                {
                    GenericPopupBuilder.Create($"{d.GetMechOmniVehicle()} Assembly", SimpleMechAssembly_Main.GetAssembleNotEnoughPartsText(___mechBay.Sim, d)).AddButton("Cancel", null, true, null)
                    .AddFader(new UIColorRef?(LazySingletonBehavior <UIManager> .Instance.UILookAndColorConstants.PopupBackfill), 0f, true).Render();
                    return(false);
                }
                ___mechBay.Sim.InterruptQueue.AddInterrupt(new SimpleMechAssembly_Main.SimpleMechAssembly_InterruptManager_AssembleMechEntry(___mechBay.Sim, d, delegate
                {
                    ___mechBay.RefreshData(false);
                }), true);
                return(false);
            }
            if (___selectedChassis.MechPartCount < ___selectedChassis.MechPartMax)
            {
                return(true);
            }
            if (___selectedChassis.IsVehicle())
            {
                bay = CUIntegration.GetFirstFreeMechBay(___mechBay.Sim, d);
                if (bay < 0)
                {
                    GenericPopupBuilder.Create("Cannot Ready Vehicle", "There are no available slots in the Vehicle Bay. You must move an active Vehicle into storage before readying this chassis.")
                    .AddFader(new UIColorRef?(LazySingletonBehavior <UIManager> .Instance.UILookAndColorConstants.PopupBackfill), 0f, true).Render();
                }
                else
                {
                    GenericPopupBuilder.Create("Ready Vehicle?", $"It will take {Mathf.CeilToInt(___mechBay.Sim.Constants.Story.MechReadyTime / (float)___mechBay.Sim.MechTechSkill)} day(s) to ready this Vehicle for combat.")
                    .AddButton("Cancel", null, true, null).AddButton("Ready", delegate
                    {
                        if (___mechBay.Sim.ScrapInactiveMech(___selectedChassis.Description.Id, false))
                        {
                            SimpleMechAssembly_Main.ReadyMech(___mechBay.Sim, new MechDef(d, ___mechBay.Sim.GenerateSimGameUID(), true), bay, true);
                            ___mechBay.RefreshData(false);
                            ___mechBay.ViewBays();
                        }
                    }).AddFader(new UIColorRef?(LazySingletonBehavior <UIManager> .Instance.UILookAndColorConstants.PopupBackfill), 0f, true).CancelOnEscape().Render();
                }
                return(false);
            }
            if (!___selectedChassis.IsOmni())
            {
                return(true);
            }
            if (bay < 0)
            {
                return(true);
            }
            ___mechBay.Sim.InterruptQueue.AddInterrupt(new SimpleMechAssembly_Main.SimpleMechAssembly_InterruptManager_UnStorageOmniEntry(___mechBay.Sim, d, delegate
            {
                ___mechBay.RefreshData(false);
            }));
            return(false);
        }