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

class CCGAL_CPT {

	const POST_TYPE = 'ccgal_gallery';

	public static function init() {
		add_action( 'init', array( __CLASS__, 'register_post_type' ) );
		add_action( 'init', array( __CLASS__, 'register_meta' ) );
	}

	public static function register_post_type() {
		$labels = array(
			'name'               => __( 'Galleries', 'customer-cloud-gallery' ),
			'singular_name'      => __( 'Gallery', 'customer-cloud-gallery' ),
			'menu_name'          => __( 'Customer Cloud Gallery', 'customer-cloud-gallery' ),
			'add_new'            => __( 'Add New Gallery', 'customer-cloud-gallery' ),
			'add_new_item'       => __( 'Add New Gallery', 'customer-cloud-gallery' ),
			'edit_item'          => __( 'Edit Gallery', 'customer-cloud-gallery' ),
			'new_item'           => __( 'New Gallery', 'customer-cloud-gallery' ),
			'view_item'          => __( 'View Gallery', 'customer-cloud-gallery' ),
			'search_items'       => __( 'Search Galleries', 'customer-cloud-gallery' ),
			'not_found'          => __( 'No galleries found', 'customer-cloud-gallery' ),
			'not_found_in_trash' => __( 'No galleries in trash', 'customer-cloud-gallery' ),
		);

		register_post_type(
			self::POST_TYPE,
			array(
				'labels'       => $labels,
				'public'       => false,
				'show_ui'      => true,
				'show_in_menu' => true,
				'menu_icon'    => 'dashicons-format-gallery',
				'menu_position'=> 25,
				'capability_type' => 'post',
				'supports'     => array( 'title' ),
				'show_in_rest' => false,
			)
		);
	}

	public static function register_meta() {
		$meta_fields = array(
			'ccgal_drive_folder_id'   => 'string',
			'ccgal_dropbox_folder_id' => 'string',
			'ccgal_storage_folder_id' => 'string',
			'ccgal_per_page'        => 'integer',
			'ccgal_load_mode'       => 'string',
			'ccgal_columns'         => 'integer',
			'ccgal_image_size'      => 'string',
			'ccgal_allow_downloads' => 'boolean',
			'ccgal_allow_favorites' => 'boolean',
			'ccgal_couple_name'     => 'string',
			'ccgal_sort_order'      => 'string',
			'ccgal_toolbar_align'   => 'string',
			'ccgal_cookies_enabled' => 'boolean',
			'ccgal_allow_print'     => 'boolean',
		);

		foreach ( $meta_fields as $key => $type ) {
			register_post_meta(
				self::POST_TYPE,
				$key,
				array(
					'type'         => $type,
					'single'       => true,
					'show_in_rest' => false,
					'auth_callback' => function() {
						return current_user_can( 'edit_posts' );
					},
				)
			);
		}
	}

	public static function get_gallery_config( $gallery_id ) {
		$gallery_id = absint( $gallery_id );
		if ( ! $gallery_id || get_post_type( $gallery_id ) !== self::POST_TYPE ) {
			return null;
		}

		$columns    = (int) ( get_post_meta( $gallery_id, 'ccgal_columns', true ) ?: 4 );
		$image_size = get_post_meta( $gallery_id, 'ccgal_image_size', true );
		if ( ! in_array( $image_size, array( 'small', 'medium', 'large', 'xlarge' ), true ) ) {
			$image_size = self::columns_to_image_size( $columns );
		}

		$sort_order = get_post_meta( $gallery_id, 'ccgal_sort_order', true );
		if ( ! in_array( $sort_order, self::valid_sort_orders(), true ) ) {
			$sort_order = 'name_natural';
		}

		$toolbar_align = get_post_meta( $gallery_id, 'ccgal_toolbar_align', true );
		if ( ! in_array( $toolbar_align, array( 'left', 'center', 'right' ), true ) ) {
			$toolbar_align = 'center';
		}

		return array(
			'id'              => $gallery_id,
			'title'           => get_the_title( $gallery_id ),
			'couple_name'     => get_post_meta( $gallery_id, 'ccgal_couple_name', true ),
			'drive_folder_id'   => get_post_meta( $gallery_id, 'ccgal_drive_folder_id', true )
			                     ?: get_post_meta( $gallery_id, 'ccgal_storage_folder_id', true ),
			'dropbox_folder_id' => get_post_meta( $gallery_id, 'ccgal_dropbox_folder_id', true ),
			'folder_id'         => get_post_meta( $gallery_id, 'ccgal_dropbox_folder_id', true )
			                     ?: get_post_meta( $gallery_id, 'ccgal_drive_folder_id', true )
			                     ?: get_post_meta( $gallery_id, 'ccgal_storage_folder_id', true ),
			'provider'          => get_post_meta( $gallery_id, 'ccgal_dropbox_folder_id', true )
			                     ? 'dropbox' : 'google_drive',
			'per_page'        => (int) ( get_post_meta( $gallery_id, 'ccgal_per_page', true ) ?: 40 ),
			'load_mode'       => get_post_meta( $gallery_id, 'ccgal_load_mode', true ) ?: 'scroll',
			'columns'         => $columns,
			'image_size'      => $image_size,
			'sort_order'      => $sort_order,
			'toolbar_align'   => $toolbar_align,
			'allow_downloads' => ( '0' !== get_post_meta( $gallery_id, 'ccgal_allow_downloads', true ) ),
			'allow_favorites' => ( '0' !== get_post_meta( $gallery_id, 'ccgal_allow_favorites', true ) ),
			'allow_print'     => ( '1' === get_post_meta( $gallery_id, 'ccgal_allow_print', true ) ),
			'cookies_enabled' => ( '0' !== get_post_meta( $gallery_id, 'ccgal_cookies_enabled', true ) ),
			'stats_enabled'   => apply_filters( 'ccgal_gallery_stats_enabled', false, $gallery_id ),
		);
	}

	public static function valid_sort_orders() {
		return array( 'name_natural', 'name', 'createdTime', 'createdTime desc', 'modifiedTime desc' );
	}

	public static function sort_order_label( $value ) {
		$labels = array(
			'name_natural'      => __( 'Filename (natural — 1, 2, …, 10, 11)', 'customer-cloud-gallery' ),
			'name'              => __( 'Filename (strict alphabetical — 1, 10, 2, 20)', 'customer-cloud-gallery' ),
			'createdTime'       => __( 'Upload date (oldest first)', 'customer-cloud-gallery' ),
			'createdTime desc'  => __( 'Upload date (newest first)', 'customer-cloud-gallery' ),
			'modifiedTime desc' => __( 'Modified date (newest first)', 'customer-cloud-gallery' ),
		);
		return isset( $labels[ $value ] ) ? $labels[ $value ] : $value;
	}

	public static function image_size_to_px( $image_size ) {
		$map = array(
			'small'  => 200,
			'medium' => 300,
			'large'  => 400,
			'xlarge' => 520,
		);
		return isset( $map[ $image_size ] ) ? $map[ $image_size ] : 300;
	}

	private static function columns_to_image_size( $columns ) {
		if ( $columns <= 2 ) return 'xlarge';
		if ( $columns === 3 ) return 'large';
		if ( $columns === 4 ) return 'medium';
		return 'small';
	}
}
