import { __awaiter, __generator } from "tslib";
import localforge from 'localforage';
import ViewPoint from '../model/scene/ViewPoint';
import Instance from '../core/Instance';
import { TIMESPAN_SOON } from '../core/Constant';
import imgCompress from '../libs/imgCompress';
var VPointControl = /** @class */ (function () {
    function VPointControl(viewer) {
        this._namespace = 'default';
        this._lo = localforge.createInstance({
            name: "vps_" + this._namespace
        });
        this._vpMap = new Map();
        this._viewer = viewer;
        this.setName();
    }
    VPointControl.prototype._load = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, vpm, lo;
            return __generator(this, function (_b) {
                _a = this, vpm = _a._vpMap, lo = _a._lo;
                vpm.clear();
                lo.iterate(function (val) {
                    vpm.set(val.name, val);
                });
                return [2 /*return*/];
            });
        });
    };
    VPointControl.prototype.setName = function (name) {
        if (name === void 0) { name = 'default'; }
        this._lo = localforge.createInstance({
            name: "vps_" + name
        });
        this._load();
    };
    VPointControl.prototype.create = function (name, option, autosave, camera) {
        if (name === void 0) { name = 'default'; }
        if (option === void 0) { option = {}; }
        if (autosave === void 0) { autosave = true; }
        if (camera === void 0) { camera = this._viewer.camera; }
        return __awaiter(this, void 0, void 0, function () {
            var imgWidth, useThumbImg, clipData, vp, imgFile, _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        option = option || {};
                        imgWidth = option.imgWidth, useThumbImg = option.useThumbImg, clipData = option.clipData;
                        vp = new ViewPoint();
                        vp.position = camera.position.clone();
                        vp.direction = camera.direction.clone();
                        vp.up = camera.up.clone();
                        vp.name = name;
                        if (!useThumbImg) return [3 /*break*/, 2];
                        imgFile = this._viewer['_cesiumWidget']._canvas.toDataURL('image/jpeg', 0.5);
                        _a = vp;
                        return [4 /*yield*/, imgCompress(imgFile, imgWidth || 100)];
                    case 1:
                        _a.img = _b.sent();
                        _b.label = 2;
                    case 2:
                        if (clipData && clipData.status) {
                            vp.clipData = clipData;
                        }
                        autosave && this.update(vp);
                        return [2 /*return*/, vp];
                }
            });
        });
    };
    VPointControl.prototype.update = function (val) {
        return __awaiter(this, void 0, void 0, function () {
            var _a, vpm, lo;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this, vpm = _a._vpMap, lo = _a._lo;
                        vpm.set(val.name, val);
                        return [4 /*yield*/, lo.setItem(val.name, val)];
                    case 1: return [2 /*return*/, _b.sent()];
                }
            });
        });
    };
    VPointControl.prototype.find = function (name) {
        var vpm = this._vpMap;
        return vpm.get(name);
    };
    VPointControl.prototype.list = function () {
        var vpm = this._vpMap;
        var it = vpm.values();
        var val;
        var vps = [];
        while ((val = it.next().value)) {
            vps.push(val);
        }
        return vps;
    };
    VPointControl.prototype.delete = function (name) {
        return __awaiter(this, void 0, void 0, function () {
            var _a, vpm, lo;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this, vpm = _a._vpMap, lo = _a._lo;
                        vpm.delete(name);
                        return [4 /*yield*/, lo.removeItem(name)];
                    case 1:
                        _b.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    VPointControl.prototype.flyto = function (name, timespan) {
        if (timespan === void 0) { timespan = TIMESPAN_SOON; }
        return __awaiter(this, void 0, void 0, function () {
            var vp, camera, cAn, clipData, cSc, clipControl;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.find(name)];
                    case 1:
                        vp = _a.sent();
                        camera = this._viewer.camera;
                        if (!(vp && camera))
                            return [2 /*return*/];
                        return [4 /*yield*/, Instance.getInstanceAnimaControl()];
                    case 2:
                        cAn = _a.sent();
                        clipData = vp.clipData;
                        if (!(clipData && clipData['status'])) return [3 /*break*/, 6];
                        return [4 /*yield*/, Instance.getInstanceSceneControl()];
                    case 3:
                        cSc = _a.sent();
                        return [4 /*yield*/, Instance.getInstanceClipControl()];
                    case 4:
                        clipControl = _a.sent();
                        return [4 /*yield*/, clipControl.initClip(clipData['position'], cSc._layer3DTile, clipData['direction'], clipData['value'])];
                    case 5:
                        _a.sent();
                        _a.label = 6;
                    case 6:
                        cAn.trans(camera.position, vp.position, timespan);
                        cAn.trans(camera.direction, vp.direction, timespan);
                        cAn.trans(camera.up, vp.up, timespan);
                        return [2 /*return*/];
                }
            });
        });
    };
    return VPointControl;
}());
export default VPointControl;
