Back to Home

ESO Lua File v101044

internalingame/market/gamepad/market_gamepad.lua

[◄ back to folders ]
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
ZO_GAMEPAD_MARKET_SCENE_NAME = "gamepad_market"
ZO_GAMEPAD_MARKET_BUNDLE_CONTENTS_SCENE_NAME = "gamepad_market_bundle_contents"
ZO_GAMEPAD_MARKET_LOCKED_SCENE_NAME = "gamepad_market_locked"
ZO_GAMEPAD_MARKET_PRE_SCENE_NAME = "gamepad_market_pre_scene"
ZO_GAMEPAD_ENDEAVOR_SEAL_MARKET_PRE_SCENE_NAME = "gamepad_endeavor_seal_market_pre_scene"
local TERTIARY_OPTION_NONE = 0
local TERTIARY_OPTION_BUY_CROWNS = 1
local TERTIARY_OPTION_OPEN_ENDEAVORS = 2
ZO_GAMEPAD_MARKET_TEMPLATES =
{
    CROWN_STORE =
    {
        shownCurrencyTypeBalances =
        {
            {
                categoryIndex = nil, -- Default for all categories
                currencyTypes =
                {
                    MKCT_CROWNS,
                    MKCT_CROWN_GEMS,
                },
            },
            {
                categoryIndex = ZO_MARKET_ESO_PLUS_CATEGORY_INDEX,
                currencyTypes =
                {
                    MKCT_CROWNS,
                    MKCT_CROWN_GEMS,
                    MKCT_ENDEAVOR_SEALS,
                },
            },
        },
        displayGroup = MARKET_DISPLAY_GROUP_CROWN_STORE,
        featuredMarketProductFiltersMask = MARKET_PRODUCT_FILTER_TYPE_COST_CROWNS + MARKET_PRODUCT_FILTER_TYPE_COST_CROWN_GEMS,
        preSceneName = ZO_GAMEPAD_MARKET_PRE_SCENE_NAME,
        marketProductFilterTypes = 
        {
            MARKET_PRODUCT_FILTER_TYPE_COST_CROWNS,
            MARKET_PRODUCT_FILTER_TYPE_COST_CROWN_GEMS,
        },
        newMarketProductFilterTypes = 
        {
            MARKET_PRODUCT_FILTER_TYPE_COST_CROWNS + MARKET_PRODUCT_FILTER_TYPE_NEW,
            MARKET_PRODUCT_FILTER_TYPE_COST_CROWN_GEMS + MARKET_PRODUCT_FILTER_TYPE_NEW,
        },
        esoPlusOfferFilterTypes = 
        {
            MARKET_PRODUCT_FILTER_TYPE_COST_CROWNS + MARKET_PRODUCT_FILTER_TYPE_ESO_PLUS_OFFERS,
            MARKET_PRODUCT_FILTER_TYPE_COST_CROWN_GEMS + MARKET_PRODUCT_FILTER_TYPE_ESO_PLUS_OFFERS,
        },
        newEsoPlusOfferFilterTypes = 
        {
            MARKET_PRODUCT_FILTER_TYPE_COST_CROWNS + MARKET_PRODUCT_FILTER_TYPE_ESO_PLUS_OFFERS + MARKET_PRODUCT_FILTER_TYPE_NEW,
            MARKET_PRODUCT_FILTER_TYPE_COST_CROWN_GEMS + MARKET_PRODUCT_FILTER_TYPE_ESO_PLUS_OFFERS + MARKET_PRODUCT_FILTER_TYPE_NEW,
        },
        showEsoPlusOffers = true,
        showFeaturedProducts = true,
        tertiaryOption = TERTIARY_OPTION_BUY_CROWNS,
    },
    SEAL_STORE =
    {
        shownCurrencyTypeBalances =
        {
            {
                categoryIndex = nil, -- Default for all categories
                currencyTypes =
                {
                    MKCT_ENDEAVOR_SEALS,
                },
            },
        },
        displayGroup = MARKET_DISPLAY_GROUP_CROWN_STORE,
        featuredMarketProductFiltersMask = MARKET_PRODUCT_FILTER_TYPE_COST_ENDEAVOR_SEALS,
        preSceneName = ZO_GAMEPAD_ENDEAVOR_SEAL_MARKET_PRE_SCENE_NAME,
        marketProductFilterTypes = 
        {
            MARKET_PRODUCT_FILTER_TYPE_COST_ENDEAVOR_SEALS,
        },
        newMarketProductFilterTypes = 
        {
            MARKET_PRODUCT_FILTER_TYPE_COST_ENDEAVOR_SEALS + MARKET_PRODUCT_FILTER_TYPE_NEW,
        },
        esoPlusOfferFilterTypes =
        {
            MARKET_PRODUCT_FILTER_TYPE_COST_ENDEAVOR_SEALS + MARKET_PRODUCT_FILTER_TYPE_ESO_PLUS_OFFERS,
        },
        newEsoPlusOfferFilterTypes =
        {
            MARKET_PRODUCT_FILTER_TYPE_COST_ENDEAVOR_SEALS + MARKET_PRODUCT_FILTER_TYPE_ESO_PLUS_OFFERS + MARKET_PRODUCT_FILTER_TYPE_NEW,
        },
        showEsoPlusOffers = true,
        showFeaturedProducts = true,
        tertiaryOption = TERTIARY_OPTION_OPEN_ENDEAVORS,
        marketOpenedTutorialTriggerType = TUTORIAL_TRIGGER_SEAL_MARKET_OPENED,
     },
}
-- Establishes priority for routing requests to show products (ascending order).
ZO_GAMEPAD_PRIORITIZED_MARKET_TEMPLATES =
{
    ZO_GAMEPAD_MARKET_TEMPLATES.CROWN_STORE,
    ZO_GAMEPAD_MARKET_TEMPLATES.SEAL_STORE,
}
local FIRST_CATEGORY_INDEX = 1
local GAMEPAD_MARKET_LABELED_GROUP_LABEL_TEMPLATE = "ZO_GamepadMarket_GroupLabel"
local LABELED_GROUP_PADDING = 110 --padding from bottom of one group to the top of the next
local LABELED_GROUP_LABEL_PADDING = -10 --padding from the top of a group to the bottom of it's header
local MARKET_TERTIARY_BUTTON_DESCRIPTOR =
{
    alignment = KEYBIND_STRIP_ALIGN_RIGHT,
    gamepadOrder = 1,
    visible = function()
        return ZO_GAMEPAD_MARKET:GetTertiaryOption() ~= TERTIARY_OPTION_NONE
    end,
    name = function()
        local tertiaryOption = ZO_GAMEPAD_MARKET:GetTertiaryOption()
        if tertiaryOption == TERTIARY_OPTION_BUY_CROWNS then
            return GetString(SI_MARKET_BUY_CROWNS)
        elseif tertiaryOption == TERTIARY_OPTION_OPEN_ENDEAVORS then
            return GetString(SI_ACTIVITY_FINDER_OPEN_ENDEAVORS)
        end
    end,
    keybind = "UI_SHORTCUT_TERTIARY",
    callback = function()
        local tertiaryOption = ZO_GAMEPAD_MARKET:GetTertiaryOption()
        if tertiaryOption == TERTIARY_OPTION_BUY_CROWNS then
            ZO_ShowBuyCrownsPlatformDialog()
        elseif tertiaryOption == TERTIARY_OPTION_OPEN_ENDEAVORS then
            RequestOpenTimedActivities()
        end
    end,
}
local g_activeMarketScreen = nil
--
--[[ Gamepad Market ]]--
--
local GamepadMarket = ZO_Object.MultiSubclass(ZO_GamepadMarket_GridScreen, ZO_Market_Shared)
function GamepadMarket:New(...)
    return ZO_Market_Shared.New(self, ...)
end
function GamepadMarket:Initialize(control)
    self:SetDisplayGroup(MARKET_DISPLAY_GROUP_CROWN_STORE)
    self.shownCurrencyTypeBalances = nil
    self.showFeaturedProducts = false
    self.esoPlusOfferFilterTypes = {}
    self.newEsoPlusOfferFilterTypes = {}
    self.showEsoPlusOffers = false
    self.featuredMarketProductFiltersMask = nil
    self.marketProductFilterTypes = {}
    self.newMarketProductFilterTypes = {}
    local EMPTY_TAB_HEADER = {}
    ZO_GamepadMarket_GridScreen.Initialize(self, control, EMPTY_TAB_HEADER)
    ZO_Market_Shared.Initialize(self, control, ZO_GAMEPAD_MARKET_SCENE_NAME)
    self.marketProductGroupTableSortFunction = function(entry1, entry2)
        return self:CompareMarketProducts(entry1, entry2)
    end
end
function GamepadMarket:InitializeLabeledGroups()
    self.labeledGroups = {}
    self.labeledGroupLabelPool = ZO_ControlPool:New(self:GetLabeledGroupLabelTemplate(), self.control)
end
function GamepadMarket:ClearLabeledGroups()
    ZO_Market_Shared.ClearLabeledGroups(self)
    ZO_ClearNumericallyIndexedTable(self.labeledGroups)
    self.labeledGroupLabelPool:ReleaseAllObjects()
end
function GamepadMarket:ApplyMarketTemplate(template)
    if self.marketTemplate ~= template then
        self:SetDisplayGroup(template.displayGroup)
        self.marketTemplate = template
        self.preSceneName = template.preSceneName
        self.shownCurrencyTypeBalances = template.shownCurrencyTypeBalances
        self.showFeaturedProducts = template.showFeaturedProducts
        self.showEsoPlusOffers = template.showEsoPlusOffers
        self.featuredMarketProductFiltersMask = template.featuredMarketProductFiltersMask
        self.esoPlusOfferFilterTypes = template.esoPlusOfferFilterTypes
        self.newEsoPlusOfferFilterTypes = template.newEsoPlusOfferFilterTypes
        self.marketProductFilterTypes = template.marketProductFilterTypes
        self.newMarketProductFilterTypes = template.newMarketProductFilterTypes
        self.marketOpenedTutorialTriggerType = template.marketOpenedTutorialTriggerType
        self.tertiaryOption = template.tertiaryOption
    end
end
function GamepadMarket:GetTertiaryOption()
    return self.tertiaryOption
end
function GamepadMarket:SetupSceneGroupCallback()
    self.marketSceneGroup = self.marketScene:GetSceneGroup()
    self.marketSceneGroup:RegisterCallback("StateChange", function(_, newState)
        if newState == SCENE_GROUP_SHOWING then
            self:OnInitialInteraction()
        elseif newState == SCENE_GROUP_HIDDEN then
            self:OnEndInteraction()
        end
    end)
end
function GamepadMarket:PerformDeferredInitialization()
    if self.isInitialized then
        return
    end
    self.isLockedForCategoryRefresh = false
    self.OnGamepadDialogHidden = function()
    end
    self.categoryLabeledGroupTableMap = {} -- Used to lookup category "LabeledGroup" tables during category building
    self.subcategoryLabeledGroupTableMap = {} -- Used to lookup subcategory "LabeledGroup" tables during category building
    MARKET_CURRENCY_GAMEPAD:RegisterCallback("OnCurrencyUpdated", function() self:OnCurrencyUpdated() end)
    self.isInitialized = true
end
function GamepadMarket:IsShowing()
    return self.marketSceneGroup:IsShowing()
end
function GamepadMarket:GetLabeledGroupLabelTemplate()
    return GAMEPAD_MARKET_LABELED_GROUP_LABEL_TEMPLATE
end
function GamepadMarket:LayoutSelectedGridEntryTooltip()
    EVENT_MANAGER:UnregisterForUpdate("GamepadMarket_Tooltip")
    local selectedEntry = self.selectedGridEntry
    if selectedEntry ~= nil then
        if selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
            selectedEntry:LayoutTooltip(GAMEPAD_RIGHT_TOOLTIP)
        elseif selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MEMBERSHIP_INFO_TILE then
            local statusText, generateTextFunction = ZO_MARKET_MANAGER:GetEsoPlusStatusText()
            GAMEPAD_TOOLTIPS:LayoutEsoPlusMembershipTooltip(GAMEPAD_RIGHT_TOOLTIP, statusText)
            if generateTextFunction then
                EVENT_MANAGER:RegisterForUpdate("GamepadMarket_Tooltip", ZO_ONE_SECOND_IN_MILLISECONDS, function()
                    GAMEPAD_TOOLTIPS:ClearTooltip(GAMEPAD_RIGHT_TOOLTIP, true)
                    GAMEPAD_TOOLTIPS:LayoutEsoPlusMembershipTooltip(GAMEPAD_RIGHT_TOOLTIP, generateTextFunction())
                end)
            end
        elseif selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_FREE_TRIAL_TILE then
            GAMEPAD_TOOLTIPS:LayoutEsoPlusTrialNotification(GAMEPAD_RIGHT_TOOLTIP, ZO_MARKET_MANAGER:GetFreeTrialProductData():GetId())
        else
            GAMEPAD_TOOLTIPS:ClearTooltip(GAMEPAD_RIGHT_TOOLTIP)
        end
    else
        GAMEPAD_TOOLTIPS:ClearTooltip(GAMEPAD_RIGHT_TOOLTIP)
    end
