<template>
    <div class="ContourViewport" ref="ContourViewport" id="ContourViewport"></div>
</template>
<script>
import {
    getRenderingEngine,
    CONSTANTS,
    setVolumesForViewports,
    eventTarget,
    Enums,
    utilities,
} from '@cornerstonejs/core'
import * as cornerstoneTools from '@cornerstonejs/tools'
import setCtTransferFunctionForVolumeActor from '@/views/trials/trials-panel/reading/dicoms/components/Fusion/js/setCtTransferFunctionForVolumeActor'
import DicomEvent from '@/views/trials/trials-panel/reading/dicoms/components/DicomEvent'
const {
    Enums: csToolsEnums,
    segmentation,
    TrackballRotateTool,
    ToolGroupManager
} = cornerstoneTools
const { MouseBindings, Events: toolsEvents } = csToolsEnums
export default {
    name: "ContourViewport",
    props: {
        renderingEngineId: {
            type: String,
            required: true
        },
        viewportId: {
            type: String,
            required: true
        },
        visitInfo: {
            type: Object,
            default: () => {
                return {}
            }
        },
        // visible: {
        //     type: Boolean,
        //     default: false
        // },
    },
    data() {
        return {
            volumeId: null,
        }
    },
    mounted() {
        eventTarget.addEventListener(Enums.Events.WEB_WORKER_PROGRESS, (evt) => {
            const { progress } = evt.detail;
            console.log(progress, 'countour_progress')

        });
    },
    methods: {
        async setSeriesInfo(obj) {
            try {
                const renderingEngine = getRenderingEngine(this.renderingEngineId)
                const viewport = renderingEngine.getViewport(this.viewportId)
                let { volumeId, segmentationId } = obj
                this.volumeId = volumeId
                await setVolumesForViewports(
                    renderingEngine,
                    [{ volumeId, callback: setCtTransferFunctionForVolumeActor }],
                    [this.viewportId]
                );
                // const volumeActor = viewport.getDefaultActor()
                //     .actor;
                // utilities.applyPreset(
                //     volumeActor,
                //     CONSTANTS.VIEWPORT_PRESETS.find((preset) => preset.name === 'CT-Bone')
                // );
                // volumeActor.setVisibility(false);
                viewport.render();
                const toolGroup = ToolGroupManager.getToolGroup(this.viewportId)
                toolGroup.setToolActive(TrackballRotateTool.toolName, {
                    bindings: [
                        {
                            mouseButton: MouseBindings.Primary,
                        },
                    ],
                });
                let s = segmentation.getActiveSegmentation(this.viewportId)
                if (s) {
                    await segmentation.removeSegmentationRepresentation(this.viewportId, {
                        segmentationId: s.segmentationId,
                        type: csToolsEnums.SegmentationRepresentations.Surface,
                    })
                }
                console.log("ContourLoading...")
                await segmentation.addSegmentationRepresentations(this.viewportId, [
                    {
                        segmentationId,
                        type: csToolsEnums.SegmentationRepresentations.Contour,
                    },
                ]);
            } catch (e) {
                console.log(e)
            }

        },
    }
}
</script>
<style lang="scss" scoped>
#ContourViewport {
    width: 450px;
    height: 300px;
    position: fixed;
    top: 100px;
    z-index: -9999;
    left: 100px;
}
</style>