var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { pad0xBegin } from "../../../utils/common/utils";
import { triggerContractByContractName, viewContractByContractName } from "../../../utils/ethers/contractHelper";
/**
 * Goldrush Join
 * @param signer Ethers signer
 * @param eventId The event id which to join
 * @param landTokenId The land token id which to join
 * @param amount The ring amount which to submit
 * @param subAddr The dvm address for receiving the new land
 * @param callback Callback
 */
export const goldRushRaffleJoin = (landId, signer, eventId, landTokenId, amount, subAddr, callback) => {
    return triggerContractByContractName(landId, signer, "goldRushRaffle", "join", [eventId, pad0xBegin(landTokenId), amount, subAddr], callback);
};
/**
 * Goldrush Change Amount
 * @param signer Ethers signer
 * @param eventId Event id
 * @param landTokenId Land token id
 * @param amount Amount (to change)
 * @param callback Callback
 */
export const goldRushRaffleChangeAmount = (landId, signer, eventId, landTokenId, amount, callback) => {
    return triggerContractByContractName(landId, signer, "goldRushRaffle", "changeAmount", [eventId, pad0xBegin(landTokenId), amount], callback);
};
/**
 * Goldrush Change Subaddress
 * @param signer Ethers signer
 * @param eventId Event id
 * @param landTokenId Land token id
 * @param subAddr Sub address (to change)
 * @param callback Callback
 */
export const goldRushRaffleChangeSubAddr = (landId, signer, eventId, landTokenId, subAddr, callback) => {
    return triggerContractByContractName(landId, signer, "goldRushRaffle", "changeSubAddr", [eventId, pad0xBegin(landTokenId), subAddr], callback);
};
/**
 * Goldrush Change Info
 * @param signer Ethers signer
 * @param eventId Event id
 * @param landTokenId Land token id
 * @param amount Amount (to change)
 * @param subAddr Sub address (to change)
 * @param callback Callback
 */
export const goldRushRaffleChangeInfo = (landId, signer, eventId, landTokenId, amount, subAddr, callback) => {
    return triggerContractByContractName(landId, signer, "goldRushRaffle", "change", [eventId, pad0xBegin(landTokenId), amount, subAddr], callback);
};
/**
 * Goldrush Exit
 * @param signer Ethers signer
 * @param eventId Event id
 * @param landTokenId Land token id
 * @param callback Callback
 */
export const goldRushRaffleExit = (landId, signer, eventId, landTokenId, callback) => {
    return triggerContractByContractName(landId, signer, "goldRushRaffle", "exit", [eventId, pad0xBegin(landTokenId)], callback);
};
/**
 * Goldrush Draw
 * @param signer Ethers signer
 * @param eventId Event id
 * @param landTokenId Land token id
 * @param isWon Is won or not
 * @param param4  hashmessage, v, r, s
 * @param callback Callback
 */
export const goldRushRaffleDraw = (landId, signer, eventId, landTokenId, isWon, { hashmessage, v, r, s }, callback) => {
    return triggerContractByContractName(landId, signer, "goldRushRaffle", "draw", [eventId, pad0xBegin(landTokenId), isWon, hashmessage, v, r, s], callback);
};
/**
 * Goldrush History
 * @param provider Ethers provider
 * @param eventId Event id
 * @param landTokenId Land token id
 * @param callback Callback
 * @returns String balance or '0'
 */
export const goldRushRaffleGetHistory = (landId, provider, eventId, landTokenId, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const results = yield viewContractByContractName(landId, provider, "goldRushRaffle", "lands", [eventId, pad0xBegin(landTokenId)], callback);
    return results.balance.toString();
});
export const goldRushRaffleGetEvents = (landId, provider, callback) => __awaiter(void 0, void 0, void 0, function* () {
    const eventsInfo = [];
    const fromLandRes = yield viewContractByContractName(landId, provider, "goldRushRaffle", "fromLandId", [], callback);
    const fromLandId = fromLandRes.length ? fromLandRes[0].toNumber() : Number(0);
    // TODO: 'eventId < 10' is not a wise approach
    for (let eventId = 0; eventId < 10; eventId++) {
        const eventsRes = yield viewContractByContractName(landId, provider, "goldRushRaffle", "events", [eventId], callback);
        const toLandId = eventsRes.toLandId.toNumber();
        if (toLandId) {
            eventsInfo.push({
                eventId,
                fromLandId,
                toLandId,
                startTime: eventsRes.startBlock.toNumber(),
                endTime: eventsRes.endBlock.toNumber(),
                finalTime: eventsRes.finalBlock.toNumber(),
                expireTime: eventsRes.expireBlock.toNumber(),
            });
        }
    }
    return eventsInfo;
});
