<?php
/**
 * Generates settings interface.
 *
 * @since 1.0.0
 */
function doc8_admin_settings_page() {
	?>
	<div class="wrap docu-wrap">
		<h1 class="page-title"><?php _e( 'Document Settings', 'docu' ); ?></h1>

		<form method="post" action="options.php">
			<?php settings_fields( 'doc8_settings' ); ?>
			<?php do_settings_sections( 'doc8_settings' ); ?>
			<?php submit_button(); ?>
		</form>
	</div>
	<?php
}

/**
 * Callback function that save/update code attachments sample when a document is save.
 *
 * @param int $post_id
 */
function doc8_save_code_attachments( $post_id ) {
    if ( wp_is_post_revision( $post_id ) || empty($_POST['doc8_attachments']) ) {
        return;
    }

    $attachments = $_POST['doc8_attachments'];

    if ( empty($attachments['file']) ) {
        return;
    }

    $files = array();
    foreach ( $attachments['file'] as $pos => $file ) {
        if ( empty($file) ) {
            continue;
        }
        $files[] = array(
            'type' => $attachments['type'][$pos],
            'file' => $file,
        );
    }

    update_post_meta( $post_id, 'doc8_attachments', $files );
}

/**
 * Add code sample meta box at edit document page.
 */
function doc8_meta_boxes() {
    add_meta_box(
        'doc8-attachments',
        __( 'Code Attachments', 'docu' ),
        'doc8_code_attachment_metabox',
        doc8_get_post_type(),
        'normal'
    );
}

/**
 * Generate code attachments samples.
 *
 * @param $doc
 */
function doc8_code_attachment_metabox( $doc ) {
    ?>
    <p class="description"><?php _e( 'Add sample attachments that supports your document.', 'docu' ); ?></p>
    <p class="doc8-add">
        <button type="button" class="doc8-add-new">
            <span class="dashicons dashicons-plus-alt"></span> <?php _e( 'Add New', 'docu' ); ?>
        </button>
    </p>
    <?php

    $attchment_types = doc8_get_attachments_types();
    $attachments = doc8_get_attachments( $doc->ID );

    $count = count( $attachments );
    $count = max( $count, 1 );

    for( $i=0; $i < $count; $i++ ) :
    ?>
    <div class="doc8-attachments">
        <button type="button" class="doc8-toggle">
            <span class="screen-reader-text"><?php _e( 'Toggle sample code', 'docu' ); ?></span>
            <span class="dashicons dashicons-arrow-down-alt2"></span>
        </button>
        <select name="doc8_attachments[type][]">
            <?php
            $value = ! empty($attachments) && ! empty($attachments[$i]) ? esc_textarea( $attachments[$i]['file'] ) : '';

            foreach ( $attchment_types as $type_id => $type ) :
                $is_type = ! empty($attachments) && ! empty($attachments[$i]) && $attachments[$i]['type'] == $type_id;
                ?>
            <option value="<?php echo $type_id; ?>" <?php selected( true, $is_type ); ?>><?php echo $type; ?></option>
            <?php endforeach; ?>
        </select>
        <button type="button" class="doc8-remove" title="<?php _e( 'Remove code sample', 'docu' ); ?>">
            <span class="screen-reader-text"><?php _e( 'Remove code sample', 'docu' ); ?></span>
            <span class="dashicons dashicons-trash"></span>
        </button>
        <textarea name="doc8_attachments[file][]" class="widefat" rows="5"><?php echo $value; ?></textarea>
    </div>

<?php
    endfor;

    wp_enqueue_style( 'doc8-admin-css' );
    wp_enqueue_script( 'doc8-admin-js' );
}
