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

export default class AvgMinMaxStrategy 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 < 0.6){
            return false;
        }

        const assetData15Min = this.getAssetData().getSlotForPeriode('15m');
        const price = this.getAssetData().getOrderBook().getBestBid().price;
        const avg15m = (assetData15Min.getLow() + assetData15Min.getHigh()) / 2;
        

        return avg15m * 1.005 <= price;
    }

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

        const assetData15Min = this.getAssetData().getSlotForPeriode('15m');
        const price = this.getAssetData().getOrderBook().getBestBid().price;
        const avg15m = (assetData15Min.getLow() + assetData15Min.getHigh()) / 2;
        
        if(avg15m * 0.995 >= price){
            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();
    }
}