001package com.fs.starfarer.api.impl.campaign;
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.CoreInteractionListener;
011import com.fs.starfarer.api.campaign.CoreUITabId;
012import com.fs.starfarer.api.campaign.InteractionDialogAPI;
013import com.fs.starfarer.api.campaign.InteractionDialogPlugin;
014import com.fs.starfarer.api.campaign.OptionPanelAPI;
015import com.fs.starfarer.api.campaign.PlanetAPI;
016import com.fs.starfarer.api.campaign.SectorEntityToken;
017import com.fs.starfarer.api.campaign.TextPanelAPI;
018import com.fs.starfarer.api.campaign.VisualPanelAPI;
019import com.fs.starfarer.api.campaign.rules.MemoryAPI;
020import com.fs.starfarer.api.combat.EngagementResultAPI;
021import com.fs.starfarer.api.fleet.FleetMemberAPI;
022import com.fs.starfarer.api.loading.Description;
023import com.fs.starfarer.api.loading.Description.Type;
024
025public class OrbitalStationInteractionDialogPluginImpl implements InteractionDialogPlugin, CoreInteractionListener {
026
027        private static enum OptionId {
028                INIT,
029                INIT_NO_TEXT,
030                TRADE_CARGO,
031                TRADE_SHIPS,
032                REFIT,
033                REPAIR_ALL,
034                LEAVE,
035        }
036        
037        private InteractionDialogAPI dialog;
038        private TextPanelAPI textPanel;
039        private OptionPanelAPI options;
040        private VisualPanelAPI visual;
041        
042        private CampaignFleetAPI playerFleet;
043        private SectorEntityToken station;
044        
045        private static final Color HIGHLIGHT_COLOR = Global.getSettings().getColor("buttonShortcut");
046        
047        public void init(InteractionDialogAPI dialog) {
048                this.dialog = dialog;
049                textPanel = dialog.getTextPanel();
050                options = dialog.getOptionPanel();
051                visual = dialog.getVisualPanel();
052
053                playerFleet = Global.getSector().getPlayerFleet();
054                station = (SectorEntityToken) dialog.getInteractionTarget();
055                
056                
057                visual.setVisualFade(0.25f, 0.25f);
058        
059                //dialog.setTextHeight(100);
060                
061                dialog.setOptionOnEscape("Leave", OptionId.LEAVE);
062                
063                optionSelected(null, OptionId.INIT);
064        }
065        
066        public Map<String, MemoryAPI> getMemoryMap() {
067                return null;
068        }
069        
070        private EngagementResultAPI lastResult = null;
071        public void backFromEngagement(EngagementResultAPI result) {
072                // no combat here, so this won't get called
073        }
074        
075        public void optionSelected(String text, Object optionData) {
076                if (optionData == null) return;
077                
078                OptionId option = (OptionId) optionData;
079                
080                if (text != null) {
081                        //textPanel.addParagraph(text, Global.getSettings().getColor("buttonText"));
082                        dialog.addOptionSelectedText(option);
083                }
084                
085                switch (option) {
086                case INIT:
087                        Description desc = Global.getSettings().getDescription(station.getCustomDescriptionId(), Type.CUSTOM);
088                        if (desc != null && desc.hasText3()) {
089                                addText(desc.getText3());
090                        }
091                        addText(getString("approach"));
092                case INIT_NO_TEXT:
093                        createInitialOptions();
094                        if (station.getCustomInteractionDialogImageVisual() != null) {
095                                visual.showImageVisual(station.getCustomInteractionDialogImageVisual());
096                        } else {
097                                if (station instanceof PlanetAPI) {
098                                        visual.showPlanetInfo(station);
099                                } else {
100                                        //visual.showImagePortion("illustrations", "hound_hangar", 1280, 800, 0, 0, 480, 300);
101                                        visual.showImagePortion("illustrations", "hound_hangar", 640, 400, 0, 0, 480, 300);
102                                }
103                        }
104                        break;
105                case TRADE_CARGO:
106                        addText(getString("tradeCargo"));
107                        options.clearOptions();
108                        visual.showCore(CoreUITabId.CARGO, station, this);
109                        break;
110                case TRADE_SHIPS:
111                        addText(getString("tradeShips"));
112                        options.clearOptions();
113                        visual.showCore(CoreUITabId.FLEET, station, this);
114                        break;
115                case REFIT:
116                        addText(getString("refit"));
117                        options.clearOptions();
118                        visual.showCore(CoreUITabId.REFIT, station, this);
119                        break;
120                case REPAIR_ALL:
121                        performRepairs();
122                        createInitialOptions();
123                        break;
124                case LEAVE:
125                        Global.getSector().setPaused(false);
126                        dialog.dismiss();
127                        break;
128                }
129        }
130        
131        private void performRepairs() {
132                addText(getString("repair"));
133                float supplies = playerFleet.getCargo().getSupplies();
134                float needed = playerFleet.getLogistics().getTotalRepairAndRecoverySupplyCost();
135                
136                textPanel.highlightLastInLastPara("" + (int) needed, HIGHLIGHT_COLOR);
137                
138                for (FleetMemberAPI member : playerFleet.getFleetData().getMembersListCopy()) {
139                        member.getStatus().repairFully();
140                        float max = member.getRepairTracker().getMaxCR();
141                        float curr = member.getRepairTracker().getBaseCR();
142                        if (max > curr) {
143                                member.getRepairTracker().applyCREvent(max - curr, "Repaired at station");
144                        }
145                }
146                if (needed > 0) {
147                        playerFleet.getCargo().removeSupplies(needed);
148                        playerFleet.getLogistics().updateRepairUtilizationForUI();
149                }
150        }
151        
152        private void createInitialOptions() {
153                options.clearOptions();
154                
155                
156                if (station.getFaction().isNeutralFaction()) {
157                        options.addOption("Transfer cargo or personnel", OptionId.TRADE_CARGO);
158                        options.setShortcut(OptionId.TRADE_CARGO, Keyboard.KEY_I, false, false, false, true);
159                        options.addOption("Transfer ships to or from this station", OptionId.TRADE_SHIPS);
160                        options.setShortcut(OptionId.TRADE_SHIPS, Keyboard.KEY_F, false, false, false, true);
161                        options.addOption("Make use of the dockyard's refitting facilities", OptionId.REFIT);
162                        options.setShortcut(OptionId.REFIT, Keyboard.KEY_R, false, false, false, true);
163                } else {
164                        options.addOption("Trade, or hire personnel", OptionId.TRADE_CARGO);
165                        options.setShortcut(OptionId.TRADE_CARGO, Keyboard.KEY_I, false, false, false, true);
166                        options.addOption("Buy or sell ships", OptionId.TRADE_SHIPS, null);
167                        options.setShortcut(OptionId.TRADE_SHIPS, Keyboard.KEY_F, false, false, false, true);
168                        options.addOption("Make use of the dockyard's refitting facilities", OptionId.REFIT);
169                        options.setShortcut(OptionId.REFIT, Keyboard.KEY_R, false, false, false, true);
170                }
171                
172                if (station.getFaction().getRelationship(playerFleet.getFaction().getId()) >= 0) {
173                        float needed = playerFleet.getLogistics().getTotalRepairAndRecoverySupplyCost();
174                        float supplies = playerFleet.getCargo().getSupplies();
175                        options.addOption("Repair your ships at the station's dockyard", OptionId.REPAIR_ALL);
176                        options.setShortcut(OptionId.REPAIR_ALL, Keyboard.KEY_A, false, false, false, true);
177
178                        if (needed <= 0) {
179                                options.setEnabled(OptionId.REPAIR_ALL, false);
180                                options.setTooltip(OptionId.REPAIR_ALL, getString("repairTooltipAlreadyRepaired"));
181                        } else if (supplies < needed) {
182                                options.setEnabled(OptionId.REPAIR_ALL, false);
183                                options.setTooltip(OptionId.REPAIR_ALL, getString("repairTooltipNotEnough"));
184                                options.setTooltipHighlightColors(OptionId.REPAIR_ALL, HIGHLIGHT_COLOR, HIGHLIGHT_COLOR);
185                                options.setTooltipHighlights(OptionId.REPAIR_ALL, "" + (int) Math.ceil(needed), "" + (int) supplies);
186                        } else {
187                                options.setTooltip(OptionId.REPAIR_ALL, getString("repairTooltip"));
188                                options.setTooltipHighlightColors(OptionId.REPAIR_ALL, HIGHLIGHT_COLOR, HIGHLIGHT_COLOR);
189                                options.setTooltipHighlights(OptionId.REPAIR_ALL, "" + (int) Math.ceil(needed), "" + (int) supplies);
190                        }
191                }
192                
193                options.addOption("Leave", OptionId.LEAVE);
194        }
195        
196        
197        private OptionId lastOptionMousedOver = null;
198        public void optionMousedOver(String optionText, Object optionData) {
199
200        }
201        
202        public void advance(float amount) {
203                
204        }
205        
206        private void addText(String text) {
207                textPanel.addParagraph(text);
208        }
209        
210        private void appendText(String text) {
211                textPanel.appendToLastParagraph(" " + text);
212        }
213        
214        private String getString(String id) {
215                String str = Global.getSettings().getString("stationInteractionDialog", id);
216
217                String fleetOrShip = "fleet";
218                if (playerFleet.getFleetData().getMembersListCopy().size() == 1) {
219                        fleetOrShip = "ship";
220                        if (playerFleet.getFleetData().getMembersListCopy().get(0).isFighterWing()) {
221                                fleetOrShip = "fighter wing";
222                        }
223                }
224                str = str.replaceAll("\\$fleetOrShip", fleetOrShip);
225                str = str.replaceAll("\\$stationName", station.getName());
226                
227                float needed = playerFleet.getLogistics().getTotalRepairAndRecoverySupplyCost();
228                float supplies = playerFleet.getCargo().getSupplies();
229                str = str.replaceAll("\\$supplies", "" + (int) supplies);
230                str = str.replaceAll("\\$repairSupplyCost", "" + (int) Math.ceil(needed));
231
232                return str;
233        }
234        
235
236        public Object getContext() {
237                return null;
238        }
239
240        public void coreUIDismissed() {
241                optionSelected(null, OptionId.INIT_NO_TEXT);
242        }
243}
244
245
246