001package com.fs.starfarer.api.impl.campaign.intel.misc;
002
003import java.awt.Color;
004import java.util.ArrayList;
005import java.util.List;
006import java.util.Set;
007
008import org.lwjgl.input.Keyboard;
009
010import com.fs.starfarer.api.Global;
011import com.fs.starfarer.api.campaign.FactionAPI;
012import com.fs.starfarer.api.campaign.SectorEntityToken;
013import com.fs.starfarer.api.campaign.TextPanelAPI;
014import com.fs.starfarer.api.campaign.comm.IntelInfoPlugin;
015import com.fs.starfarer.api.campaign.econ.MarketAPI;
016import com.fs.starfarer.api.impl.campaign.ids.Factions;
017import com.fs.starfarer.api.impl.campaign.ids.Tags;
018import com.fs.starfarer.api.impl.campaign.intel.BaseIntelPlugin;
019import com.fs.starfarer.api.impl.campaign.missions.luddic.LuddicPilgrimsPath;
020import com.fs.starfarer.api.loading.Description;
021import com.fs.starfarer.api.loading.Description.Type;
022import com.fs.starfarer.api.ui.ButtonAPI;
023import com.fs.starfarer.api.ui.IntelUIAPI;
024import com.fs.starfarer.api.ui.SectorMapAPI;
025import com.fs.starfarer.api.ui.TooltipMakerAPI;
026import com.fs.starfarer.api.util.Misc;
027
028public class LuddicShrineIntel extends BaseIntelPlugin {
029
030        public static String VISITED = "$visitedShrine";
031
032        
033        protected SectorEntityToken entity;
034        
035        public static void addShrineIntelIfNeeded(String id, TextPanelAPI text) {
036                addShrineIntelIfNeeded(getEntity(id), text);
037        }
038        public static void addShrineIntelIfNeeded(SectorEntityToken entity, TextPanelAPI text) {
039                addShrineIntelIfNeeded(entity, text, false);
040        }
041        public static void addShrineIntelIfNeeded(SectorEntityToken entity, TextPanelAPI text, boolean quiet) {
042                if (getShrineIntel(entity) == null) {
043                        LuddicShrineIntel intel = new LuddicShrineIntel(entity);
044                        Global.getSector().getIntelManager().addIntel(intel, quiet, text);
045                }
046        }
047        
048        public static void setVisited(SectorEntityToken entity, TextPanelAPI text) {
049                if (isVisited(entity)) return;
050                
051                entity.getMemoryWithoutUpdate().set(VISITED, true);
052                if (text != null) {
053                        LuddicShrineIntel intel = getShrineIntel(entity);
054                        if (intel != null) {
055                                Global.getSector().getIntelManager().addIntelToTextPanel(intel, text);
056                        }
057                }
058        }
059        
060        public static boolean isVisited(SectorEntityToken entity) {
061                return entity.getMemoryWithoutUpdate().getBoolean(VISITED) ||
062                                (entity.getMarket() != null && 
063                                entity.getMarket().getMemoryWithoutUpdate().getBoolean(VISITED));
064        }
065        
066        
067        public static LuddicShrineIntel getShrineIntel(SectorEntityToken entity) {
068                for (IntelInfoPlugin intel : Global.getSector().getIntelManager().getIntel(LuddicShrineIntel.class)) {
069                        if (((LuddicShrineIntel)intel).getEntity() == entity) return (LuddicShrineIntel)intel;
070                }
071                return null;
072        }
073        
074        public static SectorEntityToken getEntity(String id) {
075                MarketAPI market = Global.getSector().getEconomy().getMarket(id);
076                if (market != null) {
077                        if (market.getPlanetEntity() != null) {
078                                return market.getPlanetEntity();
079                        }
080                        return market.getPrimaryEntity();
081                } else {
082                        return Global.getSector().getEntityById(id);
083                }
084        }
085        
086        
087        public LuddicShrineIntel(SectorEntityToken entity) {
088                this.entity = entity;
089        }
090        
091        
092        protected void addBulletPoints(TooltipMakerAPI info, ListInfoMode mode) {
093                
094                Color h = Misc.getHighlightColor();
095                Color g = Misc.getGrayColor();
096                float pad = 3f;
097                float opad = 10f;
098                
099                float initPad = pad;
100                if (mode == ListInfoMode.IN_DESC) initPad = opad;
101                
102                Color tc = getBulletColorForMode(mode);
103                
104                bullet(info);
105                boolean isUpdate = getListInfoParam() != null;
106                
107//              if (mode == ListInfoMode.INTEL) {
108//                      info.addPara("Located in the " + gate.getContainingLocation().getNameWithLowercaseTypeShort(), tc, initPad);
109//                      initPad = 0f;
110//              }
111                
112                boolean visited = isVisited(entity);
113                
114                if (visited) {
115                        Color c = Global.getSector().getFaction(Factions.LUDDIC_CHURCH).getBaseUIColor();
116                        info.addPara("Visited", c, initPad);
117                        initPad = 0f;
118                } else {
119                        info.addPara("Not visited", tc, initPad);
120                        initPad = 0f;
121                }
122                
123                if (mode == ListInfoMode.INTEL) {
124                        // not sure if this is needed
125                        //info.addPara("In the " + entity.getContainingLocation().getNameWithLowercaseType(), tc, 0f);
126                }
127                
128                unindent(info);
129        }
130        
131        
132        @Override
133        public void createIntelInfo(TooltipMakerAPI info, ListInfoMode mode) {
134                String pre = "";
135                String post = "";
136                if (mode == ListInfoMode.MESSAGES) {
137                        pre = "Discovered: ";
138                }
139                if (mode == ListInfoMode.INTEL) {
140                        //post = " - " + entity.getContainingLocation().getNameWithTypeShort();
141                }
142                
143                Color c = getTitleColor(mode);
144                info.addPara(pre + getName() + post, c, 0f);
145                addBulletPoints(info, mode);
146        }
147        
148        @Override
149        public void createSmallDescription(TooltipMakerAPI info, float width, float height) {
150                Color h = Misc.getHighlightColor();
151                Color g = Misc.getGrayColor();
152                Color tc = Misc.getTextColor();
153                float pad = 3f;
154                float opad = 10f;
155                
156                String id = entity.getId();
157                if (entity.getMarket() != null && !entity.getMarket().isPlanetConditionMarketOnly()) {
158                        id = entity.getMarket().getId();
159                }
160                
161                boolean visited = isVisited(entity);
162                
163                if (id.equals("jangala")) {
164                        if (visited) {
165                                info.addImage(Global.getSettings().getSpriteName("illustrations", "jangala_shrine"), width, opad);
166                        }
167                        Description desc = Global.getSettings().getDescription("shrine_jangala", Type.CUSTOM);
168                        info.addPara(desc.getText1(), opad);
169                } else if (id.equals("volturn")) {
170                        if (visited) {
171                                info.addImage(Global.getSettings().getSpriteName("illustrations", "volturn_shrine_fake"), width, opad);
172                        }
173                        Description desc = Global.getSettings().getDescription("shrine_volturn", Type.CUSTOM);
174                        info.addPara(desc.getText1(), opad);
175                } else if (id.equals("hesperus")) {
176                        if (visited) {
177                                info.addImage(Global.getSettings().getSpriteName("illustrations", "hesperus_shrine"), width, opad);
178                        }
179                        Description desc = Global.getSettings().getDescription("shrine_hesperus", Type.CUSTOM);
180                        info.addPara(desc.getText1(), opad);
181                } else if (id.equals("gilead")) {
182                        if (visited) {
183                                info.addImage(Global.getSettings().getSpriteName("illustrations", "gilead"), width, opad);
184                        }
185                        Description desc = Global.getSettings().getDescription("shrine_gilead", Type.CUSTOM);
186                        info.addPara(desc.getText1(), opad);
187                } else if (id.equals("beholder_station")) {
188                        if (visited) {
189                                info.addImage(Global.getSettings().getSpriteName("illustrations", "luddic_shrine"), width, opad);
190                                //info.addImage(entity.getCustomInteractionDialogImageVisual().getSpriteName(), width, opad);
191                        }
192                        
193                        // this is probably fine for Beholder Station, but would just get the market description for
194                        // shrines that are on actual colonies
195                        Description desc = Global.getSettings().getDescription("shrine_beholder", Type.CUSTOM);
196                        info.addPara(desc.getText1(), opad);
197                        
198                } else if (id.equals("killa")) {
199                        if (visited) {
200                                info.addImage(Global.getSettings().getSpriteName("illustrations", "killa_shrine"), width, opad);
201                                //info.addImage(entity.getCustomInteractionDialogImageVisual().getSpriteName(), width, opad);
202                        }
203                        Description desc = Global.getSettings().getDescription("shrine_killa", Type.CUSTOM);
204                        info.addPara(desc.getText1(), opad);
205                }
206        
207                
208//              String buttonText = "Show shrines";
209//              if (info.getIntelUI().isShowingCustomIntelSubset()) {
210//                      buttonText = "Go back";
211//              }
212//              
213//              info.addSpacer(height - info.getHeightSoFar() - 20f - 20f);
214//              ButtonAPI button = addGenericButton(info, width, buttonText, BUTTON_SHOW_SHRINES);
215//              button.setShortcut(Keyboard.KEY_T, true);
216                addShowShrinesButton(this, width, height, info);
217                
218                //addBulletPoints(info, ListInfoMode.IN_DESC);
219                
220        }
221
222        @Override
223        public String getIcon() {
224                return Global.getSettings().getSpriteName("intel", "luddic_shrine");
225        }
226        
227        @Override
228        public Set<String> getIntelTags(SectorMapAPI map) {
229                Set<String> tags = super.getIntelTags(map);
230                tags.add(Tags.INTEL_SHRINES);
231                return tags;
232        }
233        
234        public String getSortString() {
235                return getName();
236        }
237        
238        public String getName() {
239//              String onOrAt = "on";
240//              if (entity.getMarket() != null && !entity.getMarket().isPlanetConditionMarketOnly()) {
241//                      onOrAt = entity.getMarket().getOnOrAt();
242//              }
243//              return "Luddic Shrine " + onOrAt + " " + entity.getName();
244                return "Luddic Shrine - " + entity.getName();
245        }
246        
247        @Override
248        public FactionAPI getFactionForUIColors() {
249                return Global.getSector().getFaction(Factions.LUDDIC_CHURCH);
250        }
251
252        public String getSmallDescriptionTitle() {
253                //return getName() + " - " + entity.getContainingLocation().getNameWithTypeShort();
254                return getName();
255        }
256
257        @Override
258        public SectorEntityToken getMapLocation(SectorMapAPI map) {
259                return entity;
260        }
261        
262        @Override
263        public boolean shouldRemoveIntel() {
264                return false;
265        }
266
267        @Override
268        public String getCommMessageSound() {
269                return "ui_discovered_entity";
270        }
271
272        public SectorEntityToken getEntity() {
273                return entity;
274        }
275        
276        
277        public static String BUTTON_SHOW_SHRINES = "button_show_shrines";
278        public void buttonPressConfirmed(Object buttonId, IntelUIAPI ui) {
279                if (buttonId == BUTTON_SHOW_SHRINES) {
280                        toggleShrineList(this, ui);
281                        return;
282                }
283                super.buttonPressConfirmed(buttonId, ui);
284        }
285        
286        public static void addShowShrinesButton(IntelInfoPlugin curr, float width, float height, TooltipMakerAPI info) {
287                if (!Global.getSector().getIntelManager().hasIntelOfClass(LuddicPilgrimsPath.class)) return;
288                
289                if (!info.getIntelUI().isShowingCustomIntelSubset() && 
290                                curr instanceof LuddicShrineIntel) {
291                        return;
292                }
293                
294                String buttonText = "Show shrines";
295                if (info.getIntelUI().isShowingCustomIntelSubset()) {
296                        buttonText = "Go back";
297                }
298                
299                info.addSpacer(height - info.getHeightSoFar() - 20f - 20f);
300                ButtonAPI button = ((BaseIntelPlugin)curr).addGenericButton(info, width, buttonText, BUTTON_SHOW_SHRINES);
301                button.setShortcut(Keyboard.KEY_T, true);
302        }
303        
304        public static void toggleShrineList(IntelInfoPlugin curr, IntelUIAPI ui) {
305                if (ui.isShowingCustomIntelSubset()) {
306                        ui.updateIntelList(true);
307                        ui.updateUIForItem(curr);
308                        //for (IntelInfoPlugin intel : Global.getSector().getIntelManager().getIntel(LuddicShrineIntel.class)) {
309                        for (IntelInfoPlugin intel : Global.getSector().getIntelManager().getIntel(LuddicPilgrimsPath.class)) {
310                                ui.selectItem(intel);
311                                break;
312                        }
313                } else {
314                        List<IntelInfoPlugin> show = new ArrayList<IntelInfoPlugin>();
315                        for (IntelInfoPlugin intel : Global.getSector().getIntelManager().getIntel(LuddicPilgrimsPath.class)) {
316                                show.add(intel);
317                        }
318                        for (IntelInfoPlugin intel : Global.getSector().getIntelManager().getIntel(LuddicShrineIntel.class)) {
319                                show.add(intel);
320                        }
321                        Global.getSector().getIntelManager().sortIntel(show);
322                        ui.updateIntelList(true, show);
323                        ui.updateUIForItem(curr);
324                }
325                return;
326        }
327        
328}