<?php
namespace DS_Plugin;

if (!defined('ABSPATH')) { exit; }

class Post_Types {
    public static function init() {
        add_action('init', [__CLASS__, 'register_templates_cpt']);
        add_action('init', [__CLASS__, 'register_template_meta']);
        add_action('init', [__CLASS__, 'register_designs_cpt']);
        add_filter('use_block_editor_for_post_type', [__CLASS__, 'force_classic_editor_for_templates'], 10, 2);
    }

    public static function force_classic_editor_for_templates($use_block_editor, $post_type) {
        if ($post_type === 'ds_template') {
            return false;
        }
        return $use_block_editor;
    }

    public static function register_templates_cpt() {
        $labels = [
            'name' => __('Design Templates', 'ds'),
            'singular_name' => __('Design Template', 'ds'),
            'add_new_item' => __('Add New Template', 'ds'),
            'edit_item' => __('Edit Template', 'ds'),
            'menu_name' => __('Lorandit DS Pro', 'ds'),
        ];
        register_post_type('ds_template', [
            'labels' => $labels,
            'public' => false,
            'show_ui' => true,
            'show_in_menu' => true,
            'menu_icon' => 'dashicons-art',
            'supports' => ['title', 'thumbnail'],
            'show_in_rest' => false,
            'capability_type' => 'post',
        ]);
    }

    public static function register_template_meta() {
        $args = [
            'single' => true,
            'type' => 'number',
            'auth_callback' => function() { return current_user_can('edit_posts'); },
            'show_in_rest' => [
                'schema' => [
                    'type' => 'number',
                    'context' => ['view', 'edit'],
                ],
            ],
        ];

        register_post_meta('ds_template', 'width_mm', array_merge($args, ['default' => 200]));
        register_post_meta('ds_template', 'height_mm', array_merge($args, ['default' => 90]));
        register_post_meta('ds_template', 'bleed_mm', array_merge($args, ['default' => 3]));
        register_post_meta('ds_template', 'dpi', array_merge($args, ['default' => 300]));
        register_post_meta('ds_template', 'preview_type', [
            'single' => true,
            'type' => 'string',
            'default' => 'mug',
            'auth_callback' => function() { return current_user_can('edit_posts'); },
            'show_in_rest' => [
                'schema' => [
                    'type' => 'string',
                    'enum' => ['mug', 'bottle', 'tshirt', 'magnet', 'keychain', 'watch', 'photo_stone', 'none'],
                    'context' => ['view', 'edit'],
                ],
            ],
        ]);
        register_post_meta('ds_template', 'preview_mode', [
            'single' => true,
            'type' => 'string',
            'default' => '2d',
            'auth_callback' => function() { return current_user_can('edit_posts'); },
            'show_in_rest' => [
                'schema' => [
                    'type' => 'string',
                    'enum' => ['2d', '3d'],
                    'context' => ['view', 'edit'],
                ],
            ],
        ]);
        register_post_meta('ds_template', 'zones_json', [
            'single' => true,
            'type' => 'string',
            'default' => '',
            'auth_callback' => function() { return current_user_can('edit_posts'); },
            'show_in_rest' => [
                'schema' => [
                    'type' => 'string',
                    'context' => ['view', 'edit'],
                ],
            ],
        ]);
        register_post_meta('ds_template', 'preview_config_json', [
            'single' => true,
            'type' => 'string',
            'default' => '',
            'auth_callback' => function() { return current_user_can('edit_posts'); },
            'show_in_rest' => [
                'schema' => [
                    'type' => 'string',
                    'context' => ['view', 'edit'],
                ],
            ],
        ]);

        $mockup_meta_args = [
            'single' => true,
            'type' => 'string',
            'default' => '',
            'auth_callback' => function() { return current_user_can('edit_posts'); },
            'show_in_rest' => [
                'schema' => [
                    'type' => 'string',
                    'context' => ['view', 'edit'],
                ],
            ],
        ];
        register_post_meta('ds_template', 'mockup_front_url', $mockup_meta_args);
        register_post_meta('ds_template', 'mockup_back_url', $mockup_meta_args);
        register_post_meta('ds_template', 'mockup_left_url', $mockup_meta_args);
        register_post_meta('ds_template', 'mockup_right_url', $mockup_meta_args);
    }

    public static function register_designs_cpt() {
        $labels = [
            'name' => __('Designs', 'ds'),
            'singular_name' => __('Design', 'ds'),
            'add_new_item' => __('Add New Design', 'ds'),
            'edit_item' => __('Edit Design', 'ds'),
            'menu_name' => __('Designs', 'ds'),
        ];
        register_post_type('ds_design', [
            'labels' => $labels,
            'public' => false,
            'show_ui' => true,
            'show_in_menu' => 'edit.php?post_type=ds_template',
            'menu_icon' => 'dashicons-media-document',
            'supports' => ['title'],
            'show_in_rest' => false,
            'capability_type' => 'post',
        ]);

        register_post_meta('ds_design', 'template_id', [ 'single' => true, 'type' => 'integer', 'auth_callback' => '__return_true' ]);
        register_post_meta('ds_design', 'png_url', [ 'single' => true, 'type' => 'string', 'auth_callback' => '__return_true' ]);
        register_post_meta('ds_design', 'layers_path', [ 'single' => true, 'type' => 'string', 'auth_callback' => '__return_true' ]);
        register_post_meta('ds_design', 'user_id', [ 'single' => true, 'type' => 'integer', 'auth_callback' => '__return_true' ]);
        register_post_meta('ds_design', 'visibility', [ 'single' => true, 'type' => 'string', 'auth_callback' => '__return_true', 'default' => 'private' ]);
        register_post_meta('ds_design', 'printed', [ 'single' => true, 'type' => 'boolean', 'auth_callback' => '__return_true', 'default' => false ]);
        register_post_meta('ds_design', 'printed_at', [ 'single' => true, 'type' => 'string', 'auth_callback' => '__return_true' ]);
        register_post_meta('ds_design', 'order_id', [ 'single' => true, 'type' => 'integer', 'auth_callback' => '__return_true' ]);
        register_post_meta('ds_design', 'order_item_id', [ 'single' => true, 'type' => 'integer', 'auth_callback' => '__return_true' ]);
    }
}