end
function GamepadMarket:OnSelectionChanged(selectedData)
    ZO_GamepadMarket_GridScreen.OnSelectionChanged(self, selectedData)
    local previouslySelectedEntry = self.selectedGridEntry
    local previouslySelectedCategoryName = self.selectedGridEntryCategoryName
    if selectedData then
        self.selectedGridEntry = selectedData.object
        self.selectedGridEntryCategoryName = selectedData.categoryName
        if previouslySelectedCategoryName ~= self.selectedGridEntryCategoryName then
            --If the category name changed, re-narrate the subheader
            local DONT_NARRATE_HEADER = false
            local NARRATE_SUB_HEADER = true
            SCREEN_NARRATION_MANAGER:QueueFocus(self.focusList, DONT_NARRATE_HEADER, NARRATE_SUB_HEADER)
        else
            --Re-narrate when the selection changes
            SCREEN_NARRATION_MANAGER:QueueFocus(self.focusList)
        end
    elseif not self.isLockedForCategoryRefresh then
        GAMEPAD_TOOLTIPS:ClearTooltip(GAMEPAD_RIGHT_TOOLTIP)
        self.selectedGridEntry = nil
        self.selectedGridEntryCategoryName = nil
    end
    self:UpdatePreviousAndNewlySelectedProducts(previouslySelectedEntry, self.selectedGridEntry)
    KEYBIND_STRIP:UpdateKeybindButtonGroup(self.keybindStripDescriptors)
end
function GamepadMarket:OnStateChanged(oldState, newState)
    if newState == SCENE_SHOWING then
        self:OnShowing()
    elseif newState == SCENE_SHOWN then
        self:OnShown()
    elseif newState == SCENE_HIDING then
        self:OnHiding()
    elseif newState == SCENE_HIDDEN then
        self:OnHidden()
    end
end
function GamepadMarket:OnInitialInteraction()
    -- ensure that we are in the correct state
    if self.marketState ~= GetMarketState(self:GetDisplayGroup()) then
        self:UpdateMarket()
    elseif self.marketState == MARKET_STATE_OPEN then
        self:UpdateCurrentCategory()
    end
    
    if self.isInitialized and self.marketState == MARKET_STATE_OPEN then
        GAMEPAD_TOOLTIPS:ClearStatusLabel(GAMEPAD_LEFT_DIALOG_TOOLTIP) -- Clear status labels on entry to the market
        GAMEPAD_TOOLTIPS:ClearStatusLabel(GAMEPAD_RIGHT_TOOLTIP)
        self.isLockedForCategoryRefresh = false
    end
end
function GamepadMarket:OnEndInteraction()
    self:ClearProducts()
    self.currentCategoryMarketProductPool:ReleaseAllObjects()
    self.currentCategoryBlankProductPool:ReleaseAllObjects()
    self.lastCategoryData = nil
    self.currentCategoryData = nil
    self.header.tabBar:Clear()
    g_activeMarketScreen = nil
    ZO_Market_Shared.OnEndInteraction(self)
    KEYBIND_STRIP:SetStyle(KEYBIND_STRIP_STANDARD_STYLE)
    ZO_GAMEPAD_MARKET_BUNDLE_CONTENTS:ClearLastPreviewedMarketProductId()
end
function GamepadMarket:OnShowing()
    -- Order matters
    ZO_Market_Shared.OnShowing(self)
end
function GamepadMarket:RefreshMarketCurrencyTypeBalances()
    local activeCurrencyTypes = nil
    if self.shownCurrencyTypeBalances then
        for _, categoryCurrencyData in ipairs(self.shownCurrencyTypeBalances) do
            if not categoryCurrencyData.categoryIndex and not activeCurrencyTypes then
                activeCurrencyTypes = categoryCurrencyData.currencyTypes
            elseif self.currentCategoryIndex == categoryCurrencyData.categoryIndex then
                activeCurrencyTypes = categoryCurrencyData.currencyTypes
            end
        end
    end
    local isCrownGemCurrencyTypeShown = activeCurrencyTypes and ZO_IsElementInNumericallyIndexedTable(activeCurrencyTypes, MKCT_CROWN_GEMS)
    self.showCategoryCrownGemIcons = isCrownGemCurrencyTypeShown
    -- We only want to display the Crown Crate-related tutorial in the context of a
    -- store that displays products available for purchase using Crown Gems.
    self.suppressMarketCategoryTutorials = not isCrownGemCurrencyTypeShown
    MARKET_CURRENCY_GAMEPAD:SetVisibleMarketCurrencyTypes(activeCurrencyTypes)
end
function GamepadMarket:OnShown()
    if self.marketState == MARKET_STATE_OPEN then
        self:OnMarketOpen()
        self:ReleasePreviousCategoryProducts() -- Cleans up any products left over from entering submenus like bundle details or product preview
        self:Activate()
        self:AddKeybinds()
        g_activeMarketScreen = ZO_GAMEPAD_MARKET
        ZO_Market_Shared.OnShown(self)
        ZO_GamepadMarket_GridScreen.OnShown(self)
        self:RefreshKeybinds()
        CALLBACK_MANAGER:RegisterCallback("OnGamepadDialogHidden", self.OnGamepadDialogHidden)
        self:ProcessQueuedNavigation()
    else
        self:OnMarketLocked()
    end
end
function GamepadMarket:OnHiding()
    ZO_Market_Shared.OnHiding(self)
    if self.currentCategoryData then
        -- just fade out because we are hiding
        self.currentCategoryData.fragment:SetDirection(ZO_GAMEPAD_MARKET_PAGE_NO_DIRECTION)
    end
    self.queuedTutorial = nil
    ZO_GAMEPAD_MARKET_BUNDLE_CONTENTS:SetQueuedTutorial(nil)
    self.currentCategoryControl = nil
    self:Deactivate()
    self:RemoveKeybinds()
    CALLBACK_MANAGER:UnregisterCallback("OnGamepadDialogHidden", self.OnGamepadDialogHidden)
    EVENT_MANAGER:UnregisterForUpdate("GamepadMarket_Tooltip")
end
function GamepadMarket:InitializeKeybindDescriptors()
    local function RefreshOnPurchase() -- Only called on transaction success
        self.selectedGridEntry:Refresh()
    end
    local function OnPurchaseEnd(reachedConfirmationScene, purchasedConsumables, triggerTutorialOnPurchase)
        self.isLockedForCategoryRefresh = reachedConfirmationScene
        if purchasedConsumables then
            self:SetQueuedTutorial(TUTORIAL_TRIGGER_CROWN_CONSUMABLE_PURCHASED)
        end
        -- Check if an item triggers a tutorial on purchase second, since that takes priority over the consumable tutorial
        if triggerTutorialOnPurchase then
            self:SetQueuedTutorial(triggerTutorialOnPurchase)
        end
        -- We push the purchase scene when we reach the confirmation step
        -- So when we show the market again we will reactivate.
        -- Otherwise, we need to reactivate since we are just hiding a dialog.
        if not reachedConfirmationScene then
            self:Activate()
        end
    end
    local function OnFreeTrialPurchaseEnd(reachedConfirmationScene, purchasedConsumables, triggerTutorialOnPurchase)
        self.forceRedisplayCategory = true
        OnPurchaseEnd(reachedConfirmationScene, purchasedConsumables, triggerTutorialOnPurchase)
    end
    self.keybindStripDescriptors =
    {
        alignment = KEYBIND_STRIP_ALIGN_LEFT,
        -- Primary Action Keybind (purchase, start free trial, etc.)
        {
             name = function()
                local selectedEntry = self.selectedGridEntry
                if selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
                    if selectedEntry:IsBundle() then
                        return GetString(SI_MARKET_PURCHASE_BUNDLE_KEYBIND_TEXT)
                    else
                        return GetString(SI_MARKET_PURCHASE_KEYBIND_TEXT)
                    end
                elseif selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MEMBERSHIP_INFO_TILE then
                    return GetString(SI_GAMEPAD_MARKET_BUY_PLUS_KEYBIND_LABEL)
                elseif selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_FREE_TRIAL_TILE then
                    return GetString(SI_MARKET_START_TRIAL_KEYBIND_TEXT)
                end
            end,
            keybind = "UI_SHORTCUT_PRIMARY",
            visible = function()
                local selectedEntry = self.selectedGridEntry
                if selectedEntry then
                    if selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
                        return not selectedEntry:IsPurchaseLocked()
                    elseif selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MEMBERSHIP_INFO_TILE then
                        return not IsESOPlusSubscriber() or IsOnESOPlusFreeTrial()
                    elseif selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_FREE_TRIAL_TILE then
                        return not IsOnESOPlusFreeTrial()
                    end
                end
                return false
            end,
            enabled = function()
                if self:HasQueuedTutorial() then
                    return false
                elseif self.selectedGridEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
                    return self.selectedGridEntry:CanBePurchased()
                else
                    return true
                end
            end,
            callback = function()
                local selectedEntry = self.selectedGridEntry
                if selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
                    self.isLockedForCategoryRefresh = true
                    self:Deactivate()
                    self:PurchaseMarketProductInternal(self.selectedGridEntry:GetMarketProductData(), RefreshOnPurchase, OnPurchaseEnd)
                elseif selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MEMBERSHIP_INFO_TILE then
                    ZO_ShowBuySubscriptionPlatformDialog()
                elseif selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_FREE_TRIAL_TILE then
                    self.isLockedForCategoryRefresh = true
                    self:Deactivate()
                    self:PurchaseFreeTrialMarketProductInternal(ZO_MARKET_MANAGER:GetFreeTrialProductData(), function() self:RefreshProducts() end, OnFreeTrialPurchaseEnd)
                end
            end,
        },
        -- Gift Keybind
        {
             name = function()
                if self.selectedGridEntry:IsBundle() then
                    return GetString(SI_MARKET_GIFT_BUNDLE_KEYBIND_TEXT)
                else
                    return GetString(SI_MARKET_GIFT_KEYBIND_TEXT)
                end
            end,
            keybind = "UI_SHORTCUT_RIGHT_STICK",
            visible = function()
                if self.selectedGridEntry and self.selectedGridEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
                    return self.selectedGridEntry:IsGiftable()
                end
                return false
            end,
            enabled = function()
                return not self:HasQueuedTutorial()
            end,
            callback = function()
                self.isLockedForCategoryRefresh = true
                self:GiftMarketProductInternal(self.selectedGridEntry:GetMarketProductData(), RefreshOnPurchase, OnPurchaseEnd)
                self:Deactivate()
            end,
        },
        -- Secondary Action Keybind (preview, view benefits, etc.)
        {
            name = function()
                local selectedEntry = self.selectedGridEntry
                if selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
                    local previewType = self.selectedGridEntry:GetMarketProductPreviewType()
                    if previewType == ZO_MARKET_PREVIEW_TYPE_BUNDLE or previewType == ZO_MARKET_PREVIEW_TYPE_BUNDLE_AS_LIST then
                        return GetString(SI_MARKET_BUNDLE_DETAILS_KEYBIND_TEXT)
                    else
                        return GetString(SI_MARKET_PREVIEW_KEYBIND_TEXT)
                    end
                elseif selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MEMBERSHIP_INFO_TILE then
                    return GetString(SI_GAMEPAD_MARKET_VIEW_BENEFITS_KEYBIND)
                end
            end,
            keybind = "UI_SHORTCUT_SECONDARY",
            visible = function()
                local selectedEntry = self.selectedGridEntry
                if selectedEntry ~= nil then
                    return selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT or selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MEMBERSHIP_INFO_TILE
                end
                return false
            end,
            enabled = function()
                local selectedEntry = self.selectedGridEntry
                if selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
                    local previewType = selectedEntry:GetMarketProductPreviewType()
                    if previewType == ZO_MARKET_PREVIEW_TYPE_PREVIEWABLE then
                        return selectedEntry:HasPreview() and IsCharacterPreviewingAvailable()
                    elseif previewType == ZO_MARKET_PREVIEW_TYPE_HOUSE then
                        return CanJumpToHouseFromCurrentLocation(), GetString(SI_MARKET_PREVIEW_ERROR_CANNOT_JUMP_FROM_LOCATION)
                    else
                        return true
                    end
                else
                    return true
                end
            end,
            callback = function()
                local selectedEntry = self.selectedGridEntry
                if selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
                    local previewType = selectedEntry:GetMarketProductPreviewType()
                    if previewType == ZO_MARKET_PREVIEW_TYPE_BUNDLE then
                        self:ShowBundleContents(selectedEntry)
                    elseif previewType == ZO_MARKET_PREVIEW_TYPE_CROWN_CRATE or previewType == ZO_MARKET_PREVIEW_TYPE_BUNDLE_AS_LIST then
                        self:ShowMarketProductContentsAsList(selectedEntry, previewType)
                    elseif previewType == ZO_MARKET_PREVIEW_TYPE_HOUSE then
                        self:ShowHousePreviewDialog(selectedEntry)
                    else -- ZO_MARKET_PREVIEW_TYPE_PREVIEWABLE
                        self:BeginPreview(selectedEntry)
                    end
                elseif selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MEMBERSHIP_INFO_TILE then
                    ZO_ESO_PLUS_MEMBERSHIP_DIALOG:Show()
                end
            end,
        },
        MARKET_TERTIARY_BUTTON_DESCRIPTOR,
        KEYBIND_STRIP:GetDefaultGamepadBackButtonDescriptor(),
    }
