import Transaction from "../../apiwrapper/transaction/Transaction.js";
import AbstractStrategy from "./AbstractStrategy.js";

export default class PossiblePeakStrategy extends AbstractStrategy {
    #lastBuySuggestionTime = 0;

    constructor() {
        super();
        this.#lastBuySuggestionTime = Date.now();
    }

    updateAssetData(assetData) {
    }

    /**
     * @param {Transaction} transaction 
     * @returns {boolean}
     */
    shouldSell(transaction) {
        return this.getAssetData().getOrderBook().getBestBid().price / transaction.buySettings.price >= 1.015;
    }

    /**
     * @returns {boolean}
     */
    shouldBuy() {
        if (Date.now() < this.#lastBuySuggestionTime + 120000) {
            return false;
        }

        const peakCount = this.getAssetData().getPeakCountInTimeslot('1h');
        const price = this.getAssetData().getOrderBook().getBestBid().price;

        if (peakCount < 2) {
            return false;
        }

        const assetData5Min = this.getAssetData().getSlotForPeriode('5m');
        const assetData15Min = this.getAssetData().getSlotForPeriode('15m');
        const avg5Min = (assetData5Min.getLow() + assetData5Min.getHigh()) / 2;
        const avg15Min = (assetData15Min.getLow() + assetData15Min.getHigh()) / 2;
        const m5min = assetData5Min.getClose() / assetData5Min.getOpen();

        if ((price < avg5Min && avg5Min > avg15Min)
            || (price < avg15Min && m5min > 0.9995)
            || (price < assetData5Min.getOpen() && price > avg5Min)
        ) {
            this.#lastBuySuggestionTime = Date.now();
            return true;
        }
    }


    /**
     * @param {Transaction} transaction 
     * @returns {boolean}
     */
    shouldCancle(transaction) {
        return false;
    }

    /**
    * @returns {string} string representation of this object
    */
    toJson() {
        const obj = JSON.parse(super.toJson());

        return JSON.stringify(obj);
    }

    /**
     * @param {string} objString 
     * @returns {TestStrategy}
     */
    static fromJson(objString) {
        return new PossiblePeakStrategy();
    }
}