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

    class cloudS3StorageMime {

        public $page;

        public function __construct() {
            if (false === get_option('cloud_s3_storage_mime_types')) {
                $this->set_default_mime_types();
            }

            $this->page = new cloudS3StoragePage();
            add_action('cloud_s3_storage_menu_hook', [$this, 'cloud_s3_storage_mime_menu']);
            add_action('admin_init', [$this, 'save_mime_type_settings']);
        }

        static function get_mime_type() {
            $mime_types = wp_get_mime_types();
            $keys = array_keys($mime_types); 
            $mime_types_string = implode(', ', $keys);
            return $mime_types_string;
        }

        static function get_mime_type_array() {
            return array_keys(wp_get_mime_types());
        }

        static function get_cloudS3Storage_mime_type_array() {
            $mime_types = get_option('cloud_s3_storage_mime_types');
            return $mime_types ? $mime_types : self::get_mime_type_array();
        }

        static function get_cloudS3Storage_mime_type() {
            return implode(', ', self::get_cloudS3Storage_mime_type_array());
        }

        private function set_default_mime_types() {
            $mime_types = self::get_mime_type_array();
            update_option('cloud_s3_storage_mime_types', $mime_types);
        }

        static function get_unselected_mime_types_array() {
            $all_mime_types = self::get_mime_type_array();
            $selected_mime_types = self::get_cloudS3Storage_mime_type_array();

            $unselected_mime_types = array_diff($all_mime_types, $selected_mime_types);

            return $unselected_mime_types;
        }

        static function get_unselected_mime_types() {
            return implode(", ", self::get_unselected_mime_types_array());
        }

        public function cloud_s3_storage_mime_menu() {
            add_submenu_page(
                'cloud-s3-settings',         
                'MIME Types Settings', 
                'MIME Types',  
                'manage_options', 
                'cloud-s3-storage-mime',
                array($this, 'render_mime_type_form')
            );
        }

        public function render_mime_type_form() {
            $available_mime_types = self::get_mime_type_array(); 
            $selected_mime_types = self::get_cloudS3Storage_mime_type_array(); 
            $unselected_mime_types = self::get_unselected_mime_types_array();
        
            $data = [
                'available_mime_types' => $available_mime_types,
                'selected_mime_types' => $selected_mime_types,
                'unselected_mime_types' => $unselected_mime_types,
            ];
        
            $this->page->getTemplate('cloud-s3-mime-conf', $data);
        }

        public function save_mime_type_settings() {
            if (isset($_POST['cloud_s3_mime_types_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['cloud_s3_mime_types_nonce'])), 'save_cloud_s3_mime_types') ) {
                if (isset($_POST['cloud_s3_mime_types']) && is_array($_POST['cloud_s3_mime_types'])) {
                    $selected_mime_types = array_map('sanitize_text_field', $_POST['cloud_s3_mime_types']);
                    update_option('cloud_s3_storage_mime_types', $selected_mime_types);
                }
            }
        }
    }