end
function GamepadMarket:GetPrimaryButtonDescriptor()
    return self.primaryButtonDescriptor
end
function GamepadMarket:BeginPreview(selectedEntry)
    self:Deactivate()
    self.isLockedForCategoryRefresh = true
    self.currentCategoryData.fragment:SetDirection(ZO_GAMEPAD_MARKET_PAGE_NO_DIRECTION)
    g_activeMarketScreen = ZO_GAMEPAD_MARKET_PREVIEW
    ZO_GamepadMarket_GridScreen.BeginPreview(self, selectedEntry)
end 
function GamepadMarket:OnCategorySelected(data)
    if self.marketScene:IsShowing() and self.marketState == MARKET_STATE_OPEN then
        local categoryControlWasNil = self.currentCategoryControl == nil
        self.lastCategoryData = self.currentCategoryData
        self.currentCategoryData = data
        self.currentCategoryControl = data.control
        local categoryDataChanged = self.lastCategoryData ~= self.currentCategoryData
        if categoryDataChanged then
            self.isLockedForCategoryRefresh = false
        end
        if not self.isLockedForCategoryRefresh or self.forceRedisplayCategory then
            ZO_ClearTable(self.categoryLabeledGroupTableMap)
            ZO_ClearTable(self.subcategoryLabeledGroupTableMap)
            self:ClearLabeledGroups()
            self:ClearProducts()
            self:DisplayCategory(data)
            self:UpdateCategoryAnimationDirection()
            self.forceRedisplayCategory = false
        else
            self:UpdateScrollbarAlpha()
        end
        -- When the scene hides, the temporary fragment for the category is removed and we set self.currentCategoryControl to nil
        -- so the category control being nil indicates we need to re-show the current category
        if categoryDataChanged or categoryControlWasNil then
            -- This temporarily disables scrolling for the old and new categories, but allows for the paging animation to work correctly.
            -- The category controls will be re-anchored to the scroll child if/when they are shown
            if self.lastCategoryData then
                if not self.isLockedForCategoryRefresh then
                    local lastCategoryControl = self.lastCategoryData.control
                    lastCategoryControl:ClearAnchors()
                    lastCategoryControl:SetAnchorFill(self.contentContainer)
                end
                SCENE_MANAGER:RemoveFragment(self.lastCategoryData.fragment)
            end
            if not self.isLockedForCategoryRefresh then
                self.currentCategoryControl:ClearAnchors()
                self.currentCategoryControl:SetAnchorFill(self.contentContainer)
            end
            SCENE_MANAGER:AddFragment(data.fragment)
        end
        if not self.isLockedForCategoryRefresh then
            local categoryIndex, subcategoryIndex, isFeaturedCategory
            -- faked category types don't have real category indices so keep them as nil
            if data.type == ZO_MARKET_CATEGORY_TYPE_NONE then
                if data.parentData then
                    categoryIndex = data.parentData.categoryIndex
                    subcategoryIndex = data.categoryIndex
                else
                    categoryIndex = data.categoryIndex
                end
            elseif data.type == ZO_MARKET_CATEGORY_TYPE_FEATURED then
                isFeaturedCategory = true
            end
            OnMarketCategorySelected(self:GetDisplayGroup(), categoryIndex, subcategoryIndex, self.suppressMarketCategoryTutorials, isFeaturedCategory)
        end
        local queuedCategoryIndex, queuedSubcategoryIndex = self:GetQueuedCategoryIndices()
        if queuedCategoryIndex then
            self:ScrollToSubcategory(queuedCategoryIndex, queuedSubcategoryIndex) -- can't scroll instantly here, cause we just built the scroll
        else
            local queuedMarketProductId = self:GetQueuedMarketProductId()
            if queuedMarketProductId then
                self:ScrollToMarketProduct(queuedMarketProductId) -- can't scroll instantly here, cause we just built the scroll
            end
        end
    end
end
function GamepadMarket:DisplayCategory(data)
    if data.categoryIndex ~= self.currentCategoryIndex then
        self.currentCategoryIndex = data.categoryIndex
    end
    if data.type == ZO_MARKET_CATEGORY_TYPE_ESO_PLUS then
        self:DisplayEsoPlusOffer()
    else
        ZO_Market_Shared.DisplayCategory(self, data)
    end
end
do
    local MARKET_PRODUCT_SORT_KEYS =
    {
        name = { tiebreaker = "stackCount", tieBreakerSortOrder = ZO_SORT_ORDER_DOWN },
        stackCount = {},
    }
    function GamepadMarket:CompareMarketProducts(entry1, entry2)
        return ZO_TableOrderingFunction(entry1, entry2, "name", MARKET_PRODUCT_SORT_KEYS, ZO_SORT_ORDER_UP)
    end
end
function GamepadMarket:OnCurrencyUpdated()
    if not self.control:IsHidden() then
        self:RefreshKeybinds()
    elseif GAMEPAD_MARKET_BUNDLE_CONTENTS_SCENE and GAMEPAD_MARKET_BUNDLE_CONTENTS_SCENE:IsShowing() then
        ZO_GAMEPAD_MARKET_BUNDLE_CONTENTS:RefreshKeybinds()
    end
end
function GamepadMarket:CreateMarketScene()
    local scene = ZO_RemoteScene:New(ZO_GAMEPAD_MARKET_SCENE_NAME, SCENE_MANAGER)
    SYSTEMS:RegisterGamepadRootScene(ZO_MARKET_NAME, scene)
    self:SetMarketScene(scene)
end
function GamepadMarket:InitializeObjectPools()
    local PRODUCT_BASE_CONTROL_NAME = self.control:GetName() .. ZO_GAMEPAD_MARKET_PRODUCT_TEMPLATE
    local function CreateMarketProduct(objectPool)
        return ZO_GamepadMarketProduct:New(objectPool:GetNextControlId(), self.currentCategoryControl, PRODUCT_BASE_CONTROL_NAME)
    end
    self.marketProductPool = ZO_ObjectPool:New(CreateMarketProduct, ZO_ObjectPool_DefaultResetObject)
    self.currentCategoryMarketProductPool = ZO_MetaPool:New(self.marketProductPool)
    self.lastCategoryMarketProductPool = ZO_MetaPool:New(self.marketProductPool)
    local BLANK_TILE_BASE_CONTROL_NAME = self.control:GetName() .. ZO_GAMEPAD_MARKET_BLANK_TILE_TEMPLATE
    local function CreateBlankTile(objectPool)
        local control = CreateControlFromVirtual(BLANK_TILE_BASE_CONTROL_NAME, self.currentCategoryControl, ZO_GAMEPAD_MARKET_BLANK_TILE_TEMPLATE, objectPool:GetNextControlId())
        return ZO_GamepadMarketBlankTile:New(control)
    end
    self.blankTilePool = ZO_ObjectPool:New(CreateBlankTile, ZO_ObjectPool_DefaultResetObject)
    self.currentCategoryBlankProductPool = ZO_MetaPool:New(self.blankTilePool)
    self.lastCategoryBlankProductPool = ZO_MetaPool:New(self.blankTilePool)
    local GENERIC_TILE_BASE_CONTROL_NAME = self.control:GetName() .. "ZO_Gamepad_MarketGenericTileTemplate"
    local function CreateGenericTile(objectPool)
        local control = CreateControlFromVirtual(GENERIC_TILE_BASE_CONTROL_NAME, self.currentCategoryControl, "ZO_Gamepad_MarketGenericTileTemplate", objectPool:GetNextControlId())
        return ZO_GamepadMarketGenericTile:New(control)
    end
    self.genericTilePool = ZO_ObjectPool:New(CreateGenericTile, ZO_ObjectPool_DefaultResetObject)
    self.currentCategoryGenericProductPool = ZO_MetaPool:New(self.genericTilePool)
    self.lastCategoryGenericProductPool = ZO_MetaPool:New(self.genericTilePool)
end
-- "Cache" the current products in lastCategoryMarketProductPool. These will be released after the category change animation has finished
function GamepadMarket:ReleaseAllProducts()
    self.currentCategoryMarketProductPool, self.lastCategoryMarketProductPool = self.lastCategoryMarketProductPool, self.currentCategoryMarketProductPool
    self.currentCategoryBlankProductPool, self.lastCategoryBlankProductPool = self.lastCategoryBlankProductPool, self.currentCategoryBlankProductPool
    self.currentCategoryGenericProductPool, self.lastCategoryGenericProductPool = self.lastCategoryGenericProductPool, self.currentCategoryGenericProductPool
end
function GamepadMarket:ReleasePreviousCategoryProducts()
    self.lastCategoryMarketProductPool:ReleaseAllObjects()
    self.lastCategoryBlankProductPool:ReleaseAllObjects()
    self.lastCategoryGenericProductPool:ReleaseAllObjects()
end
    self.currentCategoryControl:ClearAnchors()
    self.currentCategoryControl:SetAnchor(TOPLEFT, self.contentContainer.scrollChild, TOPLEFT)
