<?php

/*
 * Copyright (C) 2015-2025 Deciso B.V.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

namespace OPNsense\Base\FieldTypes;

use OPNsense\Core\Backend;

/**
 * Class JsonKeyValueStoreField, use a json encoded file as selection list
 * @package OPNsense\Base\FieldTypes
 */
class JsonKeyValueStoreField extends BaseListField
{
    /**
     * @var null source field
     */
    private $internalSourceField = null;

    /**
     * @var null source file pattern
     */
    private $internalSourceFile = null;

    /**
     * @var bool automatically select all when none is selected
     */
    private $internalSelectAll = false;

    /**
     * @var string action to send to configd to populate the provided source
     */
    private $internalConfigdPopulateAct = "";

    /**
     * @var int execute configd command only when file is older then TTL (seconds)
     */
    private $internalConfigdPopulateTTL = 3600;

    /**
     * @var bool sort by value (default is by key)
     */
     private $internalSortByValue = false;

    /**
     * @var array cache configd responses for the lifetime of this object
     */
    protected static $internalStaticContent = [];

    /**
     * @param string $value source field, pattern for source file
     */
    public function setSourceField($value)
    {
        $this->internalSourceField = $value;
    }

    /**
     * @param string $value optionlist content to use
     */
    public function setSourceFile($value)
    {
        $this->internalSourceFile = $value;
    }

    /**
     * @param string $value automatically select all when none is selected
     */
    public function setSelectAll($value)
    {
        if (strtoupper(trim($value)) == 'Y') {
            $this->internalSelectAll = true;
        } else {
            $this->internalSelectAll = false;
        }
    }

    /**
     * @param string $value configd action to run
     */
    public function setConfigdPopulateAct($value)
    {
        $this->internalConfigdPopulateAct = $value;
    }

    /**
     * @param string $value set TTL for config action
     */
    public function setConfigdPopulateTTL($value)
    {
        if (is_numeric($value)) {
            $this->internalConfigdPopulateTTL = $value;
        }
    }

    /**
     * populate selection data
     */
    protected function actionPostLoadingEvent()
    {
        $data = null;
        $sourcefield = null;
        if (isset($this->internalParentNode->{$this->internalSourceField})) {
            $sourcefield = basename($this->internalParentNode->{$this->internalSourceField});
        }

        if ($this->internalSourceFile != null && $sourcefield != null) {
            $sourcefile = sprintf($this->internalSourceFile, $sourcefield);
        } else {
            $sourcefile = $this->internalSourceFile;
        }
        $cachename = $sourcefile != null ? $sourcefile : $this->internalConfigdPopulateAct;
        if (!empty($this->internalConfigdPopulateAct)) {
            if (isset(static::$internalStaticContent[$cachename])) {
                /* cached for the lifetime of this session */
                $data = static::$internalStaticContent[$cachename];
            } elseif ($sourcefile != null) {
                /* use a file cache for the configd call*/
                if (is_file($sourcefile)) {
                    $sourcehandle = fopen($sourcefile, "r+");
                } else {
                    $sourcehandle = fopen($sourcefile, "w");
                }
                if (flock($sourcehandle, LOCK_EX)) {
                    $stat = fstat($sourcehandle);
                    $muttime = $stat['size'] == 0 ? 0 : $stat['mtime'];
                    if (time() - $muttime > $this->internalConfigdPopulateTTL) {
                        $act = $this->internalConfigdPopulateAct;
                        $response = (new Backend())->configdRun($act, false, 20);
                        if (!empty($response) && json_decode($response) !== null) {
                            // only store parsable results
                            fseek($sourcehandle, 0);
                            ftruncate($sourcehandle, 0);
                            fwrite($sourcehandle, $response);
                            fflush($sourcehandle);
                        }
                    }
                }
                flock($sourcehandle, LOCK_UN);
                fclose($sourcehandle);
                if (is_file($sourcefile)) {
                    $data = json_decode(file_get_contents($sourcefile), true);
                }
            } else {
                /* initial configd call  */
                $data = json_decode(
                    (new Backend())->configdRun($this->internalConfigdPopulateAct, false, 20),
                    true
                );
            }
        } elseif ($sourcefile != null && is_file($sourcefile)) {
            if (isset(static::$internalStaticContent[$cachename])) {
                $data = static::$internalStaticContent[$cachename];
            } else {
                $data = json_decode(file_get_contents($sourcefile), true) ?? [];
            }
        }
        if ($data != null) {
            static::$internalStaticContent[$cachename] = $data;

            // Support optgroups
            $this->internalOptionList = [];
            foreach ($data as $key => $value) {
                if (!is_array($value)) {
                    // flat form: { "id": "Label" }
                    $this->internalOptionList[$key] = $value;
                } else {
                    // grouped form: { "Group": { "id": "Label", ... } }
                    foreach ($value as $subkey => $subval) {
                        $this->internalOptionList[$subkey] = [
                            'optgroup' => gettext($key),
                            'value' => $subval,
                        ];
                    }
                }
            }

            if ($this->internalSelectAll && $this->internalValue == "") {
                $this->internalValue = implode(',', array_keys($this->internalOptionList));
            }
        }
    }

    /**
     * change default sorting order (value vs key)
     * @param $value boolean value Y/N
     */
    public function setSortByValue($value)
    {
        if (trim(strtoupper($value)) == "Y") {
            $this->internalSortByValue = true;
        } else {
            $this->internalSortByValue = false;
        }
    }

    /**
     * get valid options, descriptions and selected value
     * @return array
     */
    public function getNodeData()
    {
        // set sorting by key (default) or value
        if ($this->internalSortByValue) {
            natcasesort($this->internalOptionList);
        } else {
            ksort($this->internalOptionList);
        }
        return parent::getNodeData();
    }

}
