// SPDX-License-Identifier: Apache-2.0
import { ethers } from "ethers";
import { BondUSAFacet__factory, EquityUSAFacet__factory, SnapshotsFacet__factory, ScheduledCrossOrderedTasksFacet__factory, Bond__factory, ProceedRecipientsFacet__factory, FixedRate__factory, KpiLinkedRate__factory, Kpis__factory, } from "@hashgraph/asset-tokenization-contracts";
import { GAS } from "../../../../core/Constants";
import { CastRateStatus } from "../../../../domain/context/bond/RateStatus";
import LogService from "../../../../app/service/log/LogService";
export class SecurityOperations {
    executor;
    constructor(executor) {
        this.executor = executor;
    }
    async setCoupon(security, recordDate, executionDate, rate, startDate, endDate, fixingDate, rateStatus, securityId) {
        LogService.logTrace(`bond: ${security} , recordDate :${recordDate} , executionDate: ${executionDate}, rate : ${rate}, startDate: ${startDate}, endDate: ${endDate}, fixingDate: ${fixingDate}, rateStatus: ${rateStatus}`);
        const coupon = {
            recordDate: recordDate.toHexString(),
            executionDate: executionDate.toHexString(),
            rate: rate.toHexString(),
            rateDecimals: rate.decimals,
            startDate: startDate.toBigInt(),
            endDate: endDate.toBigInt(),
            fixingDate: fixingDate.toBigInt(),
            rateStatus: CastRateStatus.toNumber(rateStatus),
        };
        return this.executor.executeContractCall(securityId.toString(), BondUSAFacet__factory.createInterface(), "setCoupon", [coupon], GAS.SET_COUPON);
    }
    async cancelCoupon(security, couponId, securityId) {
        LogService.logTrace(`Cancelling coupon: ${couponId} for bond: ${security}`);
        return this.executor.executeContractCall(securityId.toString(), BondUSAFacet__factory.createInterface(), "cancelCoupon", [couponId], GAS.CANCEL_COUPON);
    }
    setRate(security, rate, rateDecimals, securityId) {
        LogService.logTrace(`Setting Rate ${rate.toString()} with decimals ${rateDecimals} for security ${security.toString()}`);
        return this.executor.executeContractCall(securityId.toString(), FixedRate__factory.createInterface(), "setRate", [rate, rateDecimals], GAS.SET_RATE);
    }
    setInterestRate(security, maxRate, baseRate, minRate, startPeriod, startRate, missedPenalty, reportPeriod, rateDecimals, securityId) {
        LogService.logTrace(`Setting Interest Rate for security ${security.toString()}`);
        return this.executor.executeContractCall(securityId.toString(), KpiLinkedRate__factory.createInterface(), "setInterestRate", [
            {
                maxRate,
                baseRate,
                minRate,
                startPeriod,
                startRate,
                missedPenalty,
                reportPeriod,
                rateDecimals,
            },
        ], GAS.SET_INTEREST_RATE);
    }
    setImpactData(security, maxDeviationCap, baseLine, maxDeviationFloor, impactDataDecimals, adjustmentPrecision, securityId) {
        LogService.logTrace(`Setting Impact Data for security ${security.toString()}`);
        return this.executor.executeContractCall(securityId.toString(), KpiLinkedRate__factory.createInterface(), "setImpactData", [
            {
                maxDeviationCap,
                baseLine,
                maxDeviationFloor,
                impactDataDecimals,
                adjustmentPrecision,
            },
        ], GAS.SET_IMPACT_DATA);
    }
    addKpiData(security, date, value, project, securityId) {
        LogService.logTrace(`Adding KPI data for security ${security.toString()}, date: ${date}, value: ${value}, project: ${project.toString()}`);
        return this.executor.executeContractCall(securityId.toString(), new ethers.Interface(Kpis__factory.abi), "addKpiData", [date, value, project.toString()], GAS.ADD_KPI_DATA);
    }
    async updateMaturityDate(security, maturityDate, securityId) {
        LogService.logTrace(`Updating bond maturity date ${maturityDate} for security ${security.toString()}`);
        return this.executor.executeContractCall(securityId.toString(), new ethers.Interface(Bond__factory.abi), "updateMaturityDate", [maturityDate], GAS.UPDATE_MATURITY_DATE);
    }
    addProceedRecipient(security, proceedRecipient, data, securityId) {
        LogService.logTrace(`Adding proceedRecipient: ${proceedRecipient} to security: ${security}`);
        return this.executor.executeContractCall(securityId.toString(), ProceedRecipientsFacet__factory.createInterface(), "addProceedRecipient", [proceedRecipient.toString(), data], GAS.ADD_PROCEED_RECIPIENT);
    }
    removeProceedRecipient(security, proceedRecipient, securityId) {
        LogService.logTrace(`Removing proceedRecipient: ${proceedRecipient} from security: ${security}`);
        return this.executor.executeContractCall(securityId.toString(), ProceedRecipientsFacet__factory.createInterface(), "removeProceedRecipient", [proceedRecipient.toString()], GAS.REMOVE_PROCEED_RECIPIENT);
    }
    updateProceedRecipientData(security, proceedRecipient, data, securityId) {
        LogService.logTrace(`Updating proceedRecipient: ${proceedRecipient} data in security: ${security}`);
        return this.executor.executeContractCall(securityId.toString(), ProceedRecipientsFacet__factory.createInterface(), "updateProceedRecipientData", [proceedRecipient.toString(), data], GAS.UPDATE_PROCEED_RECIPIENT);
    }
    async setDividend(security, recordDate, executionDate, amount, securityId) {
        LogService.logTrace(`equity: ${security} , recordDate :${recordDate} , executionDate: ${executionDate}, amount : ${amount}`);
        const dividend = {
            recordDate: recordDate.toHexString(),
            executionDate: executionDate.toHexString(),
            amount: amount.toHexString(),
            amountDecimals: amount.decimals,
        };
        return this.executor.executeContractCall(securityId.toString(), EquityUSAFacet__factory.createInterface(), "setDividend", [dividend], GAS.SET_DIVIDENDS);
    }
    async cancelDividend(security, dividendId, securityId) {
        return this.executor.executeContractCall(securityId.toString(), EquityUSAFacet__factory.createInterface(), "cancelDividend", [dividendId], GAS.CANCEL_DIVIDEND);
    }
    async setVotingRights(security, recordDate, data, securityId) {
        LogService.logTrace(`equity: ${security} , recordDate :${recordDate}`);
        const voting = {
            recordDate: recordDate.toHexString(),
            data: data,
        };
        return this.executor.executeContractCall(securityId.toString(), EquityUSAFacet__factory.createInterface(), "setVoting", [voting], GAS.SET_VOTING_RIGHTS);
    }
    async cancelVoting(security, votingId, securityId) {
        LogService.logTrace(`Cancelling voting: ${votingId} for equity: ${security}`);
        return this.executor.executeContractCall(securityId.toString(), EquityUSAFacet__factory.createInterface(), "cancelVoting", [votingId], GAS.CANCEL_VOTING);
    }
    async setScheduledBalanceAdjustment(security, executionDate, factor, decimals, securityId) {
        LogService.logTrace(`equity: ${security} , executionDate :${executionDate} , factor: ${factor}, decimals : ${decimals}`);
        const scheduleBalanceAdjustment = {
            executionDate: executionDate.toHexString(),
            factor: factor.toHexString(),
            decimals: decimals.toHexString(),
        };
        return this.executor.executeContractCall(securityId.toString(), EquityUSAFacet__factory.createInterface(), "setScheduledBalanceAdjustment", [scheduleBalanceAdjustment], GAS.SET_SCHEDULED_BALANCE_ADJUSTMENT);
    }
    async cancelScheduledBalanceAdjustment(security, balanceAdjustmentId, securityId) {
        return this.executor.executeContractCall(securityId.toString(), EquityUSAFacet__factory.createInterface(), "cancelScheduledBalanceAdjustment", [balanceAdjustmentId], GAS.CANCEL_SCHEDULED_BALANCE_ADJUSTMENT);
    }
    async takeSnapshot(security, securityId) {
        LogService.logTrace(`Take snapshot of: ${security.toString()}`);
        return this.executor.executeContractCall(securityId.toString(), SnapshotsFacet__factory.createInterface(), "takeSnapshot", [], GAS.TAKE_SNAPSHOT);
    }
    async triggerPendingScheduledSnapshots(security, securityId) {
        LogService.logTrace(`Triggering pending scheduled snapshots for ${security.toString()}`);
        return this.executor.executeContractCall(securityId.toString(), ScheduledCrossOrderedTasksFacet__factory.createInterface(), "triggerPendingScheduledCrossOrderedTasks", [], GAS.TRIGGER_PENDING_SCHEDULED_SNAPSHOTS);
    }
    async triggerScheduledSnapshots(security, max, securityId) {
        LogService.logTrace(`Triggering up to ${max.toString()} pending scheduled snapshots for ${security.toString()}`);
        return this.executor.executeContractCall(securityId.toString(), ScheduledCrossOrderedTasksFacet__factory.createInterface(), "triggerScheduledCrossOrderedTasks", [max.toHexString()], GAS.TRIGGER_PENDING_SCHEDULED_SNAPSHOTS);
    }
}