end
do
    local function CreateSubcategoryData(name, parentData, index, numProducts)
        return {
            text = name,
            numProducts = numProducts,
            soundId = SOUNDS.MARKET_SUB_CATEGORY_SELECTED,
            parentData = parentData,
            parent = parentData.control,
            categoryIndex = index,
        }
    end
    local GAMEPAD_MARKET_CATEGORY_TEMPLATE = "ZO_GamepadMarket_CategoryTemplate"
    local g_controlNameToFragment = {}
    local function GetOrCreateCategoryControlAndFragment(parent, marketCategoryIndex, categoryType)
        local currentControlName = GAMEPAD_MARKET_CATEGORY_TEMPLATE
        if marketCategoryIndex and marketCategoryIndex > 0 then
            currentControlName = currentControlName .. marketCategoryIndex
        else -- this is a faked category
            currentControlName = currentControlName .. categoryType
        end
        local currentControl = GetControl(currentControlName)
        if currentControl ~= nil then
            local currentFragment = g_controlNameToFragment[currentControlName]
            return currentControl, currentFragment
        else
            local newControl = CreateControlFromVirtual(currentControlName, parent, GAMEPAD_MARKET_CATEGORY_TEMPLATE)
            local ALWAYS_ANIMATE = true
            local newFragment = ZO_GamepadMarketPageFragment:New(newControl, ALWAYS_ANIMATE)
            g_controlNameToFragment[currentControlName] = newFragment
            return newControl, newFragment
        end
    end
    local function CreateCategoryData(name, parent, marketCategoryIndex, tabIndex, numSubcategories, categoryType)
        categoryType = categoryType or ZO_MARKET_CATEGORY_TYPE_NONE
        local control, fragment = GetOrCreateCategoryControlAndFragment(parent, marketCategoryIndex, categoryType)
        return {
            text = name,
            control = control,
            subCategories = {},
            numSubcategories = numSubcategories,
            soundId = SOUNDS.MARKET_CATEGORY_SELECTED,
            categoryIndex = marketCategoryIndex, -- Market category index used for def lookup
            tabIndex = tabIndex, -- Index in the tab header, as displayed. This can be different from the market category index when there are faked categories
            type = categoryType,
            fragment = fragment,
        }
    end
    local SUBCATEGORY_GEM_FORMATTED_ICON = ZO_Currency_GetGamepadFormattedCurrencyIcon(CURT_CROWN_GEMS)
    function GamepadMarket:AddTopLevelCategory(categoryIndex, tabIndex, name, numSubcategories, categoryType, containsNewProductsFunction)
        -- cache the possible category names so we don't reformat the string everytime we change categories
        local formattedBaseName = zo_strformat(SI_MARKET_PRODUCT_NAME_FORMATTER, name)
        local formattedNameWithNew = zo_iconTextFormat(ZO_GAMEPAD_NEW_ICON_32, 32, 32, name)
        local nameFunction = function()
            if containsNewProductsFunction and containsNewProductsFunction() then
                return formattedNameWithNew
            else
                return formattedBaseName
            end
        end
        local nameNarrationFunction = function()
            local narrations = {}
            ZO_AppendNarration(narrations, SCREEN_NARRATION_MANAGER:CreateNarratableObject(formattedBaseName))
            --If this category contains new products, include that in the narration
            if containsNewProductsFunction and containsNewProductsFunction() then
                ZO_AppendNarration(narrations, SCREEN_NARRATION_MANAGER:CreateNarratableObject(GetString(SI_SCREEN_NARRATION_NEW_ICON_NARRATION)))
            end
            return narrations
        end
        local categoryData = CreateCategoryData(formattedBaseName, self.contentContainer.scrollChild, categoryIndex, tabIndex, numSubcategories, categoryType)
        local hasChildren = numSubcategories > 0
        if hasChildren then
            local showCategoryCrownGemIcons = self.showCategoryCrownGemIcons
            local displayGroup = self:GetDisplayGroup()
            for i = 1, numSubcategories do
                if self:DoesCategoryOrSubcategoriesContainFilteredProducts(displayGroup, categoryIndex, i, self.marketProductFilterTypes) then
                    local subCategoryName, numSubCategoryMarketProducts, showGemIcon = GetMarketProductSubCategoryInfo(displayGroup, categoryIndex, i)
                    if showGemIcon and showCategoryCrownGemIcons then
                        subCategoryName = string.format("%s %s", SUBCATEGORY_GEM_FORMATTED_ICON, zo_strformat(SI_MARKET_PRODUCT_NAME_FORMATTER, subCategoryName))
                    else
                        subCategoryName = zo_strformat(SI_MARKET_PRODUCT_NAME_FORMATTER, subCategoryName)
                    end
                    local subCategoryData = CreateSubcategoryData(subCategoryName, categoryData, i, numSubCategoryMarketProducts)
                    table.insert(categoryData.subCategories, subCategoryData)
                end
            end
        end
        local function OnCategoryFragmentStateChanged(oldState, newState)
            if newState == SCENE_FRAGMENT_HIDDEN then
                self:ReleasePreviousCategoryProducts() -- Wait to release products until the after the animation has finished
            elseif newState == SCENE_FRAGMENT_SHOWN and not self.isLockedForCategoryRefresh then
                -- Re-anchoring after animation to enable scrolling of the category control
                self:AnchorCurrentCategoryControlToScrollChild()
            end
        end
        categoryData.fragment:RegisterCallback("StateChange", OnCategoryFragmentStateChanged)
        local tabEntry =
        {
            text = nameFunction,
            narrationText = nameNarrationFunction,
            categoryData = categoryData,
            callback = function()
                self:OnCategorySelected(categoryData)
                --Re-narrate on tab change
                local NARRATE_HEADER = true
                local NARRATE_SUB_HEADER = true
                SCREEN_NARRATION_MANAGER:QueueFocus(self.focusList, NARRATE_HEADER, NARRATE_SUB_HEADER)
            end,
        }
        table.insert(self.headerData.tabBarEntries, tabEntry)
    end
end
do
    local currentTabIndex = FIRST_CATEGORY_INDEX
    local function GetFirstTabIndex()
        currentTabIndex = FIRST_CATEGORY_INDEX
        return currentTabIndex
    end
    local function GetNextTabIndex()
        currentTabIndex = currentTabIndex + 1
        return currentTabIndex
    end
    local ZERO_SUBCATEGORIES = 0
    function GamepadMarket:BuildCategories(control)
        if self.marketState == MARKET_STATE_OPEN then
            ZO_ClearTable(self.headerData.tabBarEntries)
            local currentTabIndex = GetFirstTabIndex()
            local hasFeaturedCategory = self.showFeaturedProducts
            if self.showFeaturedProducts then
                hasFeaturedCategory = self:DoesFeaturedMarketProductExist()
                self.featuredCategoryIndex = nil
                if hasFeaturedCategory then
                    self.featuredCategoryIndex = currentTabIndex
                    self:AddTopLevelCategory(ZO_MARKET_FEATURED_CATEGORY_INDEX, self.featuredCategoryIndex, GetString(SI_MARKET_FEATURED_CATEGORY), ZERO_SUBCATEGORIES, ZO_MARKET_CATEGORY_TYPE_FEATURED, function()
                        return self:HasNewFeaturedMarketProducts()
                    end)
                end
            end
            if self.showEsoPlusOffers then
                local showNewOnEsoPlusCategoryFunction = function()
                    ZO_MARKET_MANAGER:UpdateFreeTrialProduct()
                    if ZO_MARKET_MANAGER:ShouldShowFreeTrial() then
                        local freeTrialIsNew = ZO_MARKET_MANAGER:GetFreeTrialProductData():IsNew()
                        if freeTrialIsNew then
                            return true
                        end
                    end
                    local displayGroup = self:GetDisplayGroup()
                    local numMarketCategories = GetNumMarketProductCategories(displayGroup)
                    for categoryIndex = 1, numMarketCategories do
                        if self:DoesCategoryOrSubcategoriesContainFilteredProducts(displayGroup, categoryIndex, ZO_NO_MARKET_SUBCATEGORY, self.newEsoPlusOfferFilterTypes) then
                            return true
                        end
                    end
                    return false
                end
                currentTabIndex = hasFeaturedCategory and GetNextTabIndex() or currentTabIndex
                self.esoPlusCategoryIndex = currentTabIndex
                self:AddTopLevelCategory(ZO_MARKET_ESO_PLUS_CATEGORY_INDEX, self.esoPlusCategoryIndex, GetString(SI_MARKET_ESO_PLUS_CATEGORY), ZERO_SUBCATEGORIES, ZO_MARKET_CATEGORY_TYPE_ESO_PLUS, showNewOnEsoPlusCategoryFunction)
            end
            -- adding in the custom categories offsets our market product category indices
            -- so even though a category is index 1 from data, it might actually be index 3
            -- since we show a featured and ESO Plus category
            self.categoryIndexOffset = currentTabIndex
            local displayGroup = self:GetDisplayGroup()
            local numCategories = GetNumMarketProductCategories(displayGroup)
            for categoryIndex = 1, numCategories do
                if self:DoesCategoryOrSubcategoriesContainFilteredProducts(displayGroup, categoryIndex, ZO_NO_MARKET_SUBCATEGORY, self.marketProductFilterTypes) then
                    local name, numSubcategories = GetMarketProductCategoryInfo(displayGroup, categoryIndex)
                    self:AddTopLevelCategory(categoryIndex, GetNextTabIndex(), name, numSubcategories, ZO_MARKET_CATEGORY_TYPE_NONE, function()
                        return self:DoesCategoryOrSubcategoriesContainFilteredProducts(displayGroup, categoryIndex, ZO_NO_MARKET_SUBCATEGORY, self.newMarketProductFilterTypes)
                    end)
                end
            end
            self.categoriesInitialized = true
            self.refreshCategories = false
        end
    end
end
function GamepadMarket:BuildFeaturedMarketProductList()
    local marketProductPresentations = self:GetFeaturedProductPresentations()
    for _, productData in ipairs(marketProductPresentations) do
        local marketProduct = self.currentCategoryMarketProductPool:AcquireObject()
        marketProduct:Show(productData)
        if productData:ContainsDLC() then
            self:AddMarketProductEntryToLabeledGroupTable(self.dlcProducts, marketProduct)
        elseif productData:IsLimitedTimeProduct() then
            self:AddMarketProductEntryToLabeledGroupTable(self.limitedTimedOfferProducts, marketProduct)
        elseif productData:IsFeatured() then
            self:AddMarketProductEntryToLabeledGroupTable(self.featuredProducts, marketProduct)
        else
            self:AddMarketProductEntryToLabeledGroupTable(self.marketProducts, marketProduct)
        end
    end
    self:SortMarketProductLabeledGroupTable(self.limitedTimedOfferProducts)
    self:SortMarketProductLabeledGroupTable(self.featuredProducts)
    local labeledGroupTables =
    {
        {name = GetString(SI_MARKET_LIMITED_TIME_OFFER_CATEGORY), marketProducts = self.limitedTimedOfferProducts},
        {name = GetString(SI_MARKET_DLC_CATEGORY), marketProducts = self.dlcProducts},
        {name = GetString(SI_MARKET_FEATURED_CATEGORY), marketProducts = self.featuredProducts},
        {name = GetString(SI_MARKET_ALL_LABEL), marketProducts = self.marketProducts},
    }
    self:LayoutMarketProducts(labeledGroupTables)
end
local function SubcategoryListSort(firstEntry, secondEntry)
    return firstEntry.categoryIndex < secondEntry.categoryIndex
end
-- the table of subcategory data may have holes in it if a category ends up not displaying
-- any market products (they were all featured for example) so we need to resort to get rid of holes
local function CreateSortedSubcategoryList(subcategoryData)
    local sortedList = {}
    for k, v in pairs(subcategoryData) do
        table.insert(sortedList, { categoryIndex = k, displayName = v.displayName, groupTable = v.groupTable})
    end
    table.sort(sortedList, SubcategoryListSort)
    return sortedList
end
function GamepadMarket:BuildMarketProductList(data)
    local parentCategoryIndex, parentSubcategoryIndex = self:GetCategoryIndices(data, data.parentData)
    -- subcategories will be accumulated into the parent categories, so ignore them
    if not parentSubcategoryIndex then
        -- For gamepad, if the top level category is set to disable the LTO grouping, we won't show it at all in the category
        -- so even though the subcategories aren't flagged as such their products won't go into a LTO grouping
        local disableLTOGrouping = IsLTODisabledForMarketProductCategory(self:GetDisplayGroup(), parentCategoryIndex)
        -- iterate over all of the subcategories in this category to display all of their market products
        -- starting at 0 since that will indicate to GetCategoryProductIds that we also want the products under the parent category itself
        for i = 0, data.numSubcategories do
            local subCategoryData = data.subCategories[i]
            local subcategoryIndex = subCategoryData and subCategoryData.categoryIndex or 0
            local marketProductPresentations = { self:GetCategoryProductIds(parentCategoryIndex, subcategoryIndex) }
            for index, productData in ipairs(marketProductPresentations) do
                local marketProduct = self.currentCategoryMarketProductPool:AcquireObject()
                marketProduct:Show(productData)
                if not disableLTOGrouping and productData:IsLimitedTimeProduct() then
                    self:AddMarketProductEntryToLabeledGroupTable(self.limitedTimedOfferProducts, marketProduct)
                elseif productData:IsFeatured() then
                    self:AddMarketProductEntryToLabeledGroupTable(self.featuredProducts, marketProduct)
                else
                    if subCategoryData then
                        self:AddMarketProductEntryToLabeledGroupTable(self:FindOrCreateSubCategoryLabeledGroupTable(subCategoryData.categoryIndex, subCategoryData.text), marketProduct)
                    else
                        self:AddMarketProductEntryToLabeledGroupTable(self.marketProducts, marketProduct)
                    end
                end
            end
        end
        self:SortMarketProductLabeledGroupTable(self.limitedTimedOfferProducts)
        self:SortMarketProductLabeledGroupTable(self.featuredProducts)
        local labeledGroupTables =
        {
            {name = GetString(SI_MARKET_LIMITED_TIME_OFFER_CATEGORY), marketProducts = self.limitedTimedOfferProducts},
            {name = GetString(SI_MARKET_FEATURED_CATEGORY), marketProducts = self.featuredProducts},
        }
        local sortedSubcategoryGroupTables = CreateSortedSubcategoryList(self.subcategoryLabeledGroupTableMap)
        for index, groupTableInfo in ipairs(sortedSubcategoryGroupTables) do
            local groupTable = groupTableInfo.groupTable
            self:SortMarketProductLabeledGroupTable(groupTable)
            table.insert(labeledGroupTables, {name = groupTableInfo.displayName, marketProducts = groupTable})
        end
        self:SortMarketProductLabeledGroupTable(self.marketProducts)
        table.insert(labeledGroupTables, {name = GetString(SI_MARKET_ALL_LABEL), marketProducts = self.marketProducts})
        self:LayoutMarketProducts(labeledGroupTables)
    end
