import Serializable from "../../util/Serializable.js";
import SerializableHelper from "../../util/SerializableHelper.js";
import Asset from "./Asset.js";

export default class TradingPair extends Serializable {
    /**
     * @type {Asset}
     */
    #asset1 = "";

    /**
     * @type {Asset}
     */
    #asset2 = "";

    /**
     * @param {Asset} currency1 
     * @param {Asset} currency2 
     */
    constructor(currency1, currency2) {
        super();
        this.#asset1 = currency1;
        this.#asset2 = currency2;
    }

    /**
     * 
     * @returns {string} trading pair key for binance api
     */
    getKey() {
        return this.#asset1.getSymbol() + this.#asset2.getSymbol();
    }

    /**
     * 
     * @returns {Asset} first currency
     */
    getFirstCurrency() {
        return this.#asset1;
    }

    /**
     * 
     * @returns {Asset} second currency
     */
    getSecondCurrency() {
        return this.#asset2;
    }

    /**
     * 
     * @returns {string} human readable representation of the traiding pair
     */
    toString() {
        return "[" + this.#asset1.getSymbol() + ", " + this.#asset2.getSymbol() + "]";
    }

    /**
     * @returns {string} string representation of this object
     */
    toJson() {
        const obj = JSON.parse(super.toJson());
        obj.asset1 = SerializableHelper.serialize(this.#asset1);
        obj.asset2 = SerializableHelper.serialize(this.#asset2);
        
        return JSON.stringify(obj);
    }

    /**
     * @param {string} objString 
     * @returns {TradingPair}
     */
    static fromJson(objString) {
        const obj = JSON.parse(objString);

        return new TradingPair(
            SerializableHelper.deserialize(obj.asset1), 
            SerializableHelper.deserialize(obj.asset2)
        );
    }
}