export default class Camera {
    constructor(map) {
        this.map = map;
        this.hasFocused = false; // 当前是否为聚焦状态
        this.isRotating = false; // 当前相机是否在旋转
    }

    // 聚焦某处
    focus(options, cb) {
        if (this.hasFocused) return;

        this.flyTo({ ...options, meta: "FOCUS" });

        if (options.rotation) {
            this.hasFocused = true;
            this.map.once("moveend", (e) => {
                if (e.moveType === "FOCUS") {
                    this.rotateCamera(true);
                    cb && cb()
                }
            });
        }
    }

    // 视角飞向某处
    flyTo(options) {
        this.map.flyTo(
            {
                center: options.center,
                zoom: options.zoom,
                pitch: options.pitch !== undefined ? options.pitch : 55,
            },
            { moveType: options.meta || "NORMAL" }
        );
    }

    // 旋转相机
    rotateCamera(isStart) {
        if (isStart) this.isRotating = true;
        if (!this.isRotating) return;
        const bearing = this.map.getBearing();
        this.map.rotateTo(bearing + 0.1, { duration: 0 });
        requestAnimationFrame(() => this.rotateCamera.call(this, false));
    }

    // 取消旋转相机
    unfocus() {
        if (this.isRotating) {
            this.hasFocused = false;
            this.isRotating = false;
        }
    }
}
