-----------------------------------
-- Area: Northern San d'Oria
--  NPC: Alphollon C Meriard
-- Type: Purifies cursed items with their corresponding abjurations.
-- !pos 98.108 -1 137.999 231
-----------------------------------
local ID = require("scripts/zones/Northern_San_dOria/IDs")
-----------------------------------

function onTrade(player, npc, trade)
    if (trade:getItemCount() == 2) then
        local item = 0
        local reward = 0
        local abjurList =
        {
            -- Abjuration, item, item -1, reward, reward +1 (5 ID's per transaction)
            1314, 1344, 1345, 13934, 13935,    1315, 1346, 1347, 14387, 14388,
            1316, 1348, 1349, 14821, 14822,    1317, 1350, 1351, 14303, 14304,
            1318, 1352, 1353, 14184, 14185,    1319, 1354, 1355, 12429, 13924,
            1320, 1356, 1357, 12557, 14371,    1321, 1358, 1359, 12685, 14816,
            1322, 1360, 1361, 12813, 14296,    1323, 1362, 1363, 12941, 14175,
            1324, 1364, 1365, 13876, 13877,    1325, 1366, 1367, 13787, 13788,
            1326, 1368, 1369, 14006, 14007,    1327, 1370, 1371, 14247, 14248,
            1328, 1372, 1373, 14123, 14124,    1329, 1374, 1375, 12421, 13911,
            1330, 1376, 1377, 12549, 14370,    1331, 1378, 1379, 12677, 14061,
            1332, 1380, 1381, 12805, 14283,    1333, 1382, 1383, 12933, 14163,
            1334, 1384, 1385, 13908, 13909,    1335, 1386, 1387, 14367, 14368,
            1336, 1388, 1389, 14058, 14059,    1337, 1390, 1391, 14280, 14281,
            1338, 1392, 1393, 14160, 14161,    1339, 1394, 1395, 13927, 13928,
            1340, 1396, 1397, 14378, 14379,    1341, 1398, 1399, 14076, 14077,
            1342, 1400, 1401, 14308, 14309,    1343, 1402, 1403, 14180, 14181,
            2429, 2439, 2440, 16113, 16114,    2430, 2441, 2442, 14573, 14574,
            2431, 2443, 2444, 14995, 14996,    2432, 2445, 2446, 15655, 15656,
            2433, 2447, 2448, 15740, 15741,    2434, 2449, 2450, 16115, 16116,
            2435, 2451, 2452, 14575, 14576,    2436, 2453, 2454, 14997, 14998,
            2437, 2455, 2456, 15657, 15658,    2438, 2457, 2458, 15742, 15743,
            1441, 4234, 4234, 4513, 4513,      1442, 4235, 4235, 4511, 4511,
            3559, 10419, 10424, 10400, 10405,   3564, 10420, 10425, 10401, 10406,
            3560, 10240, 10245, 11873, 10489,   3565, 10241, 10246, 11874, 10490,
            3561, 10303, 10308, 10534, 10539,   3566, 10304, 10309, 10535, 10540,
            3562, 10583, 10588, 10565, 10570,   3567, 10584, 10589, 10566, 10571,
            3563, 10353, 10358, 10631, 10636,   3568, 10354, 10359, 10632, 10637,
            3569, 10421, 10426, 10402, 10407,   3574, 10422, 10427, 10403, 10408,
            3570, 10242, 10247, 11875, 10491,   3575, 10243, 10248, 11876, 10492,
            3571, 10305, 10310, 10536, 10541,   3576, 10306, 10311, 10537, 10542,
            3572, 10585, 10590, 10567, 10572,   3577, 10586, 10591, 10568, 10573,
            3573, 10355, 10360, 10633, 10638,   3578, 10356, 10361, 10634, 10639,
            3579, 10423, 10428, 10404, 10409,   8777, 26680, 26681, 26668, 26669,
            3580, 10244, 10249, 11877, 10493,   8778, 26856, 26857, 26844, 26845,
            3581, 10307, 10312, 10538, 10543,   8779, 27032, 27033, 20720, 20721,
            3582, 10587, 10592, 10569, 10574,   8780, 27208, 27209, 27196, 27197,
            3583, 10357, 10362, 10635, 10640,   8781, 27384, 27385, 27372, 27373,
            8762, 26682, 26683, 26670, 26671,   8767, 26684, 26685, 26672, 26673,
            8763, 26858, 26859, 26846, 26847,   8768, 26860, 26861, 26848, 26849,
            8764, 27034, 27035, 20722, 20723,   8769, 27036, 27037, 20724, 20725,
            8765, 27210, 27211, 27198, 27199,   8770, 27212, 27213, 27200, 27201,
            8766, 27386, 27387, 27374, 27375,   8771, 27388, 27389, 27376, 27377,
            8772, 26686, 26687, 26674, 26675,   8787, 26690, 26691, 26676, 26677,
            8773, 26862, 26863, 26850, 26851,   8788, 26866, 26867, 26852, 26853,
            8774, 27038, 27039, 20726, 20727,   8789, 27042, 27043, 20728, 20729,
            8775, 27214, 27215, 27202, 27203,   8790, 27218, 27219, 27204, 27205,
            8776, 27390, 27391, 27378, 27379,   8791, 27394, 27395, 27380, 27381,
            8782, 26688, 26689, 26678, 26679,   9105, 25619, 25620, 25609, 25610,
            8783, 26864, 26865, 26854, 26855,   9106, 25692, 25693, 25682, 25683,
            8784, 27040, 27041, 20730, 20731,   9107, 27123, 27124, 27113, 27114,
            8785, 27216, 27217, 27206, 27207,   9108, 27308, 27309, 27298, 27299,
            8786, 27392, 27393, 27382, 27383,   9109, 27479, 27480, 27469, 27470,
            9110, 25621, 25622, 25611, 25611,   9115, 25623, 25624, 25613, 25614,
            9111, 25694, 25695, 25684, 25685,   9116, 25696, 25697, 25686, 25687,
            9112, 27125, 27126, 27115, 27116,   9117, 27127, 27128, 27117, 27118,
            9113, 27310, 27311, 27300, 27301,   9118, 27312, 27313, 27302, 27303,
            9114, 27481, 27482, 27471, 27472,   9119, 27483, 27484, 27473, 27474,
            9120, 25625, 25626, 25615, 25616,   9125, 25627, 25628, 25617, 25618,
            9121, 25698, 25699, 25688, 25689,   9126, 25700, 25701, 25690, 25691,
            9122, 27129, 27130, 27119, 27120,   9127, 27131, 27132, 27121, 27122,
            9123, 27314, 27315, 27304, 27305,   9128, 27316, 27317, 27306, 27307,
            9124, 27485, 27486, 27475, 27476,   9129, 27487, 27488, 27477, 27478
        }
        for i = 1, #abjurList, 5 do
            if (trade:hasItemQty(abjurList[i], 1)) then
                if (trade:hasItemQty(abjurList[i + 1], 1)) then
                    item = abjurList[i + 1]
                    reward = abjurList[i + 3]
                elseif (trade:hasItemQty(abjurList[i + 2], 1)) then
                    item = abjurList[i + 2]
                    reward = abjurList[i + 4]
                end
                break
            end
        end

        if (reward ~= 0) then
            --Trade pair for a nice reward.
            player:startEvent(720, item, reward)
            player:setCharVar("reward", reward)
        end
    end
end

function onTrigger(player, npc)
    player:startEvent(719)
end

function onEventUpdate(player, csid, option)
end

function onEventFinish(player, csid, option)
    if (csid == 720) then
        local reward = player:getCharVar("reward")
        if (reward ~= 0) then
            player:tradeComplete()
            player:addItem(reward)
            player:setCharVar("reward", 0)
            player:messageSpecial(ID.text.ITEM_OBTAINED, reward)
        end
    end
end
