<?php

use Config\Services;

function imageToBase64($path) {
    $path = $path;
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($path);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
    return $base64;
}

function filterData(&$str){ 
    $str = preg_replace("/\t/", "\\t", $str); 
    $str = preg_replace("/\r?\n/", "\\n", $str); 
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; 
}

function dataExcel($result, $fields) {

    $data = array();
    $i = 0;
    foreach($result as $obj):
        $row = array();
        // if(isset($fields['image'])) 
        if(isset($fields['name'])) 
        if(isset($fields['country'])) $row['COUNTRY'] = $obj['country'];
        if(isset($fields['seat'])) $row['SEAT'] = $obj['seating'];
        if(isset($fields['table']))  $row['TABLE/ROW'] = $obj['stage'];

        $data[] = $row;

    endforeach;
    return $data;

}

function gridEmail($data) {
    $email = new \SendGrid\Mail\Mail();
    $email->setFrom($data['fromEmail'], $data['fromName']);
    $email->setReplyTo($data['replyTo']);
    $email->setSubject($data['subject']);
    $email->addTo($data['toEmail'], $data['toName']);
    $email->addContent(
        "text/html", $data['body']
    );

    try {
        $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
        $response = $sendgrid->send($email);
    } catch (Exception $e) {
        return $e->getMessage();
    }
    return null;
}

function splitSeating($seating) {
    $posT = strpos($seating, 'T');
    $posS = strpos($seating, 'S');

    // Extract the substring between 'T' and 'S'
    $betweenTS = substr($seating, $posT + 1, $posS - $posT - 1);

    // Extract the substring after 'S'
    $afterS = substr($seating, $posS + 1);

    return array('T' => $betweenTS, 'S' => $afterS);
}

function splitStage($seating) {
    $posR = strpos($seating, 'R');
    $posS = strpos($seating, 'S');

    // Extract the substring between 'T' and 'S'
    $betweenRS = substr($seating, $posR + 1, $posS - $posR - 1);

    // Extract the substring after 'S'
    $afterS = substr($seating, $posS + 1);

    return array('R' => $betweenRS, 'S' => $afterS);
}
function rowsSeries($id) {
    $rows = array();
    
   $default = $rows[1] = [
    [   [68, 8, true],      [67, 8, true],   [48, 8, true],      [47, 8, true],      [28, 8, true],     [27, 8, true],      [9, 7, true],      [null, 9, true],      [null, 9, true],       [null, 9, true] , [null, 9, true],    [null, 9, true],     [null, 9, true]],
    [   [69, 8, true],   [66, 8, true],   [49, 8, true],     [46, 8, true],     [29, 8, true],     [26, 8, true],      [10, 7, false],         [8, 8, false],         [ null, 9, true],     [null, 9, true] , [null, 9, true],   [null, 9, true],     [null, 9, true]],
    [    [70, 8, true],   [65, 8, true],   [50, 8, true],     [45, 8, true],     [30, 8, true],     [25, 8, true],      [11, 7, false],        [7, 7, false],         ['B', 12, false,'s'],  [null, 9, true] ,[null, 9, true],   [null, 9, true],     [null, 9, true]],
    [   [71, 8, true],    [64, 8, true],   [51, 8, true],     [44, 8, true],     [31, 8, true],     [24, 8, true],      [12, 8, false],        [6, 7, false],         [null, 9, true],      [null, 9, true] ,[null, 9, true],   [null, 9, true],     [null, 9, true]],
    [   [72, 8, true],    [63, 8, true],   [52, 8, true],     [43, 8, true],     [32, 8, true],     [23, 8, true],      [null, 7, false],        [5, 7, false],         [null, 8, false],      [null, 9, true] ,[null, 9, true],   [null, 9, true],     [null, 9, true]],
    [  [null, 8, true],     [null, 9, true],   [null, 9, true],   [null, 9, true],   [null, 9, true],   [null, 9, true],    [null, 9, false],     [null, 7, true],      ['A', 14, false, 's'], [null, 9, true] ,[null, 9, true],   [null, 9, true],     [null, 9, true]],
    [   [73, 8, true],    [62, 8, true],   [53, 8, true],     [42, 8, true],     [33, 8, true],    [22, 8, true],      [14, 7, false],        [4, 7, false],         [null, 8, false],      [null, 9, true] ,[null, 9, true],   [null, 9, true],     [null, 9, true]],
    [   [74, 8, true],   [61, 8, true],   [54, 8, true],     [41, 8, true],     [34, 8, true],    [21, 8, true],      [15, 7, false],        [3, 7, false],         [null, 9, true],       [null, 9, true] ,[null, 9, true],   [null, 9, true],     [null, 9, true]],
    [    [75, 8, true],   [60, 8, true],   [55, 8, true],     [40, 8, true],     [35, 8, true],    [20, 8, true],      [16, 7, false],        [2, 7, false],         ['C', 12, false,'s'],  [null, 9, true],[null, 9, true],   [null, 9, true],     [null, 9, true] ],
    [   [76, 8, true],   [59, 8, true],   [56, 8, true],     [39, 8, true],     [36, 8, true],    [19, 8, true],     [17, 7, false],        [1, 7, false],         [null, 9, true],       [null, 9, true],[null, 9, true],   [null, 9, true],     [null, 9, true] ],
    [   [77, 8, true],   [58, 8, true],   [57, 8, true],      [38, 8, true],     [37,8, true],    [18, 8, true],     [null, 7, false],      [null, 9, true],      [null, 9, true],       [null, 9, true] ,[null, 9, true],   [null, 9, true],     [null, 9, true]]
];
    if(isset($rows[$id]))
        return $rows[$id];
    return $default;
}

function flagged_tables($session_id) {

    $rows = rowsSeries($session_id);
    $tabs = array();
    foreach($rows as $row):
        foreach($row as $table):
            if ( isset($table[2]) && $table[2] == true ) $tabs[] = $table[0];
        endforeach;
    endforeach;
    
    return $tabs;
}

function seating_name_pattern($string) {
    // Extracting the value after T
    preg_match('/T(\d+)/', $string, $matches);
    
    /* Code to be changed: This code should be changed with:  $T_value = $matches[1]; */
    if(empty($matches))
        $T_value = substr($string, 1, 1);
    else
        $T_value = $matches[1];
    /****************/
    

    // Extracting the value after S
    preg_match('/S(\d+)/', $string, $matches);
    $S_value = $matches[1];

    return array($T_value, $S_value);
}


function guidv4($data = null) {
    // Generate 16 bytes (128 bits) of random data or use the data passed into the function.
    $data = $data ?? random_bytes(16);
    assert(strlen($data) == 16);

    // Set version to 0100
    $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
    // Set bits 6-7 to 10
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80);

    // Output the 36 character UUID.
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

function uuid_qr($text) {
    return base64_encode(file_get_contents("https://quickchart.io/qr?text=" . $text));
}

function uuid_qr_bare($text){
    return file_get_contents("https://quickchart.io/qr?text=" . $text);
}
