//import APIConfig from "../js/apiwrapper/APIConfig.js";
import SerializableHelper from "../js/util/SerializableHelper.js";
import BinanceApiClient from "./apis/binance/BinanceApiClient.js";
import BinanceApiConfig from "./apis/binance/BinanceApiConfig.js";
import TradingPairs from "./apiwrapper/assets/TraidingPairs.js";
import Transaction from "./apiwrapper/transaction/Transaction.js";
import TransactionSettings from "./apiwrapper/transaction/TransactionSettings.js";
import Logger from "./logging/Logger.js";
import AssetDataCollector from "./tradingbot/AssetDataCollector.js";
import BotConfig from "./tradingbot/config/BotConfig.js";
import RestClient from "./webview/RestClient.js";
//import BinanceApiConfig from "./apis/binance/BinanceApiConfig.js";
//import TradingPair from "./apiwrapper/assets/TradingPair.js";
//import TradingPairs from "./apiwrapper/assets/TraidingPairs.js";
//import BotConfig from "./tradingbot/config/BotConfig.js";
import TradingBot from "./tradingbot/TradingBot.js";
import DefaultWorker from "./tradingbot/worker/DefaultWorker.js";
import AbstractStrategy from "./tradingbot/strategy/AbstractStrategy.js";
import StatisticWorker from "./tradingbot/worker/StatisticWorker.js";
import PeakDetector from "./tradingbot/data/asset/PeakDetector.js";
import OrderBookEntry from "./apiwrapper/orderbook/OrderBookEntry.js";
import TestStrategy from "./tradingbot/strategy/TestStrategy.js";
try{

    const tradingBot = new TradingBot("C:/Users/Wlad/Desktop/binance_bot/conf", "config-test.sjson");
    const galaWorker = new DefaultWorker("default_gala", TradingPairs.GALAUSDT, new AbstractStrategy());
    const statWorker = new StatisticWorker("gala_stats", TradingPairs.GALAUSDT, new TestStrategy());
    //tradingBot.registerWorker(galaWorker);
    //tradingBot.registerWorker(statWorker);
    tradingBot.start();
} catch (e){
    console.log(e);
}





// ############################
// ##        API TESTS       ##
// ############################
/*try{
    const conf = new BotConfig();
    const logger = new Logger("C:/Users/Wlad/Desktop/binance_bot/log");
    const collector = new AssetDataCollector(logger, conf);
    const restClient = new RestClient();
    
    conf.setApiConfig(BinanceApiConfig.createTestNetConfig());
    const listener = {
        handleAssetUpdate: function(accountUpdateEvent){
            collector.pushAssetEvent(accountUpdateEvent);
        },

        handleAccountUpdate: function(accountUpdateEvent){
            console.log(accountUpdateEvent);
        }
    };
    const client = new BinanceApiClient(conf.getApiConfig(), logger);
    const testTransaction = new Transaction("test", TradingPairs.GALAUSDT);
    testTransaction.buySettings = new TransactionSettings(0.03644, 150);
    //client.requestBuy(testTransaction);
    //client.requestBuyCancel(testTransaction);
    setInterval(() => {
        client.requestTransactionStatus(testTransaction, "buy");
        client.requestTransactionDetails(testTransaction, "buy");
    }, 10000)
    
    client.registerAccountEventListener(listener);
    client.registerAssetEventListener(listener);
    client.subscribeTrades(TradingPairs.GALAUSDT);
    client.subscribe24hTicker(TradingPairs.GALAUSDT);
    client.subscribeKLines(TradingPairs.GALAUSDT);
    client.subscribeOrderBookUpdates(TradingPairs.GALAUSDT);

    restClient.start({
        getAvailableAssets(){            
            return collector.getManagedAssets().map(tp => tp.getKey());
        },
        getOrderBook(symbol){
            return collector.getAssetData(TradingPairs.fromString(symbol)).getOrderBook();
        }
    });

    setInterval(() => {
        const assetData = collector.getAssetData(TradingPairs.GALAUSDT);
        console.log(assetData.getHigh(), assetData.getLow(), assetData.getOrderBook().getBestAsk(), assetData.getOrderBook().getBestBid());
        console.log(assetData.getSubData()[assetData.getSubData().length - 1]);
    }, 10000);
} catch (e){
    console.log(e);
}*/

// ############################
// ##      write config      ##
// ############################
/*const path = 'C:/Users/Wlad/Desktop/binance_bot/conf';
const conf = new BotConfig();
conf.setApiConfig(BinanceApiConfig.createMainNetConfig());
conf.setWorkingDirectory('C:/Users/Wlad/Desktop/binance_bot');
conf.setRelativeDataPath('data');
conf.setRelativeLogPath('logs');
conf.setRelativeTmpPath('tmp');
SerializableHelper.save(path, "config-test.sjson", conf);*/

// ############################
// ##    SERIALIZER TESTS    ##
// ############################

/*let confMain = BinanceApiConfig.createMainNetConfig();
let confTest = BinanceApiConfig.createTestNetConfig();
let confAlt = new BinanceApiConfig("testKey", "testSecret");

console.log("before main:", confMain.getKey(), confMain.getRestUrl());
console.log("before test:", confTest.getKey(), confTest.getRestUrl());
console.log("before alt:", confAlt.getKey(), confAlt.getRestUrl());

SerializableHelper.save(".", "api.conf", [confMain, confTest, confAlt]);
[confMain, confTest, confAlt] = SerializableHelper.load(".", "api.conf", BinanceApiConfig);

console.log("after main:", confMain.getKey(), confMain.getRestUrl());
console.log("after test:", confTest.getKey(), confTest.getRestUrl());
console.log("after alt:", confAlt.getKey(), confAlt.getRestUrl());

let tp = TradingPairs.GALAUSDT;
SerializableHelper.save(".", "pair.json", tp);
tp = SerializableHelper.load(".", "pair.json", TradingPair);
console.log(tp.getKey());*/