001package com.fs.starfarer.api.impl.campaign.intel;
002
003import java.awt.Color;
004import java.util.Set;
005
006import org.lwjgl.input.Keyboard;
007
008import com.fs.starfarer.api.Global;
009import com.fs.starfarer.api.campaign.FactionAPI;
010import com.fs.starfarer.api.campaign.InteractionDialogAPI;
011import com.fs.starfarer.api.campaign.SectorEntityToken;
012import com.fs.starfarer.api.campaign.TextPanelAPI;
013import com.fs.starfarer.api.campaign.econ.CommodityOnMarketAPI;
014import com.fs.starfarer.api.campaign.econ.MonthlyReport;
015import com.fs.starfarer.api.campaign.econ.MonthlyReport.FDNode;
016import com.fs.starfarer.api.campaign.listeners.EconomyTickListener;
017import com.fs.starfarer.api.impl.campaign.ids.Factions;
018import com.fs.starfarer.api.impl.campaign.ids.Tags;
019import com.fs.starfarer.api.impl.campaign.intel.events.SindrianDiktatHostileActivityFactor;
020import com.fs.starfarer.api.impl.campaign.rulecmd.HA_CMD;
021import com.fs.starfarer.api.impl.campaign.shared.SharedData;
022import com.fs.starfarer.api.ui.ButtonAPI;
023import com.fs.starfarer.api.ui.IntelUIAPI;
024import com.fs.starfarer.api.ui.LabelAPI;
025import com.fs.starfarer.api.ui.SectorMapAPI;
026import com.fs.starfarer.api.ui.TooltipMakerAPI;
027import com.fs.starfarer.api.ui.TooltipMakerAPI.TooltipCreator;
028import com.fs.starfarer.api.util.Misc;
029
030public class SindrianDiktatFuelDeal extends BaseIntelPlugin implements EconomyTickListener {
031        
032        public static enum AgreementEndingType {
033                BROKEN,
034        }
035        
036        public static float REP_FOR_BREAKING_DEAL = 0.5f;
037        
038        public static String KEY = "$sindrianDiktatDeal_ref";
039        public static SindrianDiktatFuelDeal get() {
040                return (SindrianDiktatFuelDeal) Global.getSector().getMemoryWithoutUpdate().get(KEY);
041        }
042        
043        public static String BUTTON_END = "End";
044        
045        public static String UPDATE_PARAM_ACCEPTED = "update_param_accepted";
046        
047        protected FactionAPI faction = null;
048        protected AgreementEndingType endType = null;
049        
050        public SindrianDiktatFuelDeal(InteractionDialogAPI dialog) {
051                this.faction = Global.getSector().getFaction(Factions.DIKTAT);
052                
053                setImportant(true);
054                SindrianDiktatHostileActivityFactor.setMadeDeal(true);
055                
056                TextPanelAPI text = null;
057                if (dialog != null) text = dialog.getTextPanel();
058                
059                Global.getSector().getListenerManager().addListener(this);
060                Global.getSector().getMemoryWithoutUpdate().set(KEY, this);
061                
062                Global.getSector().getIntelManager().addIntel(this, true);
063                
064                sendUpdate(UPDATE_PARAM_ACCEPTED, text);
065                
066                HA_CMD.avertOrEndDiktatAttackAsNecessary();
067        }
068        
069        @Override
070        protected void notifyEnding() {
071                super.notifyEnding();
072                
073                SindrianDiktatHostileActivityFactor.setMadeDeal(false);
074                Global.getSector().getListenerManager().removeListener(this);
075                Global.getSector().getMemoryWithoutUpdate().unset(KEY);
076        }
077        
078        @Override
079        protected void notifyEnded() {
080                super.notifyEnded();
081        }
082
083        protected Object readResolve() {
084                return this;
085        }
086        
087        public String getBaseName() {
088                return "Sindrian Diktat Fuel Accord";
089        }
090
091        public String getAcceptedPostfix() {
092                return "Accepted";
093        }
094                
095        public String getBrokenPostfix() {
096                return "Ended";
097
098        }
099        
100        public String getName() {
101                String postfix = "";
102                if (isEnding() && endType != null) {
103                        switch (endType) {
104                        case BROKEN:
105                                postfix = " - " + getBrokenPostfix();
106                                break;
107                        }
108                }
109                if (isSendingUpdate() && getListInfoParam() == UPDATE_PARAM_ACCEPTED) {
110                        postfix =  " - " + getAcceptedPostfix();
111                }
112                return getBaseName() + postfix;
113        }
114        
115        @Override
116        public FactionAPI getFactionForUIColors() {
117                return faction;
118        }
119
120        public String getSmallDescriptionTitle() {
121                return getName();
122        }
123        
124        protected void addBulletPoints(TooltipMakerAPI info, ListInfoMode mode) {
125                Color h = Misc.getHighlightColor();
126                Color g = Misc.getGrayColor();
127                float pad = 3f;
128                float opad = 10f;
129                
130                float initPad = pad;
131                if (mode == ListInfoMode.IN_DESC) initPad = opad;
132                
133                Color tc = getBulletColorForMode(mode);
134                
135                bullet(info);
136                boolean isUpdate = getListInfoParam() != null;
137                
138//              if (getListInfoParam() == UPDATE_PARAM_ACCEPTED) {
139//                      return;
140//              }
141                
142                
143        
144                unindent(info);
145        }
146        
147        
148        public void createSmallDescription(TooltipMakerAPI info, float width, float height) {
149                Color h = Misc.getHighlightColor();
150                Color g = Misc.getGrayColor();
151                Color tc = Misc.getTextColor();
152                float pad = 3f;
153                float opad = 10f;
154
155                info.addImage(getFaction().getLogo(), width, 128, opad);
156                
157                if (isEnding() || isEnded()) {
158                        info.addPara("You are no longer abiding by your agreement with the Sindrian Diktat.", opad);
159                        return;
160                }
161                
162                float feeFraction = Global.getSettings().getFloat("diktatFuelFeeFraction");
163                
164                LabelAPI label = info.addPara("You've made an agreement with the Sindrian Diktat, paying a sizeable "
165                                + "fee, %s of your gross fuel exports, in exchange for them not bombarding "
166                                + "your fuel production facilities from orbit.", 
167                                opad, h, "" + (int) Math.round(feeFraction * 100f) + "%");
168                label.setHighlight("Sindrian Diktat", "" + (int) Math.round(feeFraction * 100f) + "%");
169                label.setHighlightColors(faction.getBaseUIColor(), h);
170                
171                info.addPara("You can end this agreement, but there "
172                                + "would be no possibility of re-negotiating a similar one in the future.", opad);
173        
174                ButtonAPI button = info.addButton("End the agreement", BUTTON_END, 
175                                getFactionForUIColors().getBaseUIColor(), getFactionForUIColors().getDarkUIColor(),
176                                (int)(width), 20f, opad * 1f);
177                button.setShortcut(Keyboard.KEY_U, true);
178                
179        }
180        
181        
182        public String getIcon() {
183                return faction.getCrest();
184        }
185        
186        public Set<String> getIntelTags(SectorMapAPI map) {
187                Set<String> tags = super.getIntelTags(map);
188                tags.add(Tags.INTEL_AGREEMENTS);
189                tags.add(faction.getId());
190                return tags;
191        }
192        
193        @Override
194        public String getImportantIcon() {
195                return Global.getSettings().getSpriteName("intel", "important_accepted_mission");
196        }
197
198        @Override
199        public SectorEntityToken getMapLocation(SectorMapAPI map) {
200                return null;
201        }
202
203        
204        public FactionAPI getFaction() {
205                return faction;
206        }
207        
208        public void endAgreement(AgreementEndingType type, InteractionDialogAPI dialog) {
209                if (!isEnded() && !isEnding()) {
210                        endType = type;
211                        setImportant(false);
212                        endImmediately();
213                        
214                        if (dialog != null) {
215                                sendUpdate(new Object(), dialog.getTextPanel());
216                        }
217
218                        if (type == AgreementEndingType.BROKEN) {
219                                SindrianDiktatHostileActivityFactor.setBrokeDeal(true);
220                                
221                                //Misc.incrUntrustwortyCount();
222                                
223                                TextPanelAPI text = dialog == null ? null : dialog.getTextPanel();
224                                Misc.adjustRep(Factions.DIKTAT, -REP_FOR_BREAKING_DEAL, text);
225                        }
226                }
227        }
228        
229        
230        @Override
231        public void buttonPressConfirmed(Object buttonId, IntelUIAPI ui) {
232                if (buttonId == BUTTON_END) {
233                        endAgreement(AgreementEndingType.BROKEN, null);
234                }
235                super.buttonPressConfirmed(buttonId, ui);
236        }
237
238
239        @Override
240        public void createConfirmationPrompt(Object buttonId, TooltipMakerAPI prompt) {
241                if (buttonId == BUTTON_END) {
242                        prompt.addPara("The terms of the agreement are so onerous that your reputation at large "
243                                        + "would not suffer for unilaterally breaking it, "
244                                        + "though your standing with the Sindrian Diktat specifically "
245                                        + "would of course be affected.", 0f,
246                                        faction.getBaseUIColor(), faction.getDisplayName());                            
247                }
248                        
249        }
250        
251        @Override
252        public boolean doesButtonHaveConfirmDialog(Object buttonId) {
253                if (buttonId == BUTTON_END) {
254                        return true;
255                }
256                return super.doesButtonHaveConfirmDialog(buttonId);
257        }
258        
259        public void reportEconomyMonthEnd() {
260                
261        }
262
263        public void reportEconomyTick(int iterIndex) {
264                int numIter = Global.getSettings().getInt("economyIterPerMonth");
265                if (iterIndex != numIter - 1) return;
266                
267                float feeFraction = Global.getSettings().getFloat("diktatFuelFeeFraction");
268                
269                MonthlyReport report = SharedData.getData().getCurrentReport();
270                FDNode marketsNode = report.getNode(MonthlyReport.OUTPOSTS);
271                if (marketsNode == null) return;
272                
273                float exportIncome = computeFuelExportIncome(marketsNode);
274                int credits = (int) (exportIncome * feeFraction);
275                
276                if (credits <= 0) return;
277
278                FDNode node = getMonthlyReportNode();
279                node.upkeep += credits;
280        }
281        
282        public float computeFuelExportIncome(FDNode curr) {
283                float total = 0f;
284                if (curr.custom instanceof CommodityOnMarketAPI) {
285                        CommodityOnMarketAPI com = (CommodityOnMarketAPI) curr.custom;
286                        if (com.isFuel()) {
287                                total += curr.income;
288                        }
289                }
290                for (FDNode child : curr.getChildren().values()) {
291                        total += computeFuelExportIncome(child);
292                }
293                return total;
294                
295        }
296        
297        public FDNode getMonthlyReportNode() {
298                MonthlyReport report = SharedData.getData().getCurrentReport();
299                FDNode marketsNode = report.getNode(MonthlyReport.OUTPOSTS);
300                if (marketsNode.name == null) {
301                        marketsNode.name = "Colonies";
302                        marketsNode.custom = MonthlyReport.OUTPOSTS;
303                        marketsNode.tooltipCreator = report.getMonthlyReportTooltip();
304                }
305                
306                FDNode paymentNode = report.getNode(marketsNode, "diktat_fuel_fees"); 
307                paymentNode.name = "Sindrian Diktat fuel accord fees";
308                paymentNode.icon = faction.getCrest();
309                
310                if (paymentNode.tooltipCreator == null) {
311                        paymentNode.tooltipCreator = new TooltipCreator() {
312                                public boolean isTooltipExpandable(Object tooltipParam) {
313                                        return false;
314                                }
315                                public float getTooltipWidth(Object tooltipParam) {
316                                        return 450;
317                                }
318                                public void createTooltip(TooltipMakerAPI tooltip, boolean expanded, Object tooltipParam) {
319                                        tooltip.addPara("Fees paid to the Sindrian Diktat in exchange for their refraining "
320                                                        + "from interfering in your fuel production business. From orbit.", 0f);
321                                }
322                        };
323                }
324                
325                return paymentNode;
326        }
327        
328        
329}
330
331
332
333
334
335