/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { __extends, __values } from "tslib";
import { Base, fromEnum, markType } from "./type";
import { Random } from "./Random";
import { StockPosition } from "./StockPosition";
import { StockPricePoint } from "./StockPricePoint";
import { StockDataLookup } from "./StockDataLookup";
import { StockDetails } from "./StockDetails";
import { List$1 } from "./List$1";
import { StockPriceHistory } from "./StockPriceHistory";
import { intDivide, truncate } from "./number";
import { dateNow, dateFromValues, dateGetMonth, dateAddMonths, dateAddMinutes, dateGetTimeOfDay } from "./date";
import { timeSpanTotalMinutes, timeSpanInit1 } from "./timespan";
import { stringIsNullOrEmpty } from "./string";
import { round10N } from "./numberExtended";
/**
 * @hidden
 */
var StockDataGenerator = /** @class */ (function (_super) {
    __extends(StockDataGenerator, _super);
    function StockDataGenerator() {
        var _this = _super.call(this) || this;
        _this._priceStart = 0;
        _this._priceRange = 0;
        _this._priceMinimum = 0;
        _this._priceMaximum = 0;
        _this._volumeStart = 0;
        _this._volumeRange = 0;
        _this._dateRange = 0;
        _this._dateInterval = 0;
        _this._stockInterval = 0;
        _this._skipAfterHours = false;
        _this.g = new Date();
        _this.h = new Date();
        _this.q = new Random(0);
        _this.skipAfterHours = false;
        _this.priceStart = 0;
        _this.priceRange = NaN;
        _this.priceMinimum = NaN;
        _this.priceMaximum = NaN;
        _this.volumeStart = 0;
        _this.volumeRange = NaN;
        _this.dateRange = 12;
        _this.dateInterval = 24 * 60;
        return _this;
    }
    Object.defineProperty(StockDataGenerator.prototype, "priceStart", {
        get: function () {
            return this._priceStart;
        },
        set: function (a) {
            this._priceStart = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(StockDataGenerator.prototype, "priceRange", {
        get: function () {
            return this._priceRange;
        },
        set: function (a) {
            this._priceRange = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(StockDataGenerator.prototype, "priceMinimum", {
        get: function () {
            return this._priceMinimum;
        },
        set: function (a) {
            this._priceMinimum = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(StockDataGenerator.prototype, "priceMaximum", {
        get: function () {
            return this._priceMaximum;
        },
        set: function (a) {
            this._priceMaximum = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(StockDataGenerator.prototype, "volumeStart", {
        get: function () {
            return this._volumeStart;
        },
        set: function (a) {
            this._volumeStart = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(StockDataGenerator.prototype, "volumeRange", {
        get: function () {
            return this._volumeRange;
        },
        set: function (a) {
            this._volumeRange = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(StockDataGenerator.prototype, "dateRange", {
        get: function () {
            return this._dateRange;
        },
        set: function (a) {
            this._dateRange = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(StockDataGenerator.prototype, "dateInterval", {
        get: function () {
            return this._dateInterval;
        },
        set: function (a) {
            this._dateInterval = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(StockDataGenerator.prototype, "stockInterval", {
        get: function () {
            return this._stockInterval;
        },
        set: function (a) {
            this._stockInterval = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(StockDataGenerator.prototype, "skipAfterHours", {
        get: function () {
            return this._skipAfterHours;
        },
        set: function (a) {
            this._skipAfterHours = a;
        },
        enumerable: true,
        configurable: true
    });
    StockDataGenerator.prototype.generatePosition = function (a, b) {
        if (b === void 0) { b = ""; }
        var c = StockDataLookup.get(a);
        var d = (intDivide(this.q.next2(1, 10), 100));
        var e = new StockPosition();
        e.symbol = c.symbol;
        e.company = c.company;
        e.sector = c.sector;
        e.costPerShare = c.close - (c.open * d);
        e.marketPrice = c.close;
        e.tradeDate = dateNow();
        return e;
    };
    StockDataGenerator.prototype.generateDetails = function () {
        var e_1, _a;
        var a = new List$1(StockDetails.$, 0);
        try {
            for (var _b = __values(fromEnum(StockDataLookup.b.values)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var b = _c.value;
                var c = (intDivide(this.q.next2(10, 30), 100));
                var d = new StockDetails();
                d.company = b.company;
                d.symbol = b.symbol;
                d.sector = b.sector;
                d.marketPrice = b.close;
                d.marketShares = intDivide((b.volume * this.q.next2(15, 20)), 1000000);
                d.annualLow = b.close - (b.open * c);
                d.annualHigh = b.close + (b.open * c);
                a.add(d);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return a.toArray();
    };
    StockDataGenerator.prototype.generateStockPrices = function (a, b) {
        if (b === void 0) { b = ""; }
        var c = dateNow();
        this.g = dateFromValues(c.getFullYear(), dateGetMonth(c), c.getDate(), 16, 30, 0, 0);
        this.h = dateAddMonths(this.g, -this.dateRange);
        var d = Math.ceil(timeSpanTotalMinutes((+(this.g) - +(this.h))) / this.dateInterval);
        var e = d * this.dateInterval;
        this.s(d + " steps, " + e + " range");
        if (d > 100000) {
            d = 100000;
            e = d * this.dateInterval;
            this.s(d + "^steps, " + e + " range");
        }
        var f = dateAddMinutes(this.g, -e);
        if (this.h.getTime() > f.getTime()) {
            this.h = f;
        }
        var g = StockDataLookup.get(a);
        g.date = this.h;
        var h = g.sector;
        if (this.priceStart > 0) {
            g.open = this.priceStart;
        }
        if (this.volumeStart > 0) {
            g.volume = this.volumeStart;
        }
        this.volumeRange = g.volume * 0.05;
        this.priceRange = g.open * 0.025;
        this.priceMinimum = g.open - (g.open * 0.75);
        this.priceMaximum = g.open + (g.open * 0.75);
        g.high = g.open + (g.open * 0.02);
        g.low = g.open - (g.open * 0.02);
        g.close = g.open + ((this.q.nextDouble() - 0.5) * this.priceRange);
        var i = new StockPriceHistory();
        i.symbol = a;
        if (!stringIsNullOrEmpty(b)) {
            g.company = b;
            i.company = b;
        }
        else if (!stringIsNullOrEmpty(g.company)) {
            i.company = g.company;
        }
        do {
            if (this.skipAfterHours) {
                if (!this.e(g.date)) {
                    i.add(g);
                }
            }
            else {
                i.add(g);
            }
            g = this.generateStockPricePoint(g);
        } while (g.date.getTime() <= this.g.getTime());
        this.s(" " + i.count + " items");
        return i;
    };
    StockDataGenerator.prototype.e = function (a) {
        if (a.getDay() == 6) {
            return true;
        }
        if (a.getDay() == 0) {
            return true;
        }
        var b = timeSpanInit1(9, 30, 0);
        var c = timeSpanInit1(16, 30, 0);
        var d = dateGetTimeOfDay(a);
        if (d < b || d > c) {
            return true;
        }
        return false;
    };
    StockDataGenerator.prototype.generateStockPricePoint = function (a) {
        var _this = this;
        var b = (this.q.nextDouble() - 0.499) * this.priceRange;
        var c = a.close;
        var d = a.close + b;
        if (d < this.priceMinimum) {
            d = this.priceMinimum + b;
        }
        if (d > this.priceMaximum) {
            d = this.priceMaximum - b;
        }
        var e = (this.q.nextDouble() * 1.5 * Math.abs(b));
        var f = (this.q.nextDouble() * 1.5 * Math.abs(b));
        if (d > c) {
            e = d + e;
            f = c - f;
        }
        else {
            e = c + e;
            f = d - f;
        }
        var g = (this.q.nextDouble() - 0.49) * this.volumeRange;
        var h = truncate(Math.abs(a.volume + g));
        var i = dateAddMinutes(a.date, this.dateInterval);
        if (i.getTime() > this.g.getTime()) {
            i = this.g;
        }
        var j = ((function () {
            var $ret = new StockPricePoint();
            $ret.date = dateAddMinutes(a.date, _this.dateInterval);
            $ret.symbol = a.symbol;
            $ret.company = a.company;
            $ret.sector = a.sector;
            $ret.close = round10N(d, 2);
            $ret.open = round10N(c, 2);
            $ret.high = round10N(e, 2);
            $ret.low = round10N(f, 2);
            $ret.volume = h;
            return $ret;
        })());
        return j;
    };
    StockDataGenerator.prototype.s = function (message_) {
    };
    StockDataGenerator.$t = markType(StockDataGenerator, 'StockDataGenerator');
    return StockDataGenerator;
}(Base));
export { StockDataGenerator };
