<?php
namespace DOZENT_LMS_CERTIFICATE;

defined( 'ABSPATH' ) || exit;

/**
 * Class PostType
 *
 * @package Dozent
 *
 */

class Certificate_Metabox{

	public function __construct(){
		add_action( 'admin_init', [ $this, 'admin_init' ] );
		add_action( 'add_meta_boxes', [ $this, 'certificate_metabox' ] );
		add_action( 'save_post_dozent_certificate', [ $this, 'save_template' ] );
		add_filter( 'admin_post_thumbnail_html', [ $this, 'admin_post_thumbnail_html' ], 10, 3 );
	}

	public function admin_init(){
		add_filter( 'dozent_load_colorpicker', '__return_true', 100 );
	}

	public function certificate_metabox(){
		add_meta_box( 'certificate-builder', __( 'Certificate Builder', 'dozent-lms-certificate' ), [ $this, 'certificate_builder' ], 'dozent_certificate' );
	}

	public function certificate_builder(){
		include DLC_ABSPATH . 'core/views/certificate_builder.php';
	}

	public function save_template( $pos_ID ){
		if ( isset( $_POST[ '_certificate_content' ] ) ) {
			$certificate_content = sanitize_textarea_field( $_POST[ '_certificate_content' ] );
			update_post_meta( $pos_ID, '_certificate_content', $certificate_content );
		}

		//Save Settings

		if ( isset( $_POST['_certificate_content_wrapper_width'] ) ) {
			$wrapper_width = sanitize_textarea_field( $_POST['_certificate_content_wrapper_width'] );
			update_post_meta( $pos_ID, '_certificate_content_wrapper_width', $wrapper_width );
		}

		if ( isset( $_POST['_certificate_content_wrapper_font_family'] ) ) {
			$wrapper_font_family = sanitize_textarea_field( $_POST['_certificate_content_wrapper_font_family'] );
			update_post_meta( $pos_ID, '_certificate_content_wrapper_font_family', $wrapper_font_family );
		}

		if ( isset( $_POST['_certificate_content_wrapper_textalign'] ) ) {
			$wrapper_text_align = sanitize_textarea_field( $_POST['_certificate_content_wrapper_textalign'] );
			update_post_meta( $pos_ID, '_certificate_content_wrapper_textalign', $wrapper_text_align );
		}

		if ( isset( $_POST['_certificate_content_wrapper_font_color'] ) ) {
			$wrapper_text_color = sanitize_textarea_field( $_POST['_certificate_content_wrapper_font_color'] );
			update_post_meta( $pos_ID, '_certificate_content_wrapper_font_color', $wrapper_text_color );
		}

		if ( isset( $_POST['_certificate_content_wrapper_font_size'] ) ) {
			$wrapper_font_size = sanitize_textarea_field( $_POST['_certificate_content_wrapper_font_size'] );
			update_post_meta( $pos_ID, '_certificate_content_wrapper_font_size', $wrapper_font_size );
		}

		if ( isset( $_POST['_certificate_content_wrapper_font_size'] ) ) {
			$wrapper_font_size = sanitize_textarea_field( $_POST['_certificate_content_wrapper_font_size'] );
			update_post_meta( $pos_ID, '_certificate_content_wrapper_font_size', $wrapper_font_size );
		}

		if ( isset( $_POST['_certificate_content_wrapper_mt'] ) ) {
			$wrapper_mt = sanitize_textarea_field( $_POST['_certificate_content_wrapper_mt'] );
			update_post_meta( $pos_ID, '_certificate_content_wrapper_mt', $wrapper_mt );
		}

		if ( isset( $_POST['_certificate_content_wrapper_mb'] ) ) {
			$wrapper_mb = sanitize_textarea_field( $_POST['_certificate_content_wrapper_mb'] );
			update_post_meta( $pos_ID, '_certificate_content_wrapper_mb', $wrapper_mb );
		}

		if ( isset( $_POST['_certificate_content_wrapper_ml'] ) ) {
			$wrapper_ml = sanitize_textarea_field( $_POST['_certificate_content_wrapper_ml'] );
			update_post_meta( $pos_ID, '_certificate_content_wrapper_ml', $wrapper_ml );
		}

		if ( isset( $_POST['_certificate_content_wrapper_mr'] ) ) {
			$wrapper_mr = sanitize_textarea_field( $_POST['_certificate_content_wrapper_mr'] );
			update_post_meta( $pos_ID, '_certificate_content_wrapper_mr', $wrapper_mr );
		}

		if ( isset( $_POST['_certificate_student_name_font_family'] ) ) {
			$student_name_font_family = sanitize_textarea_field( $_POST['_certificate_student_name_font_family'] );
			update_post_meta( $pos_ID, '_certificate_student_name_font_family', $student_name_font_family );
		}

		if ( isset( $_POST['_certificate_student_name_font_size'] ) ) {
			$student_name_font_size = sanitize_textarea_field( $_POST['_certificate_student_name_font_size'] );
			update_post_meta( $pos_ID, '_certificate_student_name_font_size', $student_name_font_size );
		}

		//
		// die( var_dump( $_POST['_certificate_content_wrapper_width'] ) );
	}

	public function admin_post_thumbnail_html( $content, $post_id, $thumbnail_id ){

		$post_type = get_post_type( $post_id );

		if ( $post_type === 'dozent_certificate' ) {
			$content .= '<p class="hide-if-no-js howto">' . sprintf( __( 'Perfect certificate image size : w-%s, h-%s', 'dozent-lms-certificate' ), '<strong>1704px</strong>', '<strong>1212px</strong>' ) . '</p>';
		}
		return $content;
	}

}