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

        public $mode;

        public function __construct($mode=false) {
            $this->mode = $mode;

            if($this->mode) {
                add_action('attachment_submitbox_misc_actions', array($this,'field_media_disable'));
                add_action('edit_attachment', array($this,'save_media_field_disable'));
            } else {
                add_action('attachment_submitbox_misc_actions', array($this,'field_media_enable'));
                add_action('edit_attachment', array($this,'save_media_field_enable'));
            }
        }

        public function field_media_enable () {
            $this->add_checkbox_in_media([
                'field' => 'enable_media_field',
                'label' => __('Enable for Cloud S3 Storage', 'cloud-s3-storage')
            ]);
        }

        public function save_media_field_enable ($post_id) {
            $this->save_media_checkbox(
                $post_id,
                [
                'field' => 'enable_media_field'
            ]);
        }

        public function field_media_disable () {
            $this->add_checkbox_in_media([
                'field' => 'disable_media_field',
                'label' => __('Disable for Cloud S3 Storage', 'cloud-s3-storage')
            ]);
        }

        public function save_media_field_disable ($post_id) {
            $this->save_media_checkbox(
                $post_id,
                [
                'field' => 'disable_media_field'
            ]);
        }

        public function add_checkbox_in_media($args) {
            $media_id = isset($_GET['post']) ? intval($_GET['post']) : 0;

            if (!$media_id) return;
            $checked='';
            if(get_post_meta($media_id, $args['field'], true)=='on') {
                $checked=1;
            }
            ?>
            <div class="misc-pub-section misc-pub-section-last">
                <label for="<?php echo esc_attr($args['field']); ?>">
                    <?php echo esc_attr($args['label']); ?>
                </label>
                <input type="checkbox" class="widefat urlfield" name="<?php echo esc_attr($args['field']); ?>" id="<?php echo esc_attr($args['field']); ?>" <?php checked($checked, '1'); ?> /> 

            </div>
            <?php
        }       
              
        public function save_media_checkbox($post_id, $args) {
        
            if ( isset($_POST[$args['field']]) ) {
                $value = sanitize_text_field(wp_unslash($_POST[$args['field']]));
                update_post_meta($post_id, $args['field'], $value);
            } else {
                delete_post_meta($post_id, $args['field']);
            }
        }

        public static function deleteFile ($file_path) {
            wp_delete_file($file_path);

            if (!file_exists($file_path)) {
                return true;
            } else {
                return false;
            }
            
            return false; 
            
        }
        
    }