001package com.fs.starfarer.api.impl.campaign.intel.misc;
002
003import java.util.Set;
004
005import java.awt.Color;
006
007import org.lwjgl.input.Keyboard;
008
009import com.fs.starfarer.api.Global;
010import com.fs.starfarer.api.campaign.CampaignFleetAPI;
011import com.fs.starfarer.api.campaign.CampaignTerrainAPI;
012import com.fs.starfarer.api.campaign.FactionAPI;
013import com.fs.starfarer.api.campaign.SectorEntityToken;
014import com.fs.starfarer.api.campaign.TextPanelAPI;
015import com.fs.starfarer.api.impl.campaign.ids.Tags;
016import com.fs.starfarer.api.impl.campaign.terrain.DebrisFieldTerrainPlugin;
017import com.fs.starfarer.api.loading.Description;
018import com.fs.starfarer.api.loading.Description.Type;
019import com.fs.starfarer.api.ui.ButtonAPI;
020import com.fs.starfarer.api.ui.IntelUIAPI;
021import com.fs.starfarer.api.ui.SectorMapAPI;
022import com.fs.starfarer.api.ui.TooltipMakerAPI;
023import com.fs.starfarer.api.util.Misc;
024
025/**
026 * @author Alex
027 *
028 */
029public class MapMarkerIntel extends FleetLogIntel {
030
031        public static String BUTTON_EDIT = "button_edit";
032        
033        protected SectorEntityToken entity;
034        protected SectorEntityToken copy;
035        
036        protected String title;
037        protected String text;
038        private boolean withDesc;
039        
040        private boolean withDeleteButton = true;
041        private boolean withTimestamp = true;
042
043        
044        public MapMarkerIntel() {
045        }
046        
047        public MapMarkerIntel(SectorEntityToken entity, String title, String text, String icon, boolean withDesc) {
048                super();
049                init(entity, title, text, icon, withDesc);
050        }
051        
052        protected void init(SectorEntityToken entity, String title, String text, String icon, boolean withDesc) {
053                init(entity, title, text, icon, withDesc, null);
054        }
055        protected void init(SectorEntityToken entity, String title, String text, String icon, boolean withDesc, TextPanelAPI textPanel) {
056                this.entity = entity;
057                this.withDesc = withDesc;
058                //setRemoveTrigger(this.entity);
059                
060                // otherwise a marker on an already-explored debris field auto-removes immediately
061                if (entity instanceof CampaignTerrainAPI) {
062                        CampaignTerrainAPI terrain = (CampaignTerrainAPI) entity;
063                        if (terrain.getPlugin() instanceof DebrisFieldTerrainPlugin) {
064                                DebrisFieldTerrainPlugin debris = (DebrisFieldTerrainPlugin) terrain.getPlugin();
065                                if (debris.isScavenged()) {
066                                        setKeepExploredDebrisField(true);
067                                }
068                        }
069                }
070                
071                
072                if (entity instanceof CampaignFleetAPI && !((CampaignFleetAPI) entity).isStationMode()) {
073                        copy = makeDoubleWithSameOrbit(entity);
074                        copy.getContainingLocation().addEntity(copy);
075                        setRemoveTrigger(copy);
076                } else {
077                        setRemoveTrigger(this.entity);
078                }
079                
080                this.title = title;
081                this.text = text;
082                setIcon(icon);
083                
084                setListInfoParam(DISCOVERED_PARAM);
085                Global.getSector().getIntelManager().addIntel(this, false, textPanel);
086                setListInfoParam(null);
087        }
088        
089        @Override
090        public void reportRemovedIntel() {
091                super.reportRemovedIntel();
092                if (copy != null && copy.getContainingLocation() != null) {
093                        copy.getContainingLocation().removeEntity(copy);
094                }
095        }
096
097        public static SectorEntityToken makeDoubleWithSameOrbit(SectorEntityToken entity) {
098                SectorEntityToken copy = entity.getContainingLocation().createToken(entity.getLocation().x, entity.getLocation().y);
099                if (entity.getOrbit() != null) {
100                        copy.setOrbit(entity.getOrbit().makeCopy());
101                }
102                return copy;
103        }
104        
105        
106        public String getTitle() {
107                return title;
108        }
109
110        public void setTitle(String title) {
111                this.title = title;
112        }
113        
114        public SectorEntityToken getEntity() {
115                return entity;
116        }
117
118        public void setEntity(SectorEntityToken entity) {
119                this.entity = entity;
120        }
121
122        public String getText() {
123                return text;
124        }
125
126        public void setText(String text) {
127                this.text = text;
128        }
129
130        public boolean isWithDesc() {
131                return withDesc;
132        }
133
134        public void setWithDesc(boolean withDesc) {
135                this.withDesc = withDesc;
136        }
137
138        @Override
139        public void createIntelInfo(TooltipMakerAPI info, ListInfoMode mode) {
140                Color c = getTitleColor(mode);
141                info.addPara(getName(), c, 0f);
142                addBulletPoints(info, mode);
143        }
144        
145        protected void addBulletPoints(TooltipMakerAPI info, ListInfoMode mode) {
146                //if (text == null || text.trim().isEmpty()) return;
147                
148                
149                Color h = Misc.getHighlightColor();
150                Color g = Misc.getGrayColor();
151                float pad = 3f;
152                float opad = 10f;
153                
154                float initPad = pad;
155                if (mode == ListInfoMode.IN_DESC) initPad = opad;
156                
157                Color tc = getBulletColorForMode(mode);
158                boolean isUpdate = getListInfoParam() != null;
159                
160                bullet(info);
161                if (text != null && !text.isEmpty()) {
162                        String str = text;
163                        if (endsInPunct(str, true)) {
164                                str = str.substring(0, str.length() - 1);
165                        }
166                        info.addPara(str, tc, initPad);
167                        initPad = 0f;
168                }
169                addExtraBulletPoints(info, tc, initPad, mode);
170                unindent(info);
171        }
172        
173        protected void addExtraBulletPoints(TooltipMakerAPI info, Color tc, float initPad, ListInfoMode mode) {
174                
175        }
176        
177        protected boolean endsInPunct(String str, boolean forBulletPoint) {
178                String punct = ",.?!:;";
179                if (forBulletPoint) {
180                        punct = ",.:;";
181                }
182                String end = "" + str.charAt(str.length() - 1);
183                return punct.contains(end);
184        }
185        
186        
187        
188        protected boolean withTextInDesc() {
189                return true;
190        }
191        protected boolean withCustomVisual() {
192                return false;
193        }
194        protected boolean withCustomDescription() {
195                return false;
196        }
197        
198        protected void addCustomVisual(TooltipMakerAPI info, float width, float height) {
199                
200        }
201        protected void addCustomDescription(TooltipMakerAPI info, float width, float height) {
202                
203        }
204        
205        protected void addPostDescriptionSection(TooltipMakerAPI info, float width, float height, float opad) {
206                
207        }
208        
209        public boolean isWithDeleteButton() {
210                return withDeleteButton;
211        }
212
213        public void setWithDeleteButton(boolean withDeleteButton) {
214                this.withDeleteButton = withDeleteButton;
215        }
216
217        public boolean isWithTimestamp() {
218                return withTimestamp;
219        }
220
221        public void setWithTimestamp(boolean withTimestamp) {
222                this.withTimestamp = withTimestamp;
223        }
224
225        @Override
226        public void createSmallDescription(TooltipMakerAPI info, float width, float height) {
227                Color h = Misc.getHighlightColor();
228                Color g = Misc.getGrayColor();
229                Color tc = Misc.getTextColor();
230                float pad = 3f;
231                float opad = 10f;
232
233
234                boolean addedDesc = false;
235                if (withDesc) {
236                        if (withCustomVisual()) {
237                                addCustomVisual(info, width, height);
238                        } else if (entity.getCustomInteractionDialogImageVisual() != null) {
239                                info.addImage(entity.getCustomInteractionDialogImageVisual().getSpriteName(), width, opad);
240                        }
241                        
242                        if (withCustomDescription()) {
243                                addCustomDescription(info, width, height);
244                                addedDesc = true;
245                        } else if (entity.getCustomDescriptionId() != null) {
246                                Description desc = Global.getSettings().getDescription(entity.getCustomDescriptionId(), Type.CUSTOM);
247                                info.addPara(desc.getText1(), opad);
248                                addedDesc = true;
249                        }
250                }
251                
252                if (text != null && !text.trim().isEmpty() && withTextInDesc()) {
253                        if (addedDesc) {
254                                addBulletPoints(info, ListInfoMode.IN_DESC);
255                        } else {
256                                info.addPara(text + (endsInPunct(text, false) ? "" : "."), opad);
257                        }
258                }
259                
260                addPostDescriptionSection(info, width, height, opad);
261                //target.getOrbit().updateLocation();
262                
263                if (isWithTimestamp()) {
264                        addLogTimestamp(info, tc, opad);
265                }
266                
267                
268                if (getClass() == MapMarkerIntel.class) {
269                        ButtonAPI button = info.addButton("Edit", BUTTON_EDIT, 
270                                                getFactionForUIColors().getBaseUIColor(), getFactionForUIColors().getDarkUIColor(),
271                                          (int)(width), 20f, opad * 2f);
272                        button.setShortcut(Keyboard.KEY_T, true);
273                        if (isWithDeleteButton()) {
274                                info.addSpacer(-opad);
275                        }
276                }
277                
278                if (isWithDeleteButton()) {
279                        addDeleteButton(info, width);
280                }
281        }
282        
283
284        @Override
285        public void buttonPressConfirmed(Object buttonId, IntelUIAPI ui) {
286                if (buttonId == BUTTON_EDIT) {
287                        ui.showEditIntelMarkerDialog(this);
288                        return;
289                }
290                super.buttonPressConfirmed(buttonId, ui);
291        }
292
293        @Override
294        public boolean doesButtonHaveConfirmDialog(Object buttonId) {
295                return super.doesButtonHaveConfirmDialog(buttonId);
296        }
297
298        @Override
299        public Set<String> getIntelTags(SectorMapAPI map) {
300                Set<String> tags = super.getIntelTags(map);
301                if (getClass() == MapMarkerIntel.class) {
302                        tags.add(Tags.INTEL_MARKER);
303                }
304                return tags;
305        }
306
307        public String getSortString() {
308                return super.getSortString();
309        }
310
311        protected transient String discoveredPrefixOverride = null;
312        
313        public String getDiscoveredPrefixOverride() {
314                return discoveredPrefixOverride;
315        }
316
317        public void setDiscoveredPrefixOverride(String discoveredPrefixOverride) {
318                this.discoveredPrefixOverride = discoveredPrefixOverride;
319        }
320
321        public String getName() {
322                String prefix = "";
323                if (getListInfoParam() == DISCOVERED_PARAM && getClass() != MapMarkerIntel.class) {
324                        prefix = "Discovered: ";
325                        if (discoveredPrefixOverride != null) {
326                                prefix = discoveredPrefixOverride;
327                        }
328                }
329                return prefix + title;
330        }
331
332        @Override
333        public FactionAPI getFactionForUIColors() {
334                return super.getFactionForUIColors();
335        }
336
337        public String getSmallDescriptionTitle() {
338                return getName();
339        }
340
341        @Override
342        public SectorEntityToken getMapLocation(SectorMapAPI map) {
343//              if (copy != null && copy.getStarSystem() != null) {
344//                      return copy.getStarSystem().getCenter();
345//              } else if (copy != null) {
346//                      return copy;
347//              }
348                if (copy != null) return copy;
349                return entity;
350        }
351
352        @Override
353        public boolean shouldRemoveIntel() {
354                return super.shouldRemoveIntel();
355        }
356        
357        public boolean isHidden() {
358                return hidden != null;  
359        }
360
361        
362}