import merge from "lodash.merge";
import assert from "assert";
import Mapbox_Map from "../ui/mapbox_map.js";
import Building from "./building.js";
import Camera from "./camera.js";
import * as defaultValue from "./values";
import { genStopsMap } from "./utils";
import { createRenderTraget } from "./effect/utils";
import Effect from "./effect";
import ThreeBoxLayer from "./threejs/threeBoxLayer.js";
import ThreeJsLayer from "./threejs/threeJsLayer.js";
import SkyBox from "./skyBox";

class MyMap extends Mapbox_Map {
    constructor(options) {
        const renderTarget = createRenderTraget(options.container);
        options.renderTarget = renderTarget;
        super(options);

        this.effect = new Effect(this, renderTarget, options);

        this.on("style.load", () => {
            this.threeJsLayer = new ThreeJsLayer(this, renderTarget);
            this.addLayer(this.threeJsLayer);

            this.threeBoxLayer = new ThreeBoxLayer(this, renderTarget);
            this.threeBoxLayer.setEventedParent(this);
            this.addLayer(this.threeBoxLayer);
        });

        this.building = new Building(this);
        this.camera = new Camera(this);
        this.skyBox = new SkyBox(this);
        this.destroyed = false;
    }

    // 添加建筑层
    async addbuildings(options) {
        const buildingOptions = merge({}, defaultValue.building, options);
        assert(
            buildingOptions.textures.length === 0 ||
                buildingOptions.textures.length >= 6,
            "请上传6张建筑贴图！"
        );
        this.building.addbuildings(buildingOptions);
    }

    // 高亮建筑
    lightUpBuilding(options) {
        assert(this.threeJsLayer, "请在map.ready后调用此方法!");
        const lightupOptions = merge({}, defaultValue.lightup, options);
        this.threeJsLayer.lightupCoord(lightupOptions);
    }

    // 聚焦
    focus(options, cb) {
        const focusOptions = merge({}, defaultValue.camera, options);
        this.camera.focus(focusOptions, cb);
        this.effect.focus(
            merge({}, defaultValue.lightFocus, options.lightOptions)
        );

        this.once("click", this.unfocus.bind(this));
        this.once("wheel", this.unfocus.bind(this));
    }

    // 取消聚焦
    unfocus() {
        this.camera.unfocus();
        this.effect.unfocus();
    }

    // 打点
    addMarker(options) {
        assert(this.threeBoxLayer, "请在map.ready后调用此方法!");
        const markerOptions = merge({}, defaultValue.marker, options);
        const stops = genStopsMap(markerOptions.header.scale);
        this.threeBoxLayer.addMarker(markerOptions, stops);
    }

    // 添加3D模型
    add3DModel(options) {
        this.threeBoxLayer.add3DModel(options);
    }

    // 添加天空盒
    addSkyBox(options) {
        const skyBoxOptions = merge({}, defaultValue.skyBox, options);
        this.skyBox.init(skyBoxOptions);
    }

    // 添加飞线
    addFlyLine(options) {
        assert(this.threeJsLayer, "请在map.ready后调用此方法!");
        const flyLineOptions = merge({}, defaultValue.flyLine, options);
        this.threeJsLayer.addFlyLine(flyLineOptions);
    }

    // 设置漂浮物
    setFlotsam(options) {
        assert(this.threeJsLayer, "请在map.ready后调用此方法!");
        const flotsamOptions = merge({}, defaultValue.flotsam, options);
        this.threeJsLayer.setFlotsam(flotsamOptions);
    }

    destroy() {
        this.destroyed = true;
        this.remove();
        if (this.threeJsLayer) this.threeJsLayer.destroy();
        this.effect.destroy();
    }
}

export default MyMap;
