<?php

namespace App\Http\Controllers\Terra;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Response;
use Carbon\Carbon;

class SRController extends Controller
{

    public function index()
    {
        // Path ของ dicom SR file
        $dicom_file_path = 'D:\laragon\htdocs\playground\laravel\playground-app\asset\sr_dicom';
        // ตั้งค่า environment variable ของ Path ไปยังโฟลเดอร์ bin ของ dcmtk ที่โหลดมา
        $dcmtk_path = 'C:\Users\bright\Downloads\dcmtk-3.6.7-win64-dynamic\bin';
        // Path ของ text file
        $text_file_path = 'D:\laragon\htdocs\playground\laravel\playground-app\asset\sr_text';

        // get files in folder
        $files = file_exists($dicom_file_path) ? scandir($dicom_file_path) : 0;
        for($i=0;$i<count($files);$i++){
            if(file_exists($dicom_file_path."\\$files[$i]") && is_file($dicom_file_path."\\$files[$i]")){
                $command = "dsrdump +f +U8 +Ps $dicom_file_path\\$files[$i]";
                $output = shell_exec($command);
                $explode = explode("\n", $output);
                $dict = $this->change_to_array(array_values(array_filter($explode)));

                $json_unit_dict = $this->array_to_json($dict['unit']);
                $json_unitless_dict = $this->array_to_json($dict['unitless']);

                file_put_contents("$text_file_path\data$i.txt", $json_unitless_dict);
            }
        }
    }

    function change_to_array($explode){
        $this_dict = [];
        $this_unitless_dict = [];
        foreach ($explode as $line) {
            if(str_contains('has properties', $line) == false){
                $text_str = '';
                if(str_contains($line, 'SR Document')){
                    $this_dict['type'] = $line;
                }
    
                if(str_contains($line, ' :')){
                    $text_arr = explode(':', $line);
                    $this_dict[rtrim($text_arr[0])] = $this->remove_special_char(ltrim($text_arr[1]));
                    $this_unitless_dict[rtrim($text_arr[0])] = $this->remove_special_char(ltrim($text_arr[1]));
                }
    
                if(str_contains($line, '"')){
                    $text_arr = explode('"', $line);
                    $number = count($text_arr);
                    if(str_contains($line, 'SEPERATE')){
                        $this_dict[$text_arr[1]] = '';
                        $this_unitless_dict[$text_arr[1]] = '';
                    } else {
                        if($number > 3){
                            $z = range(3, $number-1);
                            $last_number = $z[array_key_last($z)];
                            foreach ($z as $n) {
                                $new_line = ltrim(str_replace('\n', '', $text_arr[$n]));
                                $text_str = $text_str.$new_line;
                                if(str_contains($text_arr[$n-1], '(') && $n == $last_number){
                                    $text_str = $text_str.')';
                                }
                            }
                            $this_dict[rtrim($text_arr[1])] = $this->remove_special_char($text_str);
                            $this_unitless_dict[rtrim($text_arr[1])] = $this->remove_special_char(explode('(', $text_str)[0]);
                        }   
                    }
                }
            }
        }
        $dict['unit'] = $this_dict;
        $dict['unitless'] = $this_unitless_dict;
        return $dict;
    }

    function remove_special_char($string){
        if(str_contains($string, '>')){
            $string = str_replace('>', '', $string);
        }
        return $string;
    }

    function array_to_json($array){
        $json = json_encode($array, JSON_UNESCAPED_UNICODE);
        return $json;
    }

    public function create()
    {
        //
    }

    public function store(Request $r)
    {
        // if($r->event=="check_hn")       {return $this->check_hn($r);}



    }

    public function show($id)
    {
        

    }

    public function edit($id)
    {
        //
    }

    public function update(Request $request, $id)
    {
        //
    }

    public function destroy($id)
    {
        //
    }
}
