aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/plans/models/Plan.java
blob: 339a1b9ea43dd9487c7b89c3945a28a8a7b7d11a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package org.wordpress.android.ui.plans.models;


import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.wordpress.android.util.JSONUtils;
import org.wordpress.android.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

public class Plan implements Serializable {
    private long mProductID;
    private String mProductName;
    private final Hashtable<String, Integer> mPrices = new Hashtable<>();
    private String mProductNameShort;
    private String mProductSlug;
    private String mTagline;
    private String mDescription;
    private long mCost;
    private int mBillPeriod;
    private String mProductType;
    private boolean mIsAvailable;
    private String mBillPeriodLabel;
    private String mPrice;
    private String mFormattedPrice;
    private String mIconUrl;
    private int mRawPrice;

    // Optionals
    private int mWidth;
    private int mHeight;
    private long mSaving; // diff from cost and original
    private long mOriginal; // Original price
    private String mFormattedOriginalPrice;
    private int mStore;
    private int mMulti;
    private String mSupportDocument;
    private String mCapability;
    private ArrayList<Integer> mBundleProductIds;
    private final ArrayList<PlanFeaturesHighlightSection> mFeaturesHighlightSections =  new ArrayList<>();

    // used to link with an actual product on the Store (ie: Used to load the price from the Store).
    private String mAndroidSKU;

    // Info attached to the current site/user
    private int mRawDiscount;
    private String mFormattedDiscount;
    private boolean mIsCurrentPlan;
    private boolean mCanStartTrial;
    private String mExpiry;
    private boolean mFreeTrial;
    private String mUserFacingExpiry;
    private String mSubscribedDate;
    private String mBundleSubscriptionID;


    public Plan(JSONObject planJSONObject) throws JSONException {
        mProductID = planJSONObject.getLong("product_id");
        mProductName = planJSONObject.getString("product_name");

        // Unfold prices object
        JSONObject priceJSONObject = planJSONObject.getJSONObject("prices");
        JSONArray priceKeys = priceJSONObject.names();
        if (priceKeys != null) {
            for (int i=0; i < priceKeys.length(); i ++) {
                String currentKey = priceKeys.getString(i);
                int currentPrice = priceJSONObject.getInt(currentKey);
                mPrices.put(currentKey, currentPrice);
            }
        }

        mProductNameShort = planJSONObject.getString("product_name_short");
        mProductSlug = planJSONObject.getString("product_slug");
        mTagline = planJSONObject.getString("tagline");
        mDescription = planJSONObject.getString("description");
        mCost = planJSONObject.getLong("cost");
        mBillPeriod = planJSONObject.getInt("bill_period");
        mProductType = planJSONObject.getString("product_type");
        mIsAvailable = JSONUtils.getBool(planJSONObject, "available");
        mBillPeriodLabel = planJSONObject.getString("bill_period_label");
        mPrice = planJSONObject.getString("price");
        mFormattedPrice = planJSONObject.getString("formatted_price");
        mRawPrice = planJSONObject.getInt("raw_price");
        mIconUrl = planJSONObject.optString("icon");
        mAndroidSKU = planJSONObject.optString("android_sku");

        // Optionals
        mWidth = planJSONObject.optInt("width");
        mHeight = planJSONObject.optInt("height");
        mSaving = planJSONObject.optLong("saving", 0L);
        mOriginal = planJSONObject.optLong("original", mCost);
        mFormattedOriginalPrice = planJSONObject.optString("formatted_original_price");
        mSupportDocument = planJSONObject.optString("support_document");
        mCapability = planJSONObject.optString("capability");
        mStore = planJSONObject.optInt("store");
        mMulti = planJSONObject.optInt("multi");

        if (planJSONObject.has("bundle_product_ids")) {
            JSONArray bundleIDS = planJSONObject.getJSONArray("bundle_product_ids");
            mBundleProductIds = new ArrayList<>(bundleIDS.length());
            for (int i=0; i < bundleIDS.length(); i ++) {
                int currentBundleID = bundleIDS.getInt(i);
                mBundleProductIds.add(currentBundleID);
            }
        }

        if (planJSONObject.has("features_highlight")) {
            JSONArray featuresHighlightSections = planJSONObject.getJSONArray("features_highlight");
            for (int i=0; i < featuresHighlightSections.length(); i++) {
                mFeaturesHighlightSections.add(
                        new PlanFeaturesHighlightSection(featuresHighlightSections.getJSONObject(i))
                );
            }
        }

        // Specific info liked with the current site
        mRawDiscount = planJSONObject.optInt("raw_discount", 0);
        mFormattedDiscount = planJSONObject.optString("formatted_discount");
        mCanStartTrial = JSONUtils.getBool(planJSONObject, "can_start_trial");
        mIsCurrentPlan = JSONUtils.getBool(planJSONObject, "current_plan");
        mExpiry = planJSONObject.optString("expiry");
        mUserFacingExpiry = planJSONObject.optString("user_facing_expiry");
        mSubscribedDate = planJSONObject.optString("subscribed_date");
        mFreeTrial = JSONUtils.getBool(planJSONObject, "free_trial");
        mBundleSubscriptionID = planJSONObject.optString("bundle_subscription_id");
    }


