<?php
require_once '../../App/bootstrap.php';


$attachId = isset($_GET['attach_id']) ? $_GET['attach_id'] : 0;
if (!$user->isLogged()) {
	//exit(0);
}


?>
<!DOCTYPE html>
<html lang="it">
<head>
  
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" href="/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
    <title>Dicom</title>

	<style>
    html, body {width: 100%; height:100%; background-color: black; display:flex; justify-content:center;}
    #dicomImage {
      width: 100%;
      height: 600px;
      position: relative;
    }

    #toolBar{
      position: fixed;
      background-color: white;
      color:black;
      width:100%;
      height: 50px;
      z-index:10000;
      padding:10px;
   
    }
  </style>
</head>
<body>
    <div id="toolBar">
      <button type="button" class="btn btn-outline-dark btn-sm" id="activateZoom"><i class="fa fa-search-plus" aria-hidden="true"></i></button>
      <button type="button" class="btn btn-outline-dark btn-sm" id="activatePan">Small button</button>
      <button type="button" class="btn btn-outline-dark btn-sm" id="activateWwwc"><i class="fa fa-adjust" aria-hidden="true"></i></button>
      <button type="button" class="btn btn-outline-dark btn-sm" id="activateLength"><i class="fa fa-arrows-h" aria-hidden="true"></i></button>
    </div>
    <main>
        <section>
		<div id="dicomImage">
      
    </div>
        </section>
    </main>

	<script src="https://unpkg.com/cornerstone-core@2.3.0/dist/cornerstone.js"></script>
	<script src="https://unpkg.com/dicom-parser@1.8.4/dist/dicomParser.js"></script>
	<script src="https://unpkg.com/cornerstone-wado-image-loader@3.1.0/dist/cornerstoneWADOImageLoader.js"></script>
	<script src="https://unpkg.com/cornerstone-tools@4.15.0/dist/cornerstoneTools.js"></script>

	<script>
    document.addEventListener('DOMContentLoaded', function () {
      // Abilita il caricamento delle immagini WADO-URI
      cornerstoneWADOImageLoader.external.cornerstone = cornerstone;

      // Configura il caricatore WADO-URI
      cornerstoneWADOImageLoader.configure({
        beforeSend: function (xhr) {
          // Se necessarie, puoi aggiungere intestazioni personalizzate
        }
      });

      // Inizializza Cornerstone per l'elemento
      const element = document.getElementById('dicomImage');
      cornerstone.enable(element);

      // Carica l'immagine DICOM da un URL
      const dicomUrl = '/dicom/dicom.php?attach_id=<?php echo $attachId ?>';  // Aggiorna questo con il tuo percorso
      const imageId = 'wadouri:' + dicomUrl;

      // Carica e visualizza l'immagine
      cornerstone.loadImage(imageId).then(function(image) {
        cornerstone.displayImage(element, image);
      }).catch(function(err) {
        console.error('Errore durante il caricamento dell\'immagine DICOM:', err);
      });

      // Abilita strumenti di Cornerstone Tools
      cornerstoneTools.init();
      const ZoomTool = cornerstoneTools.ZoomMouseWheelTool;
      const PanTool = cornerstoneTools.PanTool;
      const WwwcTool = cornerstoneTools.WwwcTool; // Window/Level (Contrasto)
      const LengthTool = cornerstoneTools.LengthTool; // Misurazioni lunghezza

      // Aggiungi i tool all'elemento DOM
      cornerstoneTools.addTool(ZoomTool);
      cornerstoneTools.addTool(PanTool);
      cornerstoneTools.addTool(WwwcTool);
      cornerstoneTools.addTool(LengthTool);

      // Attiva i tool con pulsanti
      document.getElementById('activateZoom').addEventListener('click', function() {
        cornerstoneTools.setToolActive('ZoomMouseWheel', { mouseButtonMask: 1 });
      });

      document.getElementById('activatePan').addEventListener('click', function() {
        cornerstoneTools.setToolActive('Pan', { mouseButtonMask: 1 });
      });

      document.getElementById('activateWwwc').addEventListener('click', function() {
        cornerstoneTools.setToolActive('Wwwc', { mouseButtonMask: 1 });
      });

      document.getElementById('activateLength').addEventListener('click', function() {
        cornerstoneTools.setToolActive('Length', { mouseButtonMask: 1 });
      });
    });
  </script>

</body>
</html>
