<?php
/**
 * app/classes/config.php
 *
 * This class handles all the hard-coded configurations.
 *
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Classes;

use TheTempusProject\Houdini\Classes\Forms;
use TheTempusProject\Houdini\Classes\Template;
use TheTempusProject\Canary\Bin\Canary as Debug;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Bedrock\Classes\Config as BedrockConfig;

class Config extends BedrockConfig {
    public static function getFieldEditHtml( $name, $includeProtected = false ) {
        $node = self::get( $name );
        if ( empty( $node ) ) {
            return;
        }
        if ( true === $node['protected'] ) {
            return;
        }
        $fieldname = str_ireplace( '/', '-', $name );

        $html = '';
        $fieldHtml = '';
        switch ( $node['type'] ) {
            case 'radio':
            case 'bool':
            case 'boolean':
                $fieldHtml = Forms::getSwitchHtml( $fieldname, $node['value'] );
                break;
            case 'select':
                $fieldHtml = Forms::getSelectHtml( $fieldname, $options, $node['value'] );
                break;
            case 'block':
                $fieldHtml = Forms::getTextBlockHtml( $fieldname, $node['value'] );
                break;
            case 'text':
            case 'url':
                $fieldHtml = Forms::getTextHtml( $fieldname, $node['value'] );
                break;
            case 'checkbox':
                $fieldHtml = Forms::getCheckboxHtml( $fieldname, $node['value'] );
                break;
            case 'timezone':
                $fieldHtml = Forms::getTimezoneHtml( $node['value'] );
                break;
            case 'file':
                $fieldHtml = Forms::getFileHtml( $fieldname );
                break;
            case 'customSelect':
                if ( empty( $options ) ) {
                    $options = '{' . $fieldname . '-options}';
                }
                $fieldHtml = Forms::getSelectHtml( $fieldname, $options, $node['value'] );
                break;
        }

        $html .= '<div class="mb-3 row">';
        $html .= '<label for="' . $fieldname . '" class="col-lg-3 col-form-label text-end">' . $node['pretty'] . '</label>';
        $html .= '<div class="col-lg-6">';
        $html .= $fieldHtml;
        $html .= '</div>';
        if ( 'file' === $node['type'] ) {
            $html .= '<div class="mb-3 row">';
            $html .= '<h4 class="col-lg-3 col-form-label text-end">Current Image</h4>';
            $html .= '<div class="col-lg-6">';
            $html .= '<img alt="User Avatar" src="{ROOT_URL}' . $node['value'] . '" class="img-circle img-fluid p-2 avatar-125">';
            $html .= '</div>';
        }
        $html .= '</div>';

        return Template::parse( $html );
    }

    public static function getCategoryEditHtml( $category ) {
        $html = '';
        if ( self::$config === false ) {
            Debug::warn( 'Config not loaded.' );
            return;
        }
        if ( empty( self::$config[$category] ) ) {
            Debug::warn( "Config category not found: $category" );
            return;
        }
        $categoryHeader = '<div class=""><h3 class="text-center">' . ucfirst( $category ) . ':</h3><hr>';
        foreach ( self::$config[$category] as $field => $node ) {
            $html .= self::getFieldEditHtml( $category . '/' . $field );
        }
        if ( !empty( $html ) ) {
            $html = $categoryHeader . $html . '</div>';
        }
        return $html;
    }

    public static function getEditHtml() {
        if ( self::$config === false ) {
            Debug::warn( 'Config not loaded.' );
            return;
        }
        $html = '';
        foreach ( self::$config as $category => $fields ) {
            $html .= self::getCategoryEditHtml( $category );
        }
        return $html;
    }
}