001package com.fs.starfarer.api.impl.campaign.intel.inspection;
002
003import java.awt.Color;
004import java.util.Map;
005
006import org.lwjgl.input.Keyboard;
007
008import com.fs.starfarer.api.Global;
009import com.fs.starfarer.api.campaign.CampaignFleetAPI;
010import com.fs.starfarer.api.campaign.InteractionDialogAPI;
011import com.fs.starfarer.api.campaign.InteractionDialogPlugin;
012import com.fs.starfarer.api.campaign.OptionPanelAPI;
013import com.fs.starfarer.api.campaign.TextPanelAPI;
014import com.fs.starfarer.api.campaign.VisualPanelAPI;
015import com.fs.starfarer.api.campaign.rules.MemoryAPI;
016import com.fs.starfarer.api.combat.EngagementResultAPI;
017import com.fs.starfarer.api.impl.campaign.ids.Sounds;
018import com.fs.starfarer.api.impl.campaign.intel.events.HegemonyHostileActivityFactor;
019import com.fs.starfarer.api.impl.campaign.intel.inspection.HegemonyInspectionIntel.AntiInspectionOrders;
020import com.fs.starfarer.api.impl.campaign.rulecmd.AddRemoveCommodity;
021import com.fs.starfarer.api.impl.campaign.rulecmd.SetStoryOption;
022import com.fs.starfarer.api.ui.IntelUIAPI;
023import com.fs.starfarer.api.util.Misc;
024
025public class HIOrdersInteractionDialogPluginImpl implements InteractionDialogPlugin {
026
027        //public static float BRIBE_BASE = 0;
028        public static int BRIBE_MULT = 200000;
029        public static int BRIBE_MAX = 500000;
030        
031        private static enum OptionId {
032                INIT,
033                COMPLY,
034                BRIBE,
035                RESIST,
036                LEAVE,
037                
038                CONFIRM,
039                CANCEL,
040                
041                HIDE, // too similar to "bribe"; not adding
042        }
043        
044        protected InteractionDialogAPI dialog;
045        protected TextPanelAPI textPanel;
046        protected OptionPanelAPI options;
047        protected VisualPanelAPI visual;
048        
049        protected CampaignFleetAPI playerFleet;
050
051        protected HegemonyInspectionIntel intel;
052        protected IntelUIAPI ui;
053        
054        public HIOrdersInteractionDialogPluginImpl(HegemonyInspectionIntel intel, IntelUIAPI ui) {
055                this.intel = intel;
056                this.ui = ui;
057        }
058
059        public void init(InteractionDialogAPI dialog) {
060                this.dialog = dialog;
061                textPanel = dialog.getTextPanel();
062                options = dialog.getOptionPanel();
063                visual = dialog.getVisualPanel();
064
065                playerFleet = Global.getSector().getPlayerFleet();
066                
067                visual.setVisualFade(0.25f, 0.25f);
068                //visual.showImagePortion("illustrations", "quartermaster", 640, 400, 0, 0, 480, 300);
069                visual.showPlanetInfo(intel.getTarget().getPrimaryEntity());
070        
071                dialog.setOptionOnEscape("Leave", OptionId.LEAVE);
072                
073                optionSelected(null, OptionId.INIT);
074        }
075        
076        public Map<String, MemoryAPI> getMemoryMap() {
077                return null;
078        }
079        
080        public void backFromEngagement(EngagementResultAPI result) {
081                // no combat here, so this won't get called
082        }
083        
084        protected int computeBribeAmount() {
085//              float threshold = HegemonyInspectionManager.getInstance().getThreshold();
086//              int bribe = (int) Math.round(BRIBE_BASE + threshold * BRIBE_MULT);
087//              return bribe;
088                
089                //int bribe = (int) (Math.pow(1.5f, HegemonyInspectionManager.getInstance().getNumAttempts()) * BRIBE_MULT);
090                int bribe = (int) (Math.pow(1.5f, HegemonyHostileActivityFactor.getInspectionAttempts()) * BRIBE_MULT);
091                if (bribe > BRIBE_MAX) bribe = BRIBE_MAX;
092                return bribe;
093                
094//              int bribe = 20000;
095//              for (String id : intel.getExpectedCores()) {
096//                      CommoditySpecAPI spec = Global.getSettings().getCommoditySpec(id);
097//                      bribe += spec.getBasePrice() * 2;
098//              }
099//              return bribe;
100        }
101        
102        protected void printOptionDesc(AntiInspectionOrders orders, boolean inConfirm) {
103                switch (orders) {
104                case BRIBE:
105                        int bribe = computeBribeAmount();
106                        textPanel.addPara("Sufficient funding allocated to proper official and unofficial actors should " +
107                                        "ensure that the inspection reaches a satisfactory outcome.");
108
109                        if (inConfirm) {
110                                textPanel.addPara("Once this order is given and the funds and agents dispatched, it can not be " +
111                                                                  "rescinded.");
112                                
113                                int credits = (int) playerFleet.getCargo().getCredits().get();
114                                Color costColor = Misc.getHighlightColor();
115                                if (bribe > credits) costColor = Misc.getNegativeHighlightColor();
116                                
117//                              textPanel.addPara("A total of %s should be enough to get the job done. " +
118//                                      "It's more expensive than the cores involved, " +
119//                                      "but guarantees that your standing with the Hegemony will not suffer.", costColor,
120//                                                              Misc.getDGSCredits(bribe));
121                                textPanel.addPara("A total of %s should be enough to get the job done. " +
122                                                "and guarantees that your standing with the Hegemony will not suffer.", costColor,
123                                                Misc.getDGSCredits(bribe));
124                                
125                                textPanel.addPara("You have %s available.", Misc.getHighlightColor(),
126                                                Misc.getDGSCredits(credits));
127                        } else {
128                                textPanel.addPara("You've allocated %s to the task and have otherwise committed to this course of action.",
129                                                                  Misc.getHighlightColor(), Misc.getDGSCredits(bribe));
130//                              textPanel.addPara("You've allocated %s to the task. Giving different orders will allow you to recover these funds.", Misc.getHighlightColor(),
131//                                              Misc.getDGSCredits(bribe));
132                        }
133                        break;
134                case COMPLY:
135                        textPanel.addPara("The local authorities will comply with the inspection. This will result " +
136                                        "in all AI cores being found and confiscated, and will cause your standing with the Hegemony " +
137                                        "to fall based on the number of AI cores found.");
138                        textPanel.addPara("If AI cores currently in use are removed or moved off-planet, this activity will " +
139                                        "surely leave traces for inspectors to find, inspiring them to much greater zeal.");
140                        break;
141                case RESIST:
142                        textPanel.addPara("All space and ground forces available will resist the inspection.");
143                        textPanel.addPara("If the inspection reaches the surface, " +
144                                                         "the ground defense strength will determine whether " +
145                                                         "they're able to confiscate any AI cores.");
146                        textPanel.addPara("The Hegemony will become aware of this - and hostile - when the inspection task force " +
147                                                          "enters the star system."); 
148                        break;
149                }
150        }
151        
152        protected void addChoiceOptions() {
153                options.clearOptions();
154
155                AntiInspectionOrders curr = intel.getOrders();
156                if (curr != AntiInspectionOrders.BRIBE) {
157                        options.addOption("Order the local authorities to comply with the inspection", OptionId.COMPLY, null);
158                        options.addOption("Allocate sufficient funds to bribe or otherwise handle the inspectors", OptionId.BRIBE, null);
159                        options.addOption("Order your local forces to resist the inspection", OptionId.RESIST, null);
160        
161                        dialog.setOptionColor(OptionId.BRIBE, Misc.getStoryOptionColor());
162                }
163                
164                options.addOption("Dismiss", OptionId.LEAVE, null);
165                options.setShortcut(OptionId.LEAVE, Keyboard.KEY_ESCAPE, false, false, false, true);
166                
167                if (curr == AntiInspectionOrders.COMPLY) {
168                        options.setEnabled(OptionId.COMPLY, false);
169                }
170                if (curr == AntiInspectionOrders.BRIBE) {
171                        options.setEnabled(OptionId.BRIBE, false);
172                }
173                if (curr == AntiInspectionOrders.RESIST) {
174                        options.setEnabled(OptionId.RESIST, false);
175                }
176        }
177        
178        protected AntiInspectionOrders beingConfirmed = null;
179        protected void addConfirmOptions() {
180                if (beingConfirmed == null) return;
181                
182                options.clearOptions();
183                
184                printOptionDesc(beingConfirmed, true);
185                
186                options.addOption("Confirm your orders", OptionId.CONFIRM, null);
187                options.addOption("Never mind", OptionId.CANCEL, null);
188                options.setShortcut(OptionId.CANCEL, Keyboard.KEY_ESCAPE, false, false, false, true);
189
190                if (beingConfirmed == AntiInspectionOrders.BRIBE) {
191                        int bribe = computeBribeAmount();
192                        if (bribe > playerFleet.getCargo().getCredits().get()) {
193                                options.setEnabled(OptionId.CONFIRM, false);
194                                options.setTooltip(OptionId.CONFIRM, "Not enough credits.");
195                        }
196                        
197                        SetStoryOption.set(dialog, 1, OptionId.CONFIRM, "bribeAICoreInspection", Sounds.STORY_POINT_SPEND_TECHNOLOGY,
198                                        "Issued bribe to prevent " + intel.getFaction().getDisplayName() + " AI core inspection");
199//                      StoryOptionParams params = new StoryOptionParams(OptionId.BRIBE, 1, "bribeAICoreInspection", Sounds.STORY_POINT_SPEND_TECHNOLOGY);
200//                      SetStoryOption.set(dialog, params, new BaseOptionStoryPointActionDelegate(dialog, params) {
201//                              @Override
202//                              public void createDescription(TooltipMakerAPI info) {
203//                                      float opad = 10f;
204//                                      info.setParaInsigniaLarge();
205//                                      info.addPara("Virtually guarantees that the inspection will not find any AI cores.",
206//                                                      -opad);
207//                                      info.addSpacer(opad * 2f);
208//                                      addActionCostSection(info);
209//                              }
210//                      });
211                }
212        }
213        
214        
215        
216        public void optionSelected(String text, Object optionData) {
217                if (optionData == null) return;
218                
219                OptionId option = (OptionId) optionData;
220                
221                if (text != null) {
222                        //textPanel.addParagraph(text, Global.getSettings().getColor("buttonText"));
223                        dialog.addOptionSelectedText(option);
224                }
225                
226                switch (option) {
227                case INIT:
228                        printOptionDesc(intel.getOrders(), false);
229                        addChoiceOptions();
230                        break;
231                case COMPLY:
232                        beingConfirmed = AntiInspectionOrders.COMPLY;
233                        addConfirmOptions();
234                        break;
235                case BRIBE:
236                        beingConfirmed = AntiInspectionOrders.BRIBE;
237                        addConfirmOptions();
238                        break;
239                case RESIST:
240                        beingConfirmed = AntiInspectionOrders.RESIST;
241                        addConfirmOptions();
242                        break;
243                case CONFIRM:
244                        int invested = intel.getInvestedCredits();
245                        if (invested > 0) {
246                                AddRemoveCommodity.addCreditsGainText(invested, textPanel);
247                                playerFleet.getCargo().getCredits().add(invested);
248                                intel.setInvestedCredits(0);
249                        }
250                        
251                        intel.setOrders(beingConfirmed);
252                        if (beingConfirmed == AntiInspectionOrders.BRIBE) {
253                                int bribe = computeBribeAmount();
254                                intel.setInvestedCredits(bribe);
255                                AddRemoveCommodity.addCreditsLossText(bribe, textPanel);
256                                playerFleet.getCargo().getCredits().subtract(bribe);
257                        } else {
258                                
259                        }
260                        
261                        addChoiceOptions();
262                        //leave();
263                        break;
264                case CANCEL:
265                        addChoiceOptions();
266                        break;
267                case LEAVE:
268                        leave();
269                        break;
270                }
271        }
272        
273        protected void leave() {
274                dialog.dismiss();
275                if (ui != null) ui.updateUIForItem(intel);      
276        }
277        
278        
279        public void optionMousedOver(String optionText, Object optionData) {
280
281        }
282        
283        public void advance(float amount) {
284                
285        }
286        
287        public Object getContext() {
288                return null;
289        }
290}
291
292
293