end
-- ... is a list of MarketProductData
function GamepadMarket:GetCategoryProductIds(categoryIndex, subcategoryIndex, productFilterFunction, ...)
    if subcategoryIndex == 0 then
        local numMarketProducts = select(3, GetMarketProductCategoryInfo(self:GetDisplayGroup(), categoryIndex))
        return self:GetMarketProductPresentations(categoryIndex, ZO_NO_MARKET_SUBCATEGORY, numMarketProducts, productFilterFunction, ...)
    else
        local numMarketProducts = select(2, GetMarketProductSubCategoryInfo(self:GetDisplayGroup(), categoryIndex, subcategoryIndex))
        return self:GetMarketProductPresentations(categoryIndex, subcategoryIndex, numMarketProducts, productFilterFunction, ...)
    end
end
-- ... is a list of MarketProductData
function GamepadMarket:GetMarketProductPresentations(categoryIndex, subcategoryIndex, index, productFilterFunction, ...)
    if index >= 1 then
        local displayGroup = self:GetDisplayGroup()
        local id, presentationIndex = GetMarketProductPresentationIds(displayGroup, categoryIndex, subcategoryIndex, index)
        index = index - 1
        if self:DoesMarketProductMatchAnyFilter(id, presentationIndex, self.marketProductFilterTypes) then
            local productData = ZO_MarketProductData:New(id, presentationIndex)
            if productFilterFunction and not productFilterFunction(productData) then
                return self:GetMarketProductPresentations(categoryIndex, subcategoryIndex, index, productFilterFunction, ...)
            else
                return self:GetMarketProductPresentations(categoryIndex, subcategoryIndex, index, productFilterFunction, productData, ...)
            end
        else
            return self:GetMarketProductPresentations(categoryIndex, subcategoryIndex, index, productFilterFunction, ...)
        end
    end
    return ...
end
-- override from ZO_GamepadMarket_GridScreen
function GamepadMarket:FinishBuild()
    self:FinishCurrentLabeledGroup() -- must come before call to parent's FinishBuild
    ZO_GamepadMarket_GridScreen.FinishBuild(self)
end
-- override from ZO_GamepadMarket_GridScreen
function GamepadMarket:EndCurrentPreview()
    ZO_GamepadMarket_GridScreen.EndCurrentPreview(self)
end
function GamepadMarket:AddLabel(labeledGroupName, parentControl, yPadding)
    local labeledGroupLabel = self.labeledGroupLabelPool:AcquireObject()
    labeledGroupLabel:SetText(labeledGroupName)
    labeledGroupLabel:SetParent(parentControl)
    labeledGroupLabel:ClearAnchors()
    labeledGroupLabel:SetAnchor(BOTTOMLEFT, parentControl, TOPLEFT, 0, yPadding)
end
function GamepadMarket:FinishRowWithBlankTiles(labeledGroupName)
    local currentItemRowIndex = #self.gridEntries % self.itemsPerRow
    if currentItemRowIndex > 0 then
        for i = currentItemRowIndex, self.itemsPerRow - 1 do
            local blankTile = self.currentCategoryBlankProductPool:AcquireObject()
            blankTile:Show()
            self:AddEntry(blankTile, blankTile:GetControl(), labeledGroupName)
        end
    end
