<?php
    if ( ! defined( 'ABSPATH' ) ) exit;

    class cloudS3StoragePage {

        CONST PATH_MODEL = '/../../assets/template/{file}.php';

        public $opt = array();

        public function __construct($opt = array())
        {
            $this->opt = $opt;
        }

        public function getTemplate($page, $data = array()) {
            if (!empty($data)) {
                extract($data);
            }
            include_once $this->getPath($page); 
        }
        

        public function getPath ($file) {
            return dirname(__FILE__).str_replace('{file}',$file,SELF::PATH_MODEL);
        }

        public function getFormInput($name, $format, $opt, $label) {
            if ($format == 'text') {
                $place_holder = isset($this->opt[$name]) && $this->opt[$name] !== '' ? esc_attr($this->opt[$name]) : '';
                echo '<input type="text" name="cloud_s3_settings[' . esc_attr($name) . ']" value="' . esc_attr($place_holder) . '" />';
            } elseif ($format == 'select') {
                $value = isset($this->opt[$name]) && $this->opt[$name] !== '' ? esc_attr($this->opt[$name]) : '';
                echo '<select name="cloud_s3_settings[' . esc_attr($name) . ']" id="cloud_s3_settings[' . esc_attr($name) . ']">';
                foreach ($opt as $key => $choice) {
                    $selected = (($value === '' && $key === 'never') || $value === $key) ? 'selected="selected"' : '';
                    echo '<option value="' . esc_attr($key) . '" ' . esc_attr($selected) . '>' . esc_html($choice) . '</option>';
                }
                echo '</select>';
            } else {
                $checked = (isset($this->opt[$name]) && $this->opt[$name] === 'on') ? 'checked="checked"' : '';
                echo '<input type="checkbox" name="cloud_s3_settings[' . esc_attr($name) . ']" ' . esc_attr($checked) . ' />';
            }
        
            if (!empty($label)) {
                echo '<p class="description">' . esc_html($label) . '</p>';
            }
        }             

        public function getFormInputField($args)  {
            $opt = isset($args['option'])?$args['option']:'';
            $label = isset($args['description'])?$args['description']:'';
            $this->getFormInput($args['name'],$args['format'],$opt, $label);
        }

        public function goForm ($options) {
            foreach($options['fields'] as $field) {
                add_settings_field(
                    $field['id'],
                    $field['name'],
                    [$this,'getFormInputField'],
                    $options['settings']['page'],
                    $options['settings']['section'],
                    [
                        "name" => $field['id'],
                        "format" => $field['type'],
                        "option" => isset($field['option'])?$field['option']:[],
                        "description" => isset($field['description']) ? $field['description'] : '',
                    ]
                );
            }
        }

    }