import TradingPair from "../../apiwrapper/assets/TradingPair.js";
import Transaction from "../../apiwrapper/transaction/Transaction.js";
import TransactionPhase from "../../apiwrapper/transaction/TransactionPhase.js";
import AssetData from "../data/asset/AssetData.js";
import AbstractStrategy from "../strategy/AbstractStrategy.js";
import AbstractWorker from "./AbstractWorker.js";

export default class DefaultWorker extends AbstractWorker{
    /**
     * @param {string} name 
     * @param {TradingPair} tradingPair 
     * @param {AbstractStrategy} strategy 
     */
    constructor(name, tradingPair, strategy){
        super(name, tradingPair, strategy);
    }

    /**
     * @param {string} dataPath path to data directory
     */
    onInit(dataPath){
        this.getLogger().debug("Worker " + this.getName() + " initialized!");
    }

    /**
     * @param {AssetData} assetData 
     * @param {Transaction[]} transactions 
     */
    onUpdate(assetData, transactions){
        this.getStrategy().updateAssetData(assetData);

        if(this.getStrategy().shouldBuy()){
            // TODO create transaction!
        }

        for(let transaction of transactions){
            if(transaction.phase == TransactionPhase.BUY_DONE && this.getStrategy().shouldSell(transaction)){
                // TODO request sell!
            } else if(this.getStrategy().shouldCancle()){
                // TODO request cancle
            }
        }
    }

    /**
    * @returns {string} string representation of this object
    */
    toJson() {
        return super.toJson();
    }

    /**
     * @param {string} objString 
     * @returns {AbstractWorker}
     */
    static fromJson(objString) {
        return super.fromJson(objString, new DefaultWorker());
    }
}