001package com.fs.starfarer.api.impl.campaign.econ;
002
003import com.fs.starfarer.api.Global;
004import com.fs.starfarer.api.campaign.econ.MarketAPI;
005import com.fs.starfarer.api.campaign.econ.MarketConditionAPI;
006import com.fs.starfarer.api.impl.campaign.ids.Conditions;
007import com.fs.starfarer.api.impl.campaign.intel.BaseIntelPlugin;
008import com.fs.starfarer.api.ui.TooltipMakerAPI;
009import com.fs.starfarer.api.util.Misc;
010import com.fs.starfarer.api.util.TimeoutTracker;
011
012public class RecentUnrest extends BaseMarketConditionPlugin {
013        
014        public static RecentUnrest get(MarketAPI market) {
015                return get(market, true);
016        }
017        public static RecentUnrest get(MarketAPI market, boolean addIfNeeded) {
018                MarketConditionAPI mc = market.getCondition(Conditions.RECENT_UNREST);
019                if (mc == null && !addIfNeeded) return null;
020                
021                if (mc == null) {
022                        String id = market.addCondition(Conditions.RECENT_UNREST);
023                        mc = market.getSpecificCondition(id);
024                }
025                return (RecentUnrest) mc.getPlugin();
026        }
027        
028        public static int getPenalty(MarketAPI market) {
029                RecentUnrest ru = get(market, false);
030                if (ru == null) return 0;
031                return ru.getPenalty();
032        }
033        
034        public static float DECREASE_DAYS = 90f;
035        
036        protected int penalty;
037        protected float untilDecrease = DECREASE_DAYS;
038        protected TimeoutTracker<String> reasons = new TimeoutTracker<String>();
039        
040        public RecentUnrest() {
041        }
042
043        public int getPenalty() {
044                return penalty;
045        }
046
047        public void apply(String id) {
048                market.getStability().modifyFlat(id, -1 * penalty, "Recent unrest");
049        }
050
051        public void unapply(String id) {
052                market.getStability().unmodify(id);
053        }
054        
055        
056        public void add(int stability, String reason) {
057                penalty += stability;
058                float dur = reasons.getRemaining(reason) + stability * DECREASE_DAYS;
059                reasons.add(reason, dur);
060        }
061        
062        public void counter(int points, String reason) {
063                points = Math.min(points, penalty);
064                penalty -= points;
065                if (penalty < 0) penalty = 0;
066                float dur = reasons.getRemaining(reason) + points * DECREASE_DAYS;
067                reasons.add(reason, dur);
068        }
069        
070        @Override
071        public void advance(float amount) {
072                super.advance(amount);
073                
074                float days = Misc.getDays(amount);
075                //days *= 100000f;
076                reasons.advance(days);
077
078                if (penalty > 0) {
079                        untilDecrease -= days;
080                        if (untilDecrease <= 0) {
081                                penalty--;
082                                if (penalty < 0) penalty = 0;
083                                untilDecrease = DECREASE_DAYS;
084                        }
085                }
086                if (penalty <= 0) {
087                        market.removeSpecificCondition(condition.getIdForPluginModifications());
088                }
089        }
090
091        @Override
092        public void createTooltip(TooltipMakerAPI tooltip, boolean expanded) {
093                super.createTooltip(tooltip, expanded);
094        }
095        
096
097        @Override
098        protected void createTooltipAfterDescription(TooltipMakerAPI tooltip, boolean expanded) {
099                super.createTooltipAfterDescription(tooltip, expanded);
100                
101                float pad = 3f;
102                float opad = 10f;
103                
104                if (Global.CODEX_TOOLTIP_MODE) {
105                        tooltip.addPara("A penalty to stability that goes down by %s point every three months.", 
106                                        opad, Misc.getHighlightColor(),
107                                        "1");
108                } else {
109                        tooltip.addPara("%s stability. Goes down by one point every three months.", 
110                                        opad, Misc.getHighlightColor(),
111                                        "-" + (int)penalty);
112                        
113                        if (!reasons.getItems().isEmpty()) {
114                                tooltip.addPara("Recent contributing factors:", opad);
115                                
116                                float initPad = pad;
117                                for (String reason : reasons.getItems()) {
118                                        tooltip.addPara(BaseIntelPlugin.BULLET + reason, initPad);
119                                        initPad = 0f;
120                                }
121                        }
122                }
123                
124        }
125
126        @Override
127        public boolean isTransient() {
128                return false;
129        }
130        
131        public void setPenalty(int penalty) {
132                this.penalty = penalty;
133        }
134        
135        
136}