import { __awaiter, __extends, __generator } from "tslib";
import localforge from 'localforage';
import ViewPoint from '../model/scene/ViewPoint';
import imgCompress from '../libs/imgCompress';
import Control from './_Control';
var VPointControl = /** @class */ (function (_super) {
    __extends(VPointControl, _super);
    function VPointControl(instance, viewer) {
        var _this = _super.call(this, instance) || this;
        _this._namespace = 'default';
        _this._lo = localforge.createInstance({
            name: "vps_" + _this._namespace,
        });
        _this._vpMap = new Map();
        _this._viewer = viewer;
        _this.setName();
        return _this;
    }
    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.getImage = function (imgWidth) {
        if (imgWidth === void 0) { imgWidth = 100; }
        return __awaiter(this, void 0, void 0, function () {
            var imgFile;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        imgFile = this._viewer['_cesiumWidget']._canvas.toDataURL('image/jpeg', 0.5);
                        return [4 /*yield*/, imgCompress(imgFile, imgWidth)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    VPointControl.prototype.getScreenCut = function (width) {
        return __awaiter(this, void 0, void 0, function () {
            var imgFile;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        imgFile = this._viewer['_cesiumWidget']._canvas.toDataURL('image/jpeg', 0.5);
                        return [4 /*yield*/, imgCompress(imgFile, width || 100)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    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, cSc, _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;
                        cSc = this._instance.getInstanceSceneControl();
                        vp.styleConf = cSc.styleConf;
                        if (!useThumbImg) return [3 /*break*/, 2];
                        _a = vp;
                        return [4 /*yield*/, this.getScreenCut(imgWidth)];
                    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 (option) {
        return __awaiter(this, void 0, void 0, function () {
            var position, direction, up, styleConf, instance, cCa, cSc;
            return __generator(this, function (_a) {
                position = option.position, direction = option.direction, up = option.up, styleConf = option.styleConf;
                instance = this._instance;
                cCa = instance.getInstanceCameraControl();
                cSc = instance.getInstanceSceneControl();
                cCa.zoomto(position, direction, up, 1);
                cSc.styleConf = styleConf;
                return [2 /*return*/];
            });
        });
    };
    return VPointControl;
}(Control));
export default VPointControl;
