<?php
/**
 * Created by PhpStorm.
 * User: tatu
 * Date: 6/23/20
 * Time: 12:10 AM
 */

use App\Models\AppSession;

if(!function_exists('get_current_session')) {
    function get_current_session(){
        return AppSession::where('session_key', request()->cookie('sessionKey'))->first();
    }
}

if(!function_exists('friendly_date_time')) {
    function friendly_date_time($value, $includeTime = true, $default = '-') {
        if(!$value || empty($value)) return $default;
        try {
            $result = strtotime($value);
            $result = date("jS M o" . ($includeTime ? ", H:i" : ""), $result);
            return $result;
        }
        catch (Exception $e) {
            return $value;
        }
    }
}

if(!function_exists('friendly_time')) {
    function friendly_time($value, $default = '-') {
        if(!$value || empty($value)) return $default;
        try {
            $result = strtotime($value);
            $result = date("H:i", $result);
            return $result;
        }
        catch (Exception $e) {
            return $value;
        }
    }
}

if(!function_exists('friendly_month')) {
    function friendly_month($value) {
        if(!$value || empty($value)) return "-";
        try {
            $result = strtotime($value);
            $result = date("M o", $result);
            return $result;
        }
        catch (Exception $e) {
            return $value;
        }
    }
}

if(!function_exists('time_in_hrminsec')) {
    function time_in_hrminsec($value, $default = '-') {
        if(!$value || empty($value)) return $default;
        $value = intval($value);
        $minutes = intval($value / 60);
        $seconds = $value % 60;
        $hours = 0;
        if($minutes >= 60) {
            $hours = intval($minutes / 60);
            $minutes = $minutes % 60;
        }
        $output = [];
        if($hours > 0) {
            $output[] = "{$hours}h";
        }
        if($minutes > 0) {
            $output[] = "{$minutes}m";
        }
        if($seconds > 0) {
            $output[] = "{$seconds}s";
        }
        return implode(" ", $output);
    }
}

if(!function_exists('sanitize_field_name')) {
    function sanitize_field_name($name) {
        $result = strtolower($name);
        return preg_replace("/[^0-9a-z]/i", "_", $result);
    }
}

if(!function_exists('renderNoteTemplate')) {
    function renderNoteTemplate($template, $topLevel)
    {
        echo
            '<div class="note-template-item" ' .
            'template="' . (isset($template->template) ? $template->template : $template->text) . '" ' .
            'type="' . (isset($template->type) ? $template->type : "value") . '" ' .
            '>' .
            '<div class="note-template-text d-flex align-items-center">' .
            '<span class="label">' .
            '<input type="checkbox" />' .
            '<span>' . $template->text . '</span>' .
            '</span>';
        if (isset($template->type) && $template->type === 'plus-minus') {
            echo '<div class="ml-auto mr-2 text-nowrap">';
            echo '<a href="#" class="plus-trigger"><i class="fa fa-plus-circle"></i></a>';
            echo '<a href="#" class="minus-trigger ml-1"><i class="fa fa-minus-circle"></i></a>';
            echo '</div>';
        }
        echo '</div>';
        if (isset($template->children) && count($template->children)) {
            echo '<i class="fa fa-chevron-right has-children"></i>';
            echo '<div class="note-template-children">';
            foreach ($template->children as $t) {
                renderNoteTemplate($t, false);
            }
            echo '</div>';
        } else if (isset($template->type) && $template->type !== 'plus-minus') {
            echo '<i class="fa fa-chevron-right has-children"></i>';
            echo '<div class="note-template-children">';
            if ($template->type === 'alpha') {
                echo '<textarea class="form-control form-control-sm"></textarea>';
            } else {
                echo '<input type="' . $template->type . '" class="form-control form-control-sm">';
            }
            echo '</div>';
        }
        echo '</div>';
    }
}

if(!function_exists('renderNoteTemplates')) {
    function renderNoteTemplates($path)
    {
        $templates = json_decode(file_get_contents($path));
        foreach ($templates->templates as $template) {
            renderNoteTemplate($template, true);
        }
    }
}

if(!function_exists('renderNoteExamTemplates')) {
    function renderNoteExamTemplates($parentPath, $childPath)
    {
        $templates = json_decode(file_get_contents($parentPath));
        $templates = $templates->templates;

        // override as needed with what is in template set
        if(file_exists($childPath)) {
            $orTemplates = json_decode(file_get_contents($parentPath));
            $orTemplates = $orTemplates->templates;
            for ($i = 0; $i < count($templates); $i++) {
                for ($j = 0; $j < count($orTemplates); $j++) {
                    if($templates[$i]->text === $orTemplates[$j]->text) {
                        $templates[$i] = $orTemplates[$j];
                    }
                }
            }
        }

        foreach ($templates as $template) {
            renderNoteTemplate($template, true);
        }
    }
}

if(!function_exists('getVal')) {
    function getVal($object, $prop)
    {
        if (isset($object->$prop)) {
            return $object->$prop;
        } else {
            return '';
        }
    }
}
