001package com.fs.starfarer.api.impl.campaign;
002
003import java.awt.Color;
004
005import com.fs.starfarer.api.Global;
006import com.fs.starfarer.api.campaign.CoreUITabId;
007import com.fs.starfarer.api.campaign.FactionAPI;
008import com.fs.starfarer.api.campaign.InteractionDialogAPI;
009import com.fs.starfarer.api.campaign.RuleBasedDialog;
010import com.fs.starfarer.api.campaign.econ.AbandonMarketPlugin;
011import com.fs.starfarer.api.campaign.econ.MarketAPI;
012import com.fs.starfarer.api.campaign.listeners.ListenerUtil;
013import com.fs.starfarer.api.impl.campaign.intel.deciv.DecivTracker;
014import com.fs.starfarer.api.impl.campaign.rulecmd.FireAll;
015import com.fs.starfarer.api.impl.campaign.rulecmd.ShowDefaultVisual;
016import com.fs.starfarer.api.ui.LabelAPI;
017import com.fs.starfarer.api.ui.TooltipMakerAPI;
018import com.fs.starfarer.api.util.Misc;
019
020public class AbandonMarketPluginImpl extends BaseGenericPlugin implements AbandonMarketPlugin {
021
022        public static int MAX_SIZE = 4;
023        public static int COST_PER_SIZE_MINUS_2 = 50000;
024        
025        public boolean canAbandon(MarketAPI market) {
026                return market.getSize() <= MAX_SIZE;// && market.getPlanetEntity() != null;
027        }
028
029        public int getAbandonCost(MarketAPI market) {
030                return Math.max(1, market.getSize() - 2) * COST_PER_SIZE_MINUS_2;
031        }
032        
033        public void createAbandonButtonTooltip(TooltipMakerAPI info, float width, boolean expanded, MarketAPI market) {
034                info.addPara("Can only abandon colonies of size %s or smaller.", 0f,
035                                Misc.getNegativeHighlightColor(), Misc.getHighlightColor(),
036                                "" + MAX_SIZE);
037        }
038
039        public boolean isAbandonButtonTooltipExpandable(MarketAPI market) {
040                return false;
041        }
042
043        public boolean abandonButtonHasTooltip(MarketAPI market) {
044                return !canAbandon(market);
045        }
046        
047
048        public void createConfirmationPrompt(MarketAPI market, TooltipMakerAPI prompt) {
049                float opad = 10f;
050                
051                int cost = getAbandonCost(market);
052                int refund = Misc.computeTotalShutdownRefund(market);
053                int credits = (int) Global.getSector().getPlayerFleet().getCargo().getCredits().get();
054                
055                Color h = Misc.getHighlightColor();
056                Color cc = h;
057                
058                String costStr = "cost";
059                if (refund > cost) {
060                        costStr = "payout";
061                }
062                
063                if (cost - refund > credits) {
064                        cc = Misc.getNegativeHighlightColor();
065                }
066                
067//              prompt.setParaFontDefault();
068//              prompt.setParaSmallInsignia();
069                
070                FactionAPI faction = market.getFaction();
071//              LabelAPI label = prompt.addPara("You can abandon %s, a size %s colony, for a cost of %s in evacuation expenses.", 0f,
072//                              h, market.getName(), "" + market.getSize(), Misc.getDGSCredits(cost));
073                LabelAPI label = prompt.addPara("Abandoning %s, a size %s colony, requires %s in evacuation expenses. " +
074                                "Shutting down all operations will generate %s credits, for a net " + costStr + " of %s.",
075                                0f,
076                                h,
077                                market.getName(), 
078                                "" + market.getSize(),
079                                Misc.getDGSCredits(cost),
080                                Misc.getDGSCredits(refund),
081                                Misc.getDGSCredits(Math.abs(cost - refund))
082                                );
083                label.setHighlightColors(faction.getBaseUIColor(), h, h, h, cc);
084                
085                
086                prompt.addPara("You have %s credits.", opad, h, Misc.getDGSCredits(credits));
087                
088                prompt.addPara("This action can not be undone. All items in storage and in use by industries will be lost.", opad);
089        }
090        
091        public boolean isConfirmEnabled(MarketAPI market) {
092                int cost = getAbandonCost(market);
093                int refund = Misc.computeTotalShutdownRefund(market);
094                
095                return Global.getSector().getPlayerFleet().getCargo().getCredits().get() >= (cost - refund);
096        }
097
098        public float getConfirmationPromptWidth(MarketAPI market) {
099                return 550f;
100        }
101
102        public void abandonConfirmed(MarketAPI market) {
103                
104                int cost = getAbandonCost(market);
105                int refund = Misc.computeTotalShutdownRefund(market);
106                
107                int diff = cost - refund;
108                
109                Global.getSector().getPlayerFleet().getCargo().getCredits().subtract(diff);
110                
111                if (diff > 0) {
112                        Global.getSector().getCampaignUI().getMessageDisplay().addMessage(
113                                        String.format("Spent %s", Misc.getDGSCredits(diff)), 
114                                        Misc.getTooltipTitleAndLightHighlightColor(), Misc.getDGSCredits(diff), Misc.getHighlightColor());
115                } else if (diff < 0) {
116                        Global.getSector().getCampaignUI().getMessageDisplay().addMessage(
117                                        String.format("Received %s", Misc.getDGSCredits(-diff)), 
118                                        Misc.getTooltipTitleAndLightHighlightColor(), Misc.getDGSCredits(-diff), Misc.getHighlightColor());
119                }
120                
121                DecivTracker.removeColony(market, false);
122                
123                ListenerUtil.reportPlayerAbandonedColony(market);
124                
125                InteractionDialogAPI dialog = Global.getSector().getCampaignUI().getCurrentInteractionDialog();
126                if (dialog != null && dialog.getPlugin() instanceof RuleBasedDialog) {
127                        //dialog.dismiss();
128                        //dialog.getVisualPanel().closeCoreUI();
129                        RuleBasedDialog rbd = ((RuleBasedDialog) dialog.getPlugin());
130                        rbd.updateMemory();
131                        market.getMemoryWithoutUpdate().unset("$tradeMode");
132                        if (market.getPrimaryEntity() != null) {
133                                market.getPrimaryEntity().getMemoryWithoutUpdate().unset("$tradeMode");
134                        }
135                        //FireBest.fire(null, dialog, rbd.getMemoryMap(), "OpenInteractionDialog");
136                        
137                        new ShowDefaultVisual().execute(null, dialog, Misc.tokenize(""), rbd.getMemoryMap());
138                        
139                        //FireAll.fire(null, dialog, memoryMap, "MarketPostOpen");
140                        dialog.getInteractionTarget().getMemoryWithoutUpdate().set("$menuState", "main", 0);
141                        dialog.getInteractionTarget().getMemoryWithoutUpdate().set("$tradeMode", "NONE", 0);
142                        dialog.getInteractionTarget().getMemoryWithoutUpdate().unset("$hasMarket");
143                        //FireAll.fire(null, dialog, rbd.getMemoryMap(), "PopulateOptions");
144                        
145                        dialog.getVisualPanel().closeCoreUI();
146                        
147                        rbd.updateMemory();
148                        FireAll.fire(null, dialog, rbd.getMemoryMap(), "OpenInteractionDialog");
149                } else {
150                        // if abandoned from command tab (rather than by interacting with colony), go back to colony list
151                        Global.getSector().getCampaignUI().showCoreUITab(CoreUITabId.OUTPOSTS);
152                }
153        }
154
155}
156
157
158
159
160
161