<?php
defined( 'ABSPATH' ) or exit;   // show a blank page if try to access this file directly

require_once( EWZ_PLUGIN_DIR . 'classes/ewz-exception.php' );
require_once( EWZ_PLUGIN_DIR . 'classes/ewz-permission.php' );
require_once( EWZ_PLUGIN_DIR . 'classes/validation/ewz-settings-input.php' );
require_once( EWZ_PLUGIN_DIR . 'classes/ewz-settings.php' );
require_once( EWZ_PLUGIN_DIR . 'includes/ewz-common.php' );


/**
 * @param string   $_key
 * @param array<mixed>    $_opt
 * @param mixed    $val
 *
 * @return string
 */
function ewz_display_boolean_setting( $_key, $_opt, $val ){
    $chkd = $val ? ' checked' : '';
    $output = '<tr><td>' . $_opt['desc'] . "</td><td><input name='$_key' id='$_key' type='checkbox' $chkd></td></tr>";
    return $output;
}

/**
 * @param string   $_key
 * @param array<mixed>    $_opt
 * @param string    $val
 *
 * @return string
 */
function ewz_display_string_setting( $_key, $_opt, $val ){

    $escval =  ewz_safe_tags( $val );
    $output = '<tr><td>' . $_opt['desc'] . "</td><td>";

    $lines  = isset( $_opt['params']['lines'] )  ? $_opt['params']['lines'] : 1;
    $maxlen = isset( $_opt['params']['maxlen'] ) ? $_opt['params']['maxlen'] : 80;
    $width   = $maxlen > 50 ? 50 : $_opt['params']['maxlen'];

    if( $maxlen >= 70 && ( !isset( $_opt['params']['lines'] )) ){
        $lines = (int)min( array( $maxlen/50 + 1, $maxlen/100 + 2, $maxlen/200 + 3, 6 ) );
    }
    if( $lines > 1 ){
        $output .=   " <textarea  name='$_key' id='$_key'  maxlength='$maxlen' rows='$lines' cols='$width' > $escval </textarea>" ;
    } else {
        $output .=   " <input type='text'  name='$_key' id='$_key'  maxlength='$maxlen' size='$width' value='$escval'> ";
    }
    $output .=   '</td></tr>';
    return $output;
}

/**
 * @param string   $_key
 * @param array<mixed>    $_opt
 * @param int    $val
 *
 * @return string
 */
function ewz_display_int_setting($_key, $_opt, $val){

    $output = '<tr><td>' . $_opt['desc'] . "</td>";
    $output .=   " <td><select name='$_key' id='$_key'>";
    for( $i = $_opt['params']['min']; $i <= $_opt['params']['max']; $i+=$_opt['params']['step'] ){
        $esc_sel = ( $i == $val ) ? " selected" : '';
        $output .=   " <option value='$i' $esc_sel>$i</option>";
    }
    $output .=   '</select></td></tr>';
    return $output;
}

/**
 * @param string   $_key
 * @param array<mixed>    $_opt
 * @param string    $val
 *
 * @return string
 */
function ewz_display_select_setting( $_key, $_opt, $val ){

    $output = '<tr><td>' . $_opt['desc'] . "</td><td><select name='$_key' id='$_key'>";
    foreach( $_opt['params']['opts'] as $returnval => $display ){
        $esc_sel = ( $val == $returnval ) ? 'selected' : '';
        $output .= "<option value='$returnval' $esc_sel>" . esc_html( $display ) . "</option>";
    }
    $output .= "</select></td></tr>";
    return $output;
}

/**
 * @param string   $_key
 * @param array<mixed>    $_opt
 * @param string    $val
 *
 * @return string
 */
function ewz_display_info_setting($_key, $_opt, $val){

    $output = '<tr><td>' . $_opt['desc'] . "</td>";
    $output .=   " <td></td></tr>";
    return $output;
}

/**
 * Generates the main settings management page
 *
 * Checks for POST data, validates and processes it
 * Then outputs the page
 *
 * @return void
 */
function ewz_settings_menu()
{
    if ( !current_user_can( 'manage_options' ) ) {
        throw new EWZ_Exception( "Sorry, you do not have permission to view this page" );
    }

    /***********************
     * process any changes
     ***********************/
    if ( !empty( $_POST ) ) {

        $settings_data = new Ewz_Settings_Input( $_POST );
        $settings = new Ewz_Settings( $settings_data->get_input_data() );
        $settings->save();
    }

    /******************
     * Display
     ******************/
?>

    <div class="wrap">
       <h2>EntryWizard Settings Management</h2>

       <form id="ewz_settings" name="ewz_settings" action=""  method="POST" >
           <div class="ewzform ewz_scrollx">
              <?php wp_nonce_field( 'ewzadmin', 'ewznonce' ); ?>
              <input type="hidden" name="ewzmode" value="settings">
              <table class="ewz_settings_tbl">

<?php

    $settings = new Ewz_Settings();  // sets options from db
    $defaults = $settings->get_defaults();

    foreach( $defaults as $_key => $_opt ){
        switch( $_opt['type'] ){
        case 'boolean': echo ewz_display_boolean_setting($_key, $_opt, $settings->get_setting($_key) );
            break;
        case 'integer': echo ewz_display_int_setting($_key, $_opt, $settings->get_setting($_key));
            break;
        case 'string': echo ewz_display_string_setting($_key, $_opt, $settings->get_setting($_key));
            break;
        case 'select': echo ewz_display_select_setting($_key, $_opt, $settings->get_setting($_key));
            break;
        case 'info': echo ewz_display_info_setting($_key, $_opt, $settings->get_setting($_key));
            break;
        default: throw new EWZ_Exception( 'Invalid Settings key ', $_opt['type'] );
        }
    }

?>
              </table>
           </div>
           <div class="ewz_buttonrow">
              <button type="submit" id="settings_submit">Save Changes</button>
           </div>
       </form>
    </div>
<?php

}

// end ewz_settings_menu
