import TradingPairs from "../apiwrapper/assets/TraidingPairs.js";
import Transaction from "../apiwrapper/transaction/Transaction.js";
import WebViewDataProvider from "../webview/WebViewDataProvider.js";
import TradingBot from "./TradingBot.js";

export default class TradingBotRestDataProvider extends WebViewDataProvider {
    /**
     * @type {TradingBot}
     */
    #tradingBot = null;

    /**
     * @param {TradingBot} tradingBot 
     */
    constructor(tradingBot) {
        super();
        this.#tradingBot = tradingBot;
    }

    /**
     * @returns {string[]}
     */
    getAvailableAssets() {
        return this.#tradingBot.getAssetDataCollector().getManagedAssets().map(tp => tp.getKey());
    }

    /**
     * @param {string} symbol 
     * @returns {OrderBook}
     */
    getOrderBook(symbol) {
        return this.#tradingBot.getAssetDataCollector().getAssetData(symbol).getOrderBook();
    }

    /**
     * @param {string} symbol 
     * @returns {Transaction[]}
     */
    getActiveTransactions(symbol) {
        return this.#tradingBot.getTransactionsByAsset(TradingPairs.fromString(symbol)).sort((t1, t2) => t1.timestamp - t2.timestamp);
    }

    /**
     * @param {string} symbol 
     * @returns {Transaction[]}
     */
    getAllTransactions(symbol) {
        const tp = TradingPairs.fromString(symbol);
        return this.#tradingBot.getTransactionsByAsset(tp).concat(this.#tradingBot.getClosedTransactionsByAsset(tp)).sort((t1, t2) => t1.timestamp - t2.timestamp);
    }

    /**
     * @returns {string[]}
     */
    getExistingTradingPairs() {
        return TradingPairs.TRADING_PAIRS.map(tp => tp.getKey());
    }

    /**
     * @type {Map<string, number>}
     */
    getBalances() {
        return this.#tradingBot.getAccountInfo().getBalances();
    }

    /**
     * @returns {AbstractWorker[]}
     */
    getWorkers() {
        return Array.from(this.#tradingBot.getWorker().values());
    }
}