001package com.fs.starfarer.api.campaign.impl.items;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.Random;
006
007import java.awt.Color;
008
009import org.json.JSONException;
010
011import com.fs.starfarer.api.Global;
012import com.fs.starfarer.api.campaign.CargoStackAPI;
013import com.fs.starfarer.api.campaign.CargoTransferHandlerAPI;
014import com.fs.starfarer.api.campaign.SpecialItemPlugin;
015import com.fs.starfarer.api.campaign.SpecialItemSpecAPI;
016import com.fs.starfarer.api.campaign.SubmarketPlugin.TransferAction;
017import com.fs.starfarer.api.campaign.econ.MarketAPI;
018import com.fs.starfarer.api.campaign.econ.SubmarketAPI;
019import com.fs.starfarer.api.combat.ShipHullSpecAPI;
020import com.fs.starfarer.api.impl.campaign.ids.Tags;
021import com.fs.starfarer.api.loading.FighterWingSpecAPI;
022import com.fs.starfarer.api.loading.WeaponSpecAPI;
023import com.fs.starfarer.api.ui.LabelAPI;
024import com.fs.starfarer.api.ui.TooltipMakerAPI;
025import com.fs.starfarer.api.util.Highlights;
026import com.fs.starfarer.api.util.Misc;
027
028public class BaseSpecialItemPlugin implements SpecialItemPlugin {
029
030        protected SpecialItemSpecAPI spec;
031        protected CargoStackAPI stack;
032        protected String itemId;
033        
034        
035        public String getId() {
036                return itemId;
037        }
038        public void setId(String id) {
039                this.itemId = id;
040                spec = Global.getSettings().getSpecialItemSpec(id);
041        }
042        public void init(CargoStackAPI stack) {
043                this.stack = stack;
044        }
045
046        @Override
047        public void performRightClickAction(RightClickActionHelper helper) {
048                performRightClickAction();
049        }
050        public void performRightClickAction() {
051                
052        }
053        
054        public boolean hasRightClickAction() {
055                return false;
056        }
057
058        public boolean shouldRemoveOnRightClickAction() {
059                return true;
060        }
061        
062        
063        public void createTooltip(TooltipMakerAPI tooltip, boolean expanded, CargoTransferHandlerAPI transferHandler, Object stackSource) {
064                boolean useGray = true;
065                if (getClass() == BaseSpecialItemPlugin.class || Global.CODEX_TOOLTIP_MODE) {
066                        useGray = false;
067                }
068                createTooltip(tooltip, expanded, transferHandler, stackSource, useGray);
069        }
070        public void createTooltip(TooltipMakerAPI tooltip, boolean expanded, CargoTransferHandlerAPI transferHandler, Object stackSource, boolean useGray) {
071                float opad = 10f;
072                
073                if (!Global.CODEX_TOOLTIP_MODE) {
074                        tooltip.addTitle(getName());
075                } else {
076                        tooltip.addSpacer(-opad);
077                }
078                
079                String design = getDesignType();
080                Misc.addDesignTypePara(tooltip, design, opad);
081                
082                if (Global.CODEX_TOOLTIP_MODE) {
083                        tooltip.setParaSmallInsignia();
084                }
085                if (!spec.getDesc().isEmpty()) {
086                        Color c = Misc.getTextColor();
087                        if (useGray) c = Misc.getGrayColor();
088                        tooltip.addPara(spec.getDesc(), c, opad);
089                }
090                
091                if (getClass() == BaseSpecialItemPlugin.class) {
092                        addCostLabel(tooltip, opad, transferHandler, stackSource);
093                }
094        }
095
096        public float getTooltipWidth() {
097                return 450;
098        }
099
100        public boolean isTooltipExpandable() {
101                return false;
102        }
103
104        public String getName() {
105                return spec.getName();
106        }
107
108        public int getPrice(MarketAPI market, SubmarketAPI submarket) {
109                if (spec != null) return (int) spec.getBasePrice();
110                return 0;
111        }
112
113        protected float getItemPriceMult() {
114                return Global.getSettings().getFloat("blueprintPriceOriginalItemMult");
115        }
116        
117        public void render(float x, float y, float w, float h, float alphaMult, float glowMult, SpecialItemRendererAPI renderer) {
118                
119        }
120
121        
122        
123        protected void addCostLabel(TooltipMakerAPI tooltip, float pad, CargoTransferHandlerAPI transferHandler, Object stackSource) {
124                if (stack == null) return;
125                
126                //if (Global.CODEX_TOOLTIP_MODE) return;
127                if (Global.CODEX_TOOLTIP_MODE) {
128                        int cost = (int) stack.getBaseValuePerUnit();
129                        tooltip.setParaFontDefault();
130                        tooltip.addPara("Base value: %s", pad, Misc.getGrayColor(), Misc.getHighlightColor(), Misc.getDGSCredits(cost));
131                        return;
132                        //tooltip.setParaSmallInsignia();
133                }
134                
135                ItemCostLabelData data = getCostLabelData(stack, transferHandler, stackSource);
136                
137                LabelAPI label = tooltip.addPara(data.text, pad);
138                if (data.highlights != null) {
139                        label.setHighlight(data.highlights.getText());
140                        label.setHighlightColors(data.highlights.getColors());
141                }
142        }
143        
144        
145        public static class ItemCostLabelData {
146                public String text;
147                public Highlights highlights;
148        }
149        
150        protected ItemCostLabelData getCostLabelData(CargoStackAPI stack, CargoTransferHandlerAPI transferHandler, Object stackSource) {
151                String text = "";
152                String highlight = null;
153                Highlights highlights = null;
154                Color highlightColor = Misc.getHighlightColor();
155                TransferAction action = TransferAction.PLAYER_BUY;
156                if (transferHandler != null && stackSource == transferHandler.getManifestOne()) {
157                        action = TransferAction.PLAYER_SELL;
158                }
159                
160                
161                if (action == TransferAction.PLAYER_SELL && stack.getSpecialItemSpecIfSpecial() != null) {
162                        SpecialItemSpecAPI spec = stack.getSpecialItemSpecIfSpecial();
163                        if (spec.hasTag(Tags.MISSION_ITEM)) {
164                                text = "Can not remove item";
165                                highlight = text;
166                                highlights = new Highlights();
167                                highlights.append(text, Misc.getNegativeHighlightColor());
168                                
169                                ItemCostLabelData data = new ItemCostLabelData();
170                                data.text = text;// + ".";
171                                data.highlights = highlights;
172                                
173                                return data;
174                        }
175                }
176                
177                
178                
179                if (transferHandler != null && transferHandler.getSubmarketTradedWith() != null &&
180                                transferHandler.getSubmarketTradedWith().isIllegalOnSubmarket(stack, action)) {
181                        highlightColor = Misc.getNegativeHighlightColor();
182                        //text = "Illegal to trade on the " + transferHandler.getSubmarketTradedWith().getNameOneLine() + " here";
183                        text = transferHandler.getSubmarketTradedWith().getPlugin().getIllegalTransferText(stack, action);
184                        highlight = text;
185                        highlights = transferHandler.getSubmarketTradedWith().getPlugin().getIllegalTransferTextHighlights(stack, TransferAction.PLAYER_BUY);
186                } else {
187                        if (stackSource != null && transferHandler != null && !transferHandler.isNoCost()) {
188                                if (stackSource == transferHandler.getManifestOne()) {
189                                        int cost = (int)transferHandler.computeCurrentSingleItemSellCost(stack);
190                                        //text = "Sells for: " + Misc.getWithDGS(cost) + " credits per unit";
191                                        text = "Sells for: " + Misc.getDGSCredits(cost) + " per unit.";
192                                        highlight = "" + Misc.getDGSCredits(cost);
193                                } else {
194                                        int cost = (int)transferHandler.computeCurrentSingleItemBuyCost(stack);
195                                        //text = "Price: " + Misc.getWithDGS(cost) + " credits per unit";
196                                        text = "Price: " + Misc.getDGSCredits(cost) + " per unit.";
197                                        highlight = "" + Misc.getDGSCredits(cost);
198                                }
199                        } else {
200                                int cost = (int) stack.getBaseValuePerUnit();
201                                //float mult = Global.getSettings().getFloat("nonEconItemSellPriceMult");
202                                //cost *= mult;
203                                //text = "Base value: " + Misc.getWithDGS(cost) + " credits per unit";
204                                text = "Base value: " + Misc.getDGSCredits(cost) + " per unit.";
205                                highlight = "" + Misc.getDGSCredits(cost);
206                        }
207                }
208                
209                if (highlights == null) {
210                        highlights = new Highlights();
211                        highlights.setText(highlight);
212                        highlights.setColors(highlightColor);
213                }
214                
215                ItemCostLabelData data = new ItemCostLabelData();
216                data.text = text;// + ".";
217                data.highlights = highlights;
218                
219                return data;
220        }
221
222        public String resolveDropParamsToSpecificItemData(String params, Random random) throws JSONException {
223                return "";
224        }
225        
226        
227        
228        
229        protected static interface BlueprintLister {
230                boolean isKnown(String id);
231                String getNoun(int num);
232                String getName(String id);
233        }
234        
235        protected void addShipList(TooltipMakerAPI tooltip, String title, List<String> hulls, int max, float opad) {
236                addBlueprintList(tooltip, title, hulls, max, opad, new BlueprintLister(){
237                        public boolean isKnown(String id) {
238                                if (Global.CODEX_TOOLTIP_MODE) return false;
239                                return Global.getSector().getPlayerFaction().knowsShip(id);
240                        }
241                        public String getNoun(int num) {
242                                if (num == 1) return "hull";
243                                return "hulls";
244                        }
245                        public String getName(String id) {
246                                ShipHullSpecAPI spec = Global.getSettings().getHullSpec(id);
247                                return spec.getNameWithDesignationWithDashClass();
248                        }
249                });
250        }
251        
252        protected void addWeaponList(TooltipMakerAPI tooltip, String title, List<String> weapons, int max, float opad) {
253                addBlueprintList(tooltip, title, weapons, max, opad, new BlueprintLister(){
254                        public boolean isKnown(String id) {
255                                if (Global.CODEX_TOOLTIP_MODE) return false;
256                                return Global.getSector().getPlayerFaction().knowsWeapon(id);
257                        }
258                        public String getNoun(int num) {
259                                if (num == 1) return "weapon";
260                                return "weapons";
261                        }
262                        public String getName(String id) {
263                                WeaponSpecAPI spec = Global.getSettings().getWeaponSpec(id);
264                                return spec.getWeaponName();
265                        }
266                });
267        }
268        
269        protected void addFighterList(TooltipMakerAPI tooltip, String title, List<String> wings, int max, float opad) {
270                addBlueprintList(tooltip, title, wings, max, opad, new BlueprintLister(){
271                        public boolean isKnown(String id) {
272                                if (Global.CODEX_TOOLTIP_MODE) return false;
273                                return Global.getSector().getPlayerFaction().knowsFighter(id);
274                        }
275                        public String getNoun(int num) {
276                                if (num == 1) return "fighter LPC";
277                                return "fighter LPCs";
278                        }
279                        public String getName(String id) {
280                                FighterWingSpecAPI spec = Global.getSettings().getFighterWingSpec(id);
281                                return spec.getWingName();
282                        }
283                });
284        }
285        
286        protected void addBlueprintList(TooltipMakerAPI tooltip, String title, List<String> ids, int max, float opad,
287                                BlueprintLister lister) {
288                
289                Color b = Misc.getButtonTextColor();
290                Color g = Misc.getGrayColor();
291                
292                if (Global.CODEX_TOOLTIP_MODE) {
293                        tooltip.setParaSmallInsignia();
294                }
295                tooltip.addPara(title, opad);
296                if (Global.CODEX_TOOLTIP_MODE) {
297                        tooltip.setParaFontDefault();
298                }
299                
300                String tab = "        ";
301                float small = 5f;
302                float pad = small;
303                
304                int left = ids.size();
305                
306                List<String> copy = new ArrayList<String>();
307                for (String id : ids) {
308                        if (!lister.isKnown(id)) copy.add(id);
309                }
310                for (String id : ids) {
311                        if (lister.isKnown(id)) copy.add(id);
312                }
313                
314                ids = copy;
315                for (String id : ids) {
316                        boolean known = lister.isKnown(id);
317                        
318                        if (known) {
319                                tooltip.addPara(tab + lister.getName(id) + " (known)", g, pad);
320                        } else {
321                                tooltip.addPara(tab + lister.getName(id), b, pad);
322                        }
323                        left--;
324                        pad = 3f;
325                        if (ids.size() - left >= max - 1) break;
326                }
327                if (ids.isEmpty()) {
328                        tooltip.addPara(tab + "None", pad);
329                }
330                if (left > 0) {
331                        String noun = lister.getNoun(left);
332                        tooltip.addPara(tab + "... and %s other " + noun + "", pad, Misc.getHighlightColor(), "" + left);
333                }
334        }
335        public String getDesignType() {
336                return spec.getManufacturer();
337        }
338        public SpecialItemSpecAPI getSpec() {
339                return spec;
340        }
341        
342//      @Override
343//      public int getNumToRemove() {
344//              return 1;
345//      }
346//      
347//      @Override
348//      public boolean shouldRemoveCalledAfterRightClickAction() {
349//              return false;
350//      }
351//      @Override
352//      public void setReadOnlyCargoForRightClickAction(CargoAPI cargo) {
353//              // TODO Auto-generated method stub
354//              
355//      }
356        
357}
358
359
360
361
362
363
364