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

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

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

    updateAssetData(assetData) {
    }

    /**
     * @param {Transaction} transaction 
     * @returns {boolean}
     */
    shouldSell(transaction) {
        const winPercent = (this.getAssetData().getOrderBook().getBestBid().price / transaction.buySettings.price) * 100 - 100;

        if(winPercent >= 3){
            return true;
        }

        if(winPercent < 0.6){
            return false;
        }

        const assetData15Min = this.getAssetData().getSlotForPeriode('15m');
        const assetData60Min = this.getAssetData().getSlotForPeriode('1h');
        const priceChangePercent24h = this.getAssetData().get24hPriceChangePercent();
        const grad15min = assetData15Min.getClose() / assetData15Min.getOpen();
        const grad60min = assetData60Min.getClose() / assetData60Min.getOpen();

        return priceChangePercent24h < 0.05 || (grad15min < 0.995 && grad60min < 0.985);
    }

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

        const assetData15Min = this.getAssetData().getSlotForPeriode('15m');
        const assetData60Min = this.getAssetData().getSlotForPeriode('1h');
        const priceChangePercent24h = this.getAssetData().get24hPriceChangePercent();
        const grad15min = (assetData15Min.getClose() - assetData15Min.getOpen()) / 15;
        const grad60min = (assetData60Min.getClose() - assetData60Min.getOpen()) / 60;
        
        // up trend
        if(priceChangePercent24h > 2 && grad15min < 0 && grad60min > 0){
            this.#lastBuySuggestionTime = Date.now();
            return true;
        }

        // down trend
        if(priceChangePercent24h < -2 && (grad60min < grad15min || grad15min > 0.15 && grad60min > -0.02)){
            this.#lastBuySuggestionTime = Date.now();
            return true;
        }

        return false;
    }

    /**
     * @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 Simple24hStrategy();
    }
}