end
function GamepadMarket:FinishCurrentLabeledGroup()
    local currentLabeledGroup = self.labeledGroups[#self.labeledGroups]
    self:FinishRowWithBlankTiles(currentLabeledGroup.name)
    if currentLabeledGroup.numEntries > 0 then
        self.gridYPaddingOffset = self.gridYPaddingOffset + LABELED_GROUP_PADDING
    end
end
function GamepadMarket:AddLabeledGroupTable(labeledGroupName, labeledGroupTable)
    if #self.labeledGroups > 0 then
        self:FinishCurrentLabeledGroup()
    end
    local numEntries = #labeledGroupTable
    table.insert(self.labeledGroups, { name = labeledGroupName, table = labeledGroupTable, numEntries = numEntries })
    for i, entry in ipairs(labeledGroupTable) do
        self:AddEntry(entry.object, entry.control, labeledGroupName)
        if i == 1 and labeledGroupName then
            self:AddLabel(labeledGroupName, entry.control, LABELED_GROUP_LABEL_PADDING)
        end
    end
end
function GamepadMarket:AddMarketProductEntryToLabeledGroupTable(labeledGroupTable, marketProduct)
    local entryInfo = {
                            object = marketProduct,
                            control = marketProduct:GetControl(),
                            entryType = marketProduct:GetEntryType(),
                            -- for sorting
                            name = marketProduct:GetMarketProductDisplayName(),
                            stackCount = marketProduct:GetStackCount(),
                        }
    table.insert(labeledGroupTable, entryInfo)
end
function GamepadMarket:SortMarketProductLabeledGroupTable(labeledGroupTable)
    table.sort(labeledGroupTable, self.marketProductGroupTableSortFunction)
end
function GamepadMarket:FindOrCreateSubCategoryLabeledGroupTable(subcategoryIndex, displayName)
    if not self.subcategoryLabeledGroupTableMap[subcategoryIndex] then
        self.subcategoryLabeledGroupTableMap[subcategoryIndex] = {displayName = displayName, groupTable = {}}
    end
    return self.subcategoryLabeledGroupTableMap[subcategoryIndex].groupTable
end
function GamepadMarket:FindOrCreateCategoryLabeledGroupTable(categoryIndex, displayName)
    if not self.categoryLabeledGroupTableMap[categoryIndex] then
        self.categoryLabeledGroupTableMap[categoryIndex] = {displayName = displayName, groupTable = {}}
    end
    return self.categoryLabeledGroupTableMap[categoryIndex].groupTable
end
function GamepadMarket:UpdateCategoryAnimationDirection()
    local direction = ZO_GAMEPAD_MARKET_PAGE_LEFT_DIRECTION
    if self.lastCategoryData and self.lastCategoryData ~= self.currentCategoryData then
        -- We have to account for tab wrapping and perceived direction traversal
        local currentTabIndex = self.currentCategoryData.tabIndex
        local lastTabIndex = self.lastCategoryData.tabIndex
        if currentTabIndex < lastTabIndex then
            if currentTabIndex ~= FIRST_CATEGORY_INDEX or lastTabIndex ~= #self.headerData.tabBarEntries then
                direction = ZO_GAMEPAD_MARKET_PAGE_RIGHT_DIRECTION
            end
        elseif currentTabIndex == #self.headerData.tabBarEntries and lastTabIndex == FIRST_CATEGORY_INDEX then
            direction = ZO_GAMEPAD_MARKET_PAGE_RIGHT_DIRECTION
        end
    else -- we don't have a last category or its the same as the current so no need to animate
        direction = ZO_GAMEPAD_MARKET_PAGE_NO_DIRECTION
    end
    if self.lastCategoryData then
        self.lastCategoryData.fragment:SetDirection(direction)
    end
    self.currentCategoryData.fragment:SetDirection(self.isLockedForCategoryRefresh and ZO_GAMEPAD_MARKET_PAGE_NO_DIRECTION or direction)
end
function GamepadMarket:SetCurrentKeybinds(keybindDescriptor)
    if keybindDescriptor ~= self.currentKeybindDescriptor then
        KEYBIND_STRIP:RemoveKeybindButtonGroup(self.currentKeybindDescriptor)
        KEYBIND_STRIP:AddKeybindButtonGroup(keybindDescriptor)
        self.currentKeybindDescriptor = keybindDescriptor
    end
end
do
    local function DoesMarketProductHaveEsoPlusPrice(productData)
        local productId = productData:GetId()
        local presentationIndex = productData:HasValidPresentationIndex() and productData:GetPresentationIndex() or ZO_FEATURED_PRESENTATION_INDEX
        return ZO_GAMEPAD_MARKET:DoesMarketProductMatchAnyFilter(productId, presentationIndex, ZO_GAMEPAD_MARKET.esoPlusOfferFilterTypes)
    end
    local function IsFullEsoPlusMember()
        return IsESOPlusSubscriber() and not IsOnESOPlusFreeTrial()
    end
    function GamepadMarket:DisplayEsoPlusOffer()
        local esoPlusMembershipTiles = {}
        ZO_MARKET_MANAGER:UpdateFreeTrialProduct()
        local shouldShowFreeTrial = ZO_MARKET_MANAGER:HasFreeTrialProduct() and not IsFullEsoPlusMember()
        if shouldShowFreeTrial then
            local freeTrialTile = self.currentCategoryGenericProductPool:AcquireObject()
            freeTrialTile:SetEntryType(ZO_GAMEPAD_MARKET_ENTRY_FREE_TRIAL_TILE)
            freeTrialTile:SetBackground("EsoUI/Art/Market/Gamepad/gp_free_trial_tile_background.dds")
            freeTrialTile:SetTitle(GetString(SI_GAMEPAD_MARKET_FREE_TRIAL_TILE_TITLE))
            local function GetFreeTrialText()
                if IsOnESOPlusFreeTrial() then
                    return GetString(SI_GAMEPAD_MARKET_FREE_TRIAL_TILE_ACTIVE_TEXT)
                else
                    return GetString(SI_GAMEPAD_MARKET_FREE_TRIAL_TILE_TEXT)
                end
            end
            freeTrialTile:SetText(GetFreeTrialText)
            freeTrialTile:SetPurchaseCheckFunction(IsOnESOPlusFreeTrial)
            freeTrialTile:Show()
            local entryInfo =
            {
                object = freeTrialTile,
                control = freeTrialTile:GetControl(),
            }
            table.insert(esoPlusMembershipTiles, entryInfo)
        end
        -- Add membership info tile
        local membershipInfoTile = self.currentCategoryGenericProductPool:AcquireObject()
        membershipInfoTile:SetEntryType(ZO_GAMEPAD_MARKET_ENTRY_MEMBERSHIP_INFO_TILE)
        membershipInfoTile:SetBackground("EsoUI/Art/Market/Gamepad/gp_membership_info_tile_background.dds")
        membershipInfoTile:SetTitle(GetString(SI_GAMEPAD_MARKET_MEMBERSHIP_INFO_TILE_TITLE))
        membershipInfoTile:SetTitleColors(ZO_MARKET_PRODUCT_ESO_PLUS_COLOR, ZO_MARKET_PRODUCT_ESO_PLUS_DIMMED_COLOR)
        local function GetMembershipInfoText()
            if IsFullEsoPlusMember() then
                return GetString(SI_GAMEPAD_MARKET_MEMBERSHIP_INFO_TILE_ACTIVE_TEXT)
            else
                return GetString(SI_GAMEPAD_MARKET_MEMBERSHIP_INFO_TILE_TEXT)
            end
        end
        membershipInfoTile:SetText(GetMembershipInfoText)
        membershipInfoTile:SetTextColors(ZO_MARKET_PRODUCT_ESO_PLUS_COLOR, ZO_MARKET_PRODUCT_ESO_PLUS_DIMMED_COLOR)
        membershipInfoTile:SetPurchaseCheckFunction(IsFullEsoPlusMember)
        membershipInfoTile:Show()
        local entryInfo =
        {
            object = membershipInfoTile,
            control = membershipInfoTile:GetControl(),
        }
        table.insert(esoPlusMembershipTiles, entryInfo)
        local displayGroup = self:GetDisplayGroup()
        local numCategories = GetNumMarketProductCategories(displayGroup)
        for categoryIndex = 1, numCategories do
            if self:DoesCategoryOrSubcategoriesContainFilteredProducts(displayGroup, categoryIndex, ZO_NO_MARKET_SUBCATEGORY, self.esoPlusOfferFilterTypes) then
                local categoryName, numSubcategories = GetMarketProductCategoryInfo(displayGroup, categoryIndex)
                local formattedBaseName = zo_strformat(SI_MARKET_PRODUCT_NAME_FORMATTER, categoryName)
                -- iterate over all of the subcategories in this category to display all of their market products
                -- starting at 0 since that will indicate to GetCategoryProductIds that we also want the products under the parent category itself
                for subcategoryIndex = 0, numSubcategories do
                    if subcategoryIndex == 0 or self:DoesCategoryContainFilteredProducts(displayGroup, categoryIndex, subcategoryIndex, self.esoPlusOfferFilterTypes) then
                        local marketProductPresentations = { self:GetCategoryProductIds(categoryIndex, subcategoryIndex, DoesMarketProductHaveEsoPlusPrice) }
                        for productIndex, productData in ipairs(marketProductPresentations) do
                            local shouldAddProduct = true
                            if productData:IsLimitedTimeProduct() then
                                for index, value in ipairs(self.limitedTimedOfferProducts) do
                                    if value.object.productData and value.object.productData:GetId() == productData:GetId() then
                                        shouldAddProduct = false
                                        break
                                    end
                                end
                                if shouldAddProduct then
                                    local marketProduct = self.currentCategoryMarketProductPool:AcquireObject()
                                    marketProduct:Show(productData)
                                    self:AddMarketProductEntryToLabeledGroupTable(self.limitedTimedOfferProducts, marketProduct)
                                end
                            elseif productData:IsFeatured() then
                                for index, value in ipairs(self.featuredProducts) do
                                    if value.object.productData and value.object.productData:GetId() == productData:GetId() then
                                        shouldAddProduct = false
                                        break
                                    end
                                end
                                if shouldAddProduct then
                                    local marketProduct = self.currentCategoryMarketProductPool:AcquireObject()
                                    marketProduct:Show(productData)
                                    self:AddMarketProductEntryToLabeledGroupTable(self.featuredProducts, marketProduct)
                                end
                            else
                                local marketProduct = self.currentCategoryMarketProductPool:AcquireObject()
                                marketProduct:Show(productData)
                                self:AddMarketProductEntryToLabeledGroupTable(self:FindOrCreateCategoryLabeledGroupTable(categoryIndex, formattedBaseName), marketProduct)
                            end
                        end
                    end
                end
            end
        end
        self:SortMarketProductLabeledGroupTable(self.limitedTimedOfferProducts)
        self:SortMarketProductLabeledGroupTable(self.featuredProducts)
        local labeledGroupTables =
        {
            { name = GetString(SI_MARKET_ESO_PLUS_MEMBERSHIP_CATEGORY), marketProducts = esoPlusMembershipTiles },
            { name = GetString(SI_MARKET_LIMITED_TIME_OFFER_CATEGORY), marketProducts = self.limitedTimedOfferProducts },
            { name = GetString(SI_MARKET_FEATURED_CATEGORY), marketProducts = self.featuredProducts },
        }
        local sortedCategoryGroupTables = CreateSortedSubcategoryList(self.categoryLabeledGroupTableMap)
        for index, groupTableInfo in ipairs(sortedCategoryGroupTables) do
            local groupTable = groupTableInfo.groupTable
            self:SortMarketProductLabeledGroupTable(groupTable)
            table.insert(labeledGroupTables, { name = groupTableInfo.displayName, marketProducts = groupTable })
        end
        self:LayoutMarketProducts(labeledGroupTables)
    end
end
function GamepadMarket:LayoutMarketProducts(marketProductLabeledGroupTables)
    self:SetCurrentKeybinds(self.keybindStripDescriptors)
    self:PrepareGridForBuild(ZO_GAMEPAD_MARKET_INDIVIDUAL_PRODUCTS_PER_ROW, ZO_GAMEPAD_MARKET_INDIVIDUAL_PRODUCT_WIDTH, ZO_GAMEPAD_MARKET_INDIVIDUAL_PRODUCT_HEIGHT, ZO_GAMEPAD_MARKET_PRODUCT_PADDING)
    for i, labeledGroupTable in ipairs(marketProductLabeledGroupTables) do
        self:AddLabeledGroupTable(labeledGroupTable.name, labeledGroupTable.marketProducts)
    end
    self:FinishBuild()
end
function GamepadMarket:OnMarketOpen()
    if ZO_GAMEPAD_MARKET_LOCKED then -- This can be called before the lock screen has been initialized
        ZO_GAMEPAD_MARKET_LOCKED:OnMarketOpen()
    end
    if self.isInitialized then
        if not self.categoriesInitialized or self.refreshCategories then
            self:BuildCategories()
        else
            self:UpdateCurrentCategory()
        end
        self.isLockedForCategoryRefresh = true -- prevent double category initialization on first entry
        self:RefreshHeader()
        self.isLockedForCategoryRefresh = false
    end
end
function GamepadMarket:OnMarketLocked()
    if self.isInitialized then
        -- if the market locks while we are showing the Crown Store we want to switch to the locked screen
        if self.marketScene:IsShowing() then
            -- if we are in the base Crown Store we can just swap the current scene to the locked scene to preserve the stack
            SCENE_MANAGER:SwapCurrentScene(ZO_GAMEPAD_MARKET_LOCKED_SCENE_NAME)
        elseif GAMEPAD_MARKET_SCENE_GROUP:IsShowing() and not SCENE_MANAGER:IsShowing(ZO_GAMEPAD_MARKET_PURCHASE_SCENE_NAME) then
            -- otherwise if we are in another Crown Store scene then just show the lock screen since we don't
            -- know what's on the stack. Additionally, we won't interrupt the purchase dialog if that's showing.
            SCENE_MANAGER:Show(ZO_GAMEPAD_MARKET_LOCKED_SCENE_NAME)
        end
    end
end
-- If the Market is loading/updating switch to the pre-scene so we can show the loading info and then switch to the proper market state
function GamepadMarket:OnMarketLoading()
    if self.isInitialized and (self.marketScene:IsShowing() or ZO_GAMEPAD_MARKET_LOCKED_SCENE:IsShowing()) then
        SCENE_MANAGER:SwapCurrentScene(self.preSceneName)
    end
end
function GamepadMarket:OnMarketPurchaseResult()
    -- handled by GamepadMarketPurchaseManager
end
do
    local PURCHASE_MANAGER = ZO_GamepadMarketPurchaseManager:New() -- Singleton purchase manager
    local FROM_INGAME = true
    local FROM_CROWN_STORE = false
    function GamepadMarket:PurchaseMarketProduct(marketProductData)
        PURCHASE_MANAGER:BeginPurchase(marketProductData, FROM_INGAME)
    end
    function GamepadMarket:PurchaseMarketProductInternal(marketProductData, onPurchaseSuccessCallback, onPurchaseEndCallback)
        PURCHASE_MANAGER:BeginPurchase(marketProductData, FROM_CROWN_STORE, onPurchaseSuccessCallback, onPurchaseEndCallback)
    end
    function GamepadMarket:PurchaseFreeTrialMarketProductInternal(marketProductData, onPurchaseSuccessCallback, onPurchaseEndCallback)
        PURCHASE_MANAGER:BeginFreeTrialPurchase(marketProductData, FROM_CROWN_STORE, onPurchaseSuccessCallback, onPurchaseEndCallback)
    end
    function GamepadMarket:GiftMarketProduct(marketProductData)
        PURCHASE_MANAGER:BeginGiftPurchase(marketProductData, FROM_INGAME)
    end
    function GamepadMarket:GiftMarketProductInternal(marketProductData, onPurchaseSuccessCallback, onPurchaseEndCallback)
        PURCHASE_MANAGER:BeginGiftPurchase(marketProductData, FROM_CROWN_STORE, onPurchaseSuccessCallback, onPurchaseEndCallback)
    end
end
do
    local DONT_ALLOW_IF_DISABLED = false
    local SCROLL_INSTANTLY = true
    function GamepadMarket:RequestShowCategory(categoryIndex, subcategoryIndex)
        if self.isInitialized and self.marketScene:IsShowing() and self.marketState == MARKET_STATE_OPEN then
            -- subcategories are displayed as part of the parent category, so for now we'll just show the whole category
            local categoryData = self:GetCategoryData(categoryIndex)
            if categoryData then
                local targetIndex = categoryData.tabIndex
                if self.header.tabBar:GetSelectedIndex() ~= targetIndex then
                    self:SetQueuedCategoryIndices(categoryIndex, subcategoryIndex)
                    self.header.tabBar:SetSelectedDataIndex(targetIndex, DONT_ALLOW_IF_DISABLED, SCROLL_INSTANTLY)
                else
                    self:ScrollToSubcategory(categoryIndex, subcategoryIndex, SCROLL_INSTANTLY)
                end
            end
        else
            self:SetQueuedCategoryIndices(categoryIndex, subcategoryIndex)
        end
    end
end
function GamepadMarket:RequestShowCategoryById(categoryId)
    if self.isInitialized and self.marketScene:IsShowing() and self.marketState == MARKET_STATE_OPEN then
        local categoryIndex, subcategoryIndex = GetCategoryIndicesFromMarketProductCategoryId(self:GetDisplayGroup(), categoryId)
        self:RequestShowCategory(categoryIndex, subcategoryIndex)
        self:ClearQueuedCategoryId()
    else
        self:SetQueuedCategoryId(categoryId)
    end
end
function GamepadMarket:ShowBundleContents(bundleMarketProduct)
    self:Deactivate()
    self.isLockedForCategoryRefresh = true
    ZO_GAMEPAD_MARKET_BUNDLE_CONTENTS:SetBundle(bundleMarketProduct:GetMarketProductData())
    SCENE_MANAGER:Push(ZO_GAMEPAD_MARKET_BUNDLE_CONTENTS_SCENE_NAME)
end
function GamepadMarket:ShowMarketProductContentsAsList(marketProduct, previewType)
    self:Deactivate()
    self.isLockedForCategoryRefresh = true
    SCENE_MANAGER:Push(ZO_GAMEPAD_MARKET_CONTENT_LIST_SCENE_NAME)
    ZO_GAMEPAD_MARKET_PRODUCT_LIST:SetMarketProduct(marketProduct:GetId(), previewType)
end
function GamepadMarket:ShowHousePreviewDialog(marketProduct)
    local mainTextParams = { mainTextParams = ZO_MarketDialogs_Shared_GetPreviewHouseDialogMainTextParams(marketProduct:GetId()) }
    ZO_Dialogs_ShowGamepadDialog("CROWN_STORE_PREVIEW_HOUSE", { marketProductData = marketProduct:GetMarketProductData() }, mainTextParams)
end
function GamepadMarket:OnDialogShowing()
    if g_activeMarketScreen then
        g_activeMarketScreen:RemoveKeybinds()
        g_activeMarketScreen:Deactivate()
    end
end
function GamepadMarket:OnDialogHidden()
    if g_activeMarketScreen then
        g_activeMarketScreen:Activate()
        g_activeMarketScreen:AddKeybinds()
    end
end
function GamepadMarket:ClearProducts()
    ZO_GamepadMarket_GridScreen.ClearProducts(self)
    self.selectedGridEntry = nil
    self.selectedGridEntryCategoryName = nil
    KEYBIND_STRIP:UpdateKeybindButtonGroup(self.keybindStripDescriptors)
end
function GamepadMarket:GetMarketProductFromCurrentCategoryById(productId)
    for index, entry in ipairs(self.gridEntries) do
        if entry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT and entry:GetId() == productId then
            return entry
        end
    end
end
function GamepadMarket:GetFirstMarketProductInSubcategoryFromCurrentCategory(categoryIndex, subcategoryIndex)
    for index, entry in ipairs(self.gridEntries) do
        if entry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
            local productData = entry:GetMarketProductData()
            local productCategoryIndex, productSubcategoryIndex = productData:GetCategoryIndicesFromPresentation()
            if productCategoryIndex == categoryIndex and productSubcategoryIndex == subcategoryIndex then
                return entry
            end
        end
    end
end
function GamepadMarket:SwitchToMarketTemplateAndShowMarketProduct(alternateMarketTemplate, marketProductId)
    if not alternateMarketTemplate or not marketProductId then
        return false
    end
    -- Switch to the specified market template and attempt to show the specified market product.
    -- Order matters:
    self:ApplyMarketTemplate(alternateMarketTemplate)
    self:OnShown()
    self:ShowMarketProduct(marketProductId)
end
function GamepadMarket:RequestShowMarketProduct(marketProductId)
    if not (self.isInitialized and self.marketScene:IsShowing() and self.marketState == MARKET_STATE_OPEN) then
        -- The market is not yet initialized or is hidden; queue the market product for deferred display.
        self:SetQueuedMarketProductId(marketProductId)
        return
    end
    -- Identify the highest priority market template that offers this market product.
    local marketTemplate = nil
    for _, template in ipairs(ZO_GAMEPAD_PRIORITIZED_MARKET_TEMPLATES) do
        for _, filterType in ipairs(template.marketProductFilterTypes) do
            if DoesAnyMarketProductPresentationMatchFilter(marketProductId, filterType) then
                marketTemplate = template
                break
            end
        end
        if marketTemplate then
            break
        end
    end
    if marketTemplate == self.marketTemplate then
        self:ShowMarketProduct(marketProductId)
    elseif marketTemplate then
        -- The market product was not found in any category for the current market template;
        -- attempt to switch to the market that currently offers this product.
        self:SwitchToMarketTemplateAndShowMarketProduct(marketTemplate, marketProductId)
    end
end
do
    local DONT_ALLOW_IF_DISABLED = false
    local SCROLL_INSTANTLY = true
    function GamepadMarket:ShowMarketProduct(marketProductId)
        local data = self:GetCategoryDataForMarketProduct(marketProductId)
        if data then
            -- check if we are already showing the correct category for the market product
            -- if we aren't, we need to select it before we can scroll to the product
            local targetIndex = data.tabIndex
            if self.header.tabBar:GetSelectedIndex() ~= targetIndex then
                self.header.tabBar:SetSelectedDataIndex(targetIndex, DONT_ALLOW_IF_DISABLED, SCROLL_INSTANTLY)
            else
                self:ScrollToMarketProduct(marketProductId, SCROLL_INSTANTLY)
            end
        end
    end
end
function GamepadMarket:ScrollToMarketProductEntry(marketProduct, scrollInstantly)
    if self.showScrollbar then
        self:ScrollToGridEntry(marketProduct:GetFocusData(), scrollInstantly)
    end
    local listIndex = marketProduct:GetListIndex()
    self.focusList:SetFocusByIndex(listIndex)
end
function GamepadMarket:ScrollToMarketProduct(marketProductId, scrollInstantly)
    local targetMarketProduct = self:GetMarketProductFromCurrentCategoryById(marketProductId)
    if targetMarketProduct then
        self:ScrollToMarketProductEntry(targetMarketProduct, scrollInstantly)
        self:ClearQueuedMarketProductId()
    end
end
function GamepadMarket:ScrollToSubcategory(categoryIndex, subcategoryIndex, scrollInstantly)
    local targetMarketProduct = self:GetFirstMarketProductInSubcategoryFromCurrentCategory(categoryIndex, subcategoryIndex)
    if targetMarketProduct then
        self:ScrollToMarketProductEntry(targetMarketProduct, scrollInstantly)
        self:ClearQueuedCategoryIndices()
    end
end
function GamepadMarket:GetCategoryData(categoryIndex)
    local normalizedCategoryIndex
    if categoryIndex > 0 then
       normalizedCategoryIndex = categoryIndex + self.categoryIndexOffset
    elseif categoryIndex == ZO_MARKET_ESO_PLUS_CATEGORY_INDEX then
        normalizedCategoryIndex = self.esoPlusCategoryIndex
    elseif categoryIndex == ZO_MARKET_FEATURED_CATEGORY_INDEX then
        normalizedCategoryIndex = self.featuredCategoryIndex
    end
    if normalizedCategoryIndex then
        local categoryTable = self.headerData.tabBarEntries[normalizedCategoryIndex]
        if categoryTable ~= nil then
            return categoryTable.categoryData
        end
    end
end
function GamepadMarket:RefreshEsoPlusPage()
end
-- overrides from ZO_Market_Shared
function GamepadMarket:AddKeybinds()
    KEYBIND_STRIP:AddKeybindButtonGroup(self.currentKeybindDescriptor)
end
function GamepadMarket:RemoveKeybinds()
    KEYBIND_STRIP:RemoveKeybindButtonGroup(self.currentKeybindDescriptor)
end
function GamepadMarket:RefreshKeybinds()
    KEYBIND_STRIP:UpdateKeybindButtonGroup(self.currentKeybindDescriptor)
end
-- copy the standard gamepad style but change the center and right offsets
ZO_GAMEPAD_KEYBIND_STRIP_MARKET_GAMEPAD_STYLE = ZO_ShallowTableCopy(KEYBIND_STRIP_GAMEPAD_STYLE)
ZO_GAMEPAD_KEYBIND_STRIP_MARKET_GAMEPAD_STYLE.centerAnchorOffset = -230
    local currencyStyle = MARKET_CURRENCY_GAMEPAD:ModifyKeybindStripStyleForCurrency(ZO_GAMEPAD_KEYBIND_STRIP_MARKET_GAMEPAD_STYLE)
    KEYBIND_STRIP:SetStyle(currencyStyle)
end
--
--[[ Gamepad Market Bundle Contents ]]--
--
local GamepadMarketBundleContents = ZO_GamepadMarket_GridScreen:Subclass()
function GamepadMarketBundleContents:New(...)
    local bundleContents = ZO_Object.New(self)
    bundleContents:Initialize(...)
    return bundleContents
end
function GamepadMarketBundleContents:Initialize(control)
    ZO_GamepadMarket_GridScreen.Initialize(self, control)
    GAMEPAD_MARKET_BUNDLE_CONTENTS_SCENE = ZO_RemoteScene:New(ZO_GAMEPAD_MARKET_BUNDLE_CONTENTS_SCENE_NAME, SCENE_MANAGER)
    GAMEPAD_MARKET_BUNDLE_CONTENTS_SCENE:RegisterCallback("StateChange", function(...) self:OnStateChanged(...) end)
end
function GamepadMarketBundleContents:InitializeMarketProductPool()
    local BUNDLE_ATTACHMENT_NAME = self.control:GetName() .. ZO_GAMEPAD_MARKET_PRODUCT_BUNDLE_ATTACHMENT_TEMPLATE
    local function CreateMarketBundleAttachment(objectPool)
        return ZO_GamepadMarketProductBundleAttachment:New(objectPool:GetNextControlId(), self.currentCategoryControl, BUNDLE_ATTACHMENT_NAME)
    end
    self.marketBundleAttachmentPool = ZO_ObjectPool:New(CreateMarketBundleAttachment, ZO_ObjectPool_DefaultResetObject)
    local BLANK_TILE_NAME = self.control:GetName() .. ZO_GAMEPAD_MARKET_BLANK_TILE_TEMPLATE
    local function CreateBlankTile(objectPool)
        local control = CreateControlFromVirtual(BLANK_TILE_NAME, self.currentCategoryControl, ZO_GAMEPAD_MARKET_BLANK_TILE_TEMPLATE, objectPool:GetNextControlId())
        return ZO_GamepadMarketBlankTile:New(control)
    end
     self.blankTilePool = ZO_ObjectPool:New(CreateBlankTile, ZO_ObjectPool_DefaultResetObject)
end
function GamepadMarketBundleContents:FinishRowWithBlankTiles()
    local currentItemRowIndex = #self.gridEntries % self.itemsPerRow
    if currentItemRowIndex > 0 then
        for i = currentItemRowIndex, self.itemsPerRow - 1 do
            local blankTile = self.blankTilePool:AcquireObject()
            blankTile:Show()
            self:AddEntry(blankTile, blankTile:GetControl())
        end
    end
end
function GamepadMarketBundleContents:OnStateChanged(oldState, newState)
    if newState == SCENE_SHOWING then
        self:OnShowing()
    elseif newState == SCENE_SHOWN then
        self:OnShown()
    elseif newState == SCENE_HIDING then
        self:OnHiding()
    end
end
function GamepadMarketBundleContents:PerformDeferredInitialization()
    if not self.isInitialized then
        self.titleControl = self.header:GetNamedChild("TitleContainerTitle")
        local function RefreshOnPurchase()
            self:RefreshProducts()
        end
        local function QueueTutorial(_, purchasedConsumables, triggerTutorialOnPurchase)
            if purchasedConsumables then
                self:SetQueuedTutorial(TUTORIAL_TRIGGER_CROWN_CONSUMABLE_PURCHASED)
            end
            if triggerTutorialOnPurchase then
                self:SetQueuedTutorial(triggerTutorialOnPurchase)
            end
        end
        self.keybindStripDescriptors =
        {
            alignment = KEYBIND_STRIP_ALIGN_LEFT,
            -- Purchase Keybind
            {
                name = GetString(SI_MARKET_PURCHASE_BUNDLE_KEYBIND_TEXT),
                keybind = "UI_SHORTCUT_PRIMARY",
                visible = function()
                    return not self.marketProductData:IsPurchaseLocked()
                end,
                enabled = function() return not self:HasQueuedTutorial() end,
                callback = function()
                    self.prePurchaseSelectedIndex = self.focusList:GetSelectedIndex()
                    ZO_GAMEPAD_MARKET:PurchaseMarketProductInternal(self.marketProductData, RefreshOnPurchase, QueueTutorial)
                end,
            },
            -- Gift Keybind
            {
                 name = GetString(SI_MARKET_GIFT_BUNDLE_KEYBIND_TEXT),
                keybind = "UI_SHORTCUT_RIGHT_STICK",
                visible = function()
                                return self.marketProductData:IsGiftable()
                            end,
                enabled = function() return not self:HasQueuedTutorial() end,
                callback = function()
                    self.prePurchaseSelectedIndex = self.focusList:GetSelectedIndex()
                    ZO_GAMEPAD_MARKET:GiftMarketProductInternal(self.marketProductData, RefreshOnPurchase, QueueTutorial)
                end,
            },
            -- Preview keybind
            {
                name =  GetString(SI_MARKET_PREVIEW_KEYBIND_TEXT),
                keybind = "UI_SHORTCUT_SECONDARY",
                visible = function()
                    if self.selectedGridEntry then
                        return self.selectedGridEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT
                    end
                    return false
                end,
                enabled = function()
                    local selectedEntry = self.selectedGridEntry
                    if selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
                        local previewType = selectedEntry:GetMarketProductPreviewType()
                        if previewType == ZO_MARKET_PREVIEW_TYPE_PREVIEWABLE then
                            return selectedEntry:HasPreview() and IsCharacterPreviewingAvailable()
                        elseif previewType == ZO_MARKET_PREVIEW_TYPE_HOUSE then
                            return CanJumpToHouseFromCurrentLocation(), GetString(SI_MARKET_PREVIEW_ERROR_CANNOT_JUMP_FROM_LOCATION)
                        end
                    end
        
                    return false
                end,
                callback = function()
                    local selectedEntry = self.selectedGridEntry
                    if selectedEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
                        local previewType = selectedEntry:GetMarketProductPreviewType()
                        if previewType == ZO_MARKET_PREVIEW_TYPE_HOUSE then
                            self:ShowHousePreviewDialog(selectedEntry)
                        elseif previewType == ZO_MARKET_PREVIEW_TYPE_PREVIEWABLE then
                            self:BeginPreview(selectedEntry)
                        end
                    end
                end,
            },
            MARKET_TERTIARY_BUTTON_DESCRIPTOR,
            KEYBIND_STRIP:GetDefaultGamepadBackButtonDescriptor()
        }
        self.isInitialized = true
    end
end
function GamepadMarketBundleContents:SetBundle(marketProductData)
    self.marketProductData = marketProductData
    self.headerData.titleText = zo_strformat(SI_MARKET_PRODUCT_NAME_FORMATTER, marketProductData:GetDisplayName())
    self.prePurchaseSelectedIndex = nil
end
function GamepadMarketBundleContents:LayoutBundleProducts()
    self:ClearProducts()
    self:PrepareGridForBuild(ZO_GAMEPAD_MARKET_INDIVIDUAL_PRODUCTS_PER_ROW, ZO_GAMEPAD_MARKET_INDIVIDUAL_PRODUCT_WIDTH, ZO_GAMEPAD_MARKET_INDIVIDUAL_PRODUCT_HEIGHT, ZO_GAMEPAD_MARKET_PRODUCT_PADDING)
    local numChildren = self.marketProductData:GetNumChildren()
    if numChildren > 0 then
        local childTiles = {}
        for childIndex = 1, numChildren do
            local childMarketProductId = self.marketProductData:GetChildMarketProductId(childIndex)
            local productData = ZO_MarketProductData:New(childMarketProductId, ZO_INVALID_PRESENTATION_INDEX)
            local displayName = productData:GetDisplayName()
            local marketProduct = self.marketBundleAttachmentPool:AcquireObject()
            marketProduct:SetBundleMarketProductId(self.marketProductData:GetId())
            marketProduct:Show(productData)
            table.insert(childTiles, {displayName = displayName, marketProduct = marketProduct})
        end
        -- Sort the child tiles alphabetically
        table.sort(childTiles, function(a,b)
                                        return a.displayName < b.displayName
                                    end)
        for i = 1, #childTiles do
            local marketProduct = childTiles[i].marketProduct
            self:AddEntry(marketProduct, marketProduct:GetControl())
        end
    end
    self:FinishBuild()
end
function GamepadMarketBundleContents:OnShowing()
    self.titleControl:SetText(self.headerData.titleText)
    self:AddKeybinds()
    self:Activate()
    if self.prePurchaseSelectedIndex then
        self.focusList:SetFocusByIndex(self.prePurchaseSelectedIndex)
        self.prePurchaseSelectedIndex = nil
    end
end
function GamepadMarketBundleContents:OnShown()
    g_activeMarketScreen = ZO_GAMEPAD_MARKET_BUNDLE_CONTENTS
    ZO_GamepadMarket_GridScreen.OnShown(self)
end
function GamepadMarketBundleContents:OnHiding()
    self:RemoveKeybinds()
    self:Deactivate()
end
function GamepadMarketBundleContents:LayoutSelectedGridEntryTooltip()
    if self.selectedGridEntry ~= nil and self.selectedGridEntry:GetEntryType() == ZO_GAMEPAD_MARKET_ENTRY_MARKET_PRODUCT then
        self.selectedGridEntry:LayoutTooltip(GAMEPAD_RIGHT_TOOLTIP)
    else
        GAMEPAD_TOOLTIPS:ClearTooltip(GAMEPAD_RIGHT_TOOLTIP)
    end
end
function GamepadMarketBundleContents:OnSelectionChanged(selectedData)
    ZO_GamepadMarket_GridScreen.OnSelectionChanged(self, selectedData)
    local previouslySelectedEntry = self.selectedGridEntry
    if selectedData then
        self.selectedGridEntry = selectedData.object
    else
        GAMEPAD_TOOLTIPS:ClearTooltip(GAMEPAD_RIGHT_TOOLTIP)
        self.selectedGridEntry = nil
    end
    self:UpdatePreviousAndNewlySelectedProducts(previouslySelectedEntry, self.selectedGridEntry)
    --Re-narrate on selection changed
    SCREEN_NARRATION_MANAGER:QueueFocus(self.focusList)
end
function GamepadMarketBundleContents:ReleaseAllProducts()
    self.marketBundleAttachmentPool:ReleaseAllObjects()
    self.blankTilePool:ReleaseAllObjects()
end
function GamepadMarketBundleContents:ClearProducts()
    ZO_GamepadMarket_GridScreen.ClearProducts(self)
end
function GamepadMarketBundleContents:AddKeybinds()
    KEYBIND_STRIP:AddKeybindButtonGroup(self.keybindStripDescriptors)
end
function GamepadMarketBundleContents:RemoveKeybinds()
    KEYBIND_STRIP:RemoveKeybindButtonGroup(self.keybindStripDescriptors)
end
function GamepadMarketBundleContents:RefreshKeybinds()
    KEYBIND_STRIP:UpdateKeybindButtonGroup(self.keybindStripDescriptors)
end
function GamepadMarketBundleContents:ShowHousePreviewDialog(marketProduct)
    local mainTextParams = { mainTextParams = ZO_MarketDialogs_Shared_GetPreviewHouseDialogMainTextParams(marketProduct:GetId()) }
    ZO_Dialogs_ShowGamepadDialog("CROWN_STORE_PREVIEW_HOUSE", { marketProductData = marketProduct:GetMarketProductData() }, mainTextParams)
end
-- override from ZO_GamepadMarket_GridScreen
function GamepadMarketBundleContents:FinishBuild()
    ZO_GamepadMarket_GridScreen.FinishBuild(self)
end
-- override from ZO_GamepadMarket_GridScreen
function GamepadMarketBundleContents:EndCurrentPreview()
    ZO_GamepadMarket_GridScreen.EndCurrentPreview(self)
end
-- MarketClasses_Shared expects us to have this function (because Market_Shared does)
function GamepadMarketBundleContents:RefreshActions()
end
--
-- [[ Market Locked Screen ]]--
--
local GamepadMarketLockedScreen = ZO_Object:Subclass()
function GamepadMarketLockedScreen:New(...)
    local locked = ZO_Object.New(self)
    locked:Initialize(...)
    return locked
end
function GamepadMarketLockedScreen:Initialize(control)
    control.owner = self
    self.control = control
    self.keybindStripDescriptors =
    {
        alignment = KEYBIND_STRIP_ALIGN_LEFT,
        KEYBIND_STRIP:GetDefaultGamepadBackButtonDescriptor()
    }
    ZO_GAMEPAD_MARKET_LOCKED_SCENE = ZO_RemoteScene:New(ZO_GAMEPAD_MARKET_LOCKED_SCENE_NAME, SCENE_MANAGER)
    ZO_GAMEPAD_MARKET_LOCKED_SCENE:RegisterCallback("StateChange", function(...) self:OnStateChanged(...) end)
end
function GamepadMarketLockedScreen:OnStateChanged(oldState, newState)
    if newState == SCENE_SHOWN then
        if GetMarketState(ZO_GAMEPAD_MARKET:GetDisplayGroup()) == MARKET_STATE_OPEN then
            self:OnMarketOpen()
        else
            KEYBIND_STRIP:AddKeybindButtonGroup(self.keybindStripDescriptors)
        end
    elseif newState == SCENE_HIDDEN then
        KEYBIND_STRIP:RemoveKeybindButtonGroup(self.keybindStripDescriptors)
        KEYBIND_STRIP:SetStyle(KEYBIND_STRIP_STANDARD_STYLE)
    end
end
function GamepadMarketLockedScreen:OnMarketOpen()
    if ZO_GAMEPAD_MARKET_LOCKED_SCENE:IsShowing() then
        SCENE_MANAGER:SwapCurrentScene(ZO_GAMEPAD_MARKET_SCENE_NAME)
    end
end
--
-- [[ Market Pre Scene ]]--
--
local GamepadMarketPreScene = ZO_InitializingObject:Subclass()
-- 'marketTemplate' is a reference to a single ZO_GAMEPAD_MARKET_TEMPLATES options table
-- 'sceneName' is the string name for the associated pre-scene
-- 'sceneFragment' is a reference to the associated pre-scene fragment
function GamepadMarketPreScene:Initialize(control, marketTemplate, sceneName, sceneFragment)
    self.control = control
    self.marketTemplate = marketTemplate
    self.sceneName = sceneName
    self.sceneFragment = sceneFragment
    self.keybindStripDescriptors =
    {
        alignment = KEYBIND_STRIP_ALIGN_LEFT,
        KEYBIND_STRIP:GetDefaultGamepadBackButtonDescriptor()
    }
    self.scene = ZO_RemoteScene:New(self.sceneName, SCENE_MANAGER)
    self.scene:RegisterCallback("StateChange", function(...) self:OnStateChanged(...) end)
end
function GamepadMarketPreScene:OnStateChanged(oldState, newState)
    if newState == SCENE_SHOWN then
        self:OnShown()
    elseif newState == SCENE_HIDING then
        self:OnHiding()
    end
end
function GamepadMarketPreScene:OnShown()
    self.marketState = GetMarketState(ZO_GAMEPAD_MARKET:GetDisplayGroup())
    self.loadingStartTime = nil
    EVENT_MANAGER:RegisterForEvent(self.sceneName, EVENT_MARKET_STATE_UPDATED, function(eventId, ...) self:OnMarketStateUpdated(...) end)
    EVENT_MANAGER:RegisterForUpdate(self.sceneName, 0, function(...) self:OnUpdate(...) end)
    OpenMarket(ZO_GAMEPAD_MARKET:GetDisplayGroup())
end
function GamepadMarketPreScene:OnHiding()
    EVENT_MANAGER:UnregisterForUpdate(self.sceneName)
    EVENT_MANAGER:UnregisterForEvent(self.sceneName, EVENT_MARKET_STATE_UPDATED)
    self.scene:RemoveFragment(self.sceneFragment)
    KEYBIND_STRIP:RemoveKeybindButtonGroup(self.keybindStripDescriptors)
end
function GamepadMarketPreScene:OnMarketStateUpdated(displayGroup, marketState)
    if displayGroup == ZO_GAMEPAD_MARKET:GetDisplayGroup() then
        self.marketState = marketState
        self:TrySwapToMarketScene()
    end
end
function GamepadMarketPreScene:OnUpdate(currentMs)
    if self.loadingStartTime == nil then
        self.loadingStartTime = currentMs
    end
    if currentMs - self.loadingStartTime >= ZO_MARKET_DISPLAY_LOADING_DELAY_MS then
        -- show the loading text
        self.scene:AddFragment(self.sceneFragment)
        KEYBIND_STRIP:AddKeybindButtonGroup(self.keybindStripDescriptors)
    end
end
function GamepadMarketPreScene:TrySwapToMarketScene()
    if self.scene:IsShowing() then
        if self.marketState == MARKET_STATE_OPEN then
            ZO_GAMEPAD_MARKET:ApplyMarketTemplate(self.marketTemplate)
            SCENE_MANAGER:SwapCurrentScene(ZO_GAMEPAD_MARKET_SCENE_NAME)
        elseif self.marketState == MARKET_STATE_LOCKED then
            ZO_GAMEPAD_MARKET:ApplyMarketTemplate(self.marketTemplate)
            SCENE_MANAGER:SwapCurrentScene(ZO_GAMEPAD_MARKET_LOCKED_SCENE_NAME)
        end
    end
end
function GamepadMarketPreScene:GetScene()
    return self.scene
end
function GamepadMarketPreScene:GetSceneFragment()
    return self.sceneFragment
end
--
--[[ XML Handlers ]]--
--
    ZO_GAMEPAD_MARKET = GamepadMarket:New(control)
    SYSTEMS:RegisterGamepadObject(ZO_MARKET_NAME, ZO_GAMEPAD_MARKET)
end
    ZO_GAMEPAD_MARKET_BUNDLE_CONTENTS = GamepadMarketBundleContents:New(control)
end
    ZO_GAMEPAD_MARKET_LOCKED = GamepadMarketLockedScreen:New(control)
end
    local MARKET_TEMPLATE = ZO_GAMEPAD_MARKET_TEMPLATES.CROWN_STORE
    local PRE_SCENE_NAME = ZO_GAMEPAD_MARKET_PRE_SCENE_NAME
    local PRE_SCENE_FRAGMENT = ZO_FadeSceneFragment:New(ZO_GamepadMarket_PreScene)
    ZO_MARKET_PRE_SCENE = GamepadMarketPreScene:New(control, MARKET_TEMPLATE, PRE_SCENE_NAME, PRE_SCENE_FRAGMENT)
    ZO_GAMEPAD_MARKET_PRE_SCENE = ZO_MARKET_PRE_SCENE:GetScene()
end
    local MARKET_TEMPLATE = ZO_GAMEPAD_MARKET_TEMPLATES.SEAL_STORE
    local PRE_SCENE_NAME = ZO_GAMEPAD_ENDEAVOR_SEAL_MARKET_PRE_SCENE_NAME
    local PRE_SCENE_FRAGMENT = ZO_FadeSceneFragment:New(ZO_GamepadEndeavorSealMarket_PreScene)
    ZO_ENDEAVOR_SEAL_MARKET_PRE_SCENE = GamepadMarketPreScene:New(control, MARKET_TEMPLATE, PRE_SCENE_NAME, PRE_SCENE_FRAGMENT)
    ZO_GAMEPAD_ENDEAVOR_SEAL_MARKET_PRE_SCENE = ZO_ENDEAVOR_SEAL_MARKET_PRE_SCENE:GetScene()
end