    public long getProductID() {
        return mProductID;
    }

    public String getProductName() {
        return StringUtils.notNullStr(mProductName);
    }

    public Hashtable<String, Integer> getPrices() {
        return mPrices;
    }

    public String getProductNameShort() {
        return StringUtils.notNullStr(mProductNameShort);
    }

    public String getProductSlug() {
        return StringUtils.notNullStr(mProductSlug);
    }

    public String getTagline() {
        return StringUtils.notNullStr(mTagline);
    }

    public String getDescription() {
        return StringUtils.notNullStr(mDescription);
    }

    public long getCost() {
        return mCost;
    }

    public int getBillPeriod() {
        return mBillPeriod;
    }

    public String getProductType() {
        return StringUtils.notNullStr(mProductType);
    }

    public boolean isAvailable() {
        return mIsAvailable;
    }

    public int getWidth() {
        return mWidth;
    }

    public int getHeight() {
        return mHeight;
    }

    public String getBillPeriodLabel() {
        return StringUtils.notNullStr(mBillPeriodLabel);
    }

    public String getPrice() {
        return StringUtils.notNullStr(mPrice);
    }

    public String getFormattedPrice() {
        return StringUtils.notNullStr(mFormattedPrice);
    }

    public int getRawPrice() {
        return mRawPrice;
    }

    public ArrayList<PlanFeaturesHighlightSection> getFeaturesHighlightSections() {
        return mFeaturesHighlightSections;
    }

    public List<Integer> getBundleProductIds() {
        return mBundleProductIds;
    }

    public String getCapability() {
        return StringUtils.notNullStr(mCapability);
    }

    public String getSupportDocument() {
        return StringUtils.notNullStr(mSupportDocument);
    }

    public int getMulti() {
        return mMulti;
    }

    public int getStore() {
        return mStore;
    }

    public String getFormattedOriginalPrice() {
        return StringUtils.notNullStr(mFormattedOriginalPrice);
    }

    public long getSaving() {
        return mSaving;
    }

    public long getOriginal() {
        return mOriginal;
    }

    public String getIconUrl() {
        return StringUtils.notNullStr(mIconUrl);
    }
    public void setIconUrl(String iconUrl) {
        mIconUrl = StringUtils.notNullStr(iconUrl);
    }
    public boolean hasIconUrl() {
        return !TextUtils.isEmpty(mIconUrl);
    }

    public int getRawDiscount() {
        return mRawDiscount;
    }

    public String getFormattedDiscount() {
        return StringUtils.notNullStr(mFormattedDiscount);
    }


    public boolean isCurrentPlan() {
        return mIsCurrentPlan;
    }

    public boolean canStartTrial() {
        return mCanStartTrial;
    }

    public String getSubscribedDate() {
        return StringUtils.notNullStr(mSubscribedDate);
    }

    public String getUserFacingExpiry() {
        return StringUtils.notNullStr(mUserFacingExpiry);
    }

    public boolean isFreeTrial() {
        return mFreeTrial;
    }

    public String getExpiry() {
        return StringUtils.notNullStr(mExpiry);
    }

    public String getBundleSubscriptionID() {
        return StringUtils.notNullStr(mBundleSubscriptionID);
    }
}