<?php
namespace OxygenApp\Hooks;

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

use OxygenApp\Models\Background;
use OxygenApp\Models\BackgroundSound;
use OxygenApp\Utils\Utils;

class HookManageCptColumns implements IHook {
    static function register() {
        add_filter('manage_' . Background::POST_TYPE . '_posts_columns' , [self::class, 'backgroundCustomListingColumns']);
        add_action('manage_' . Background::POST_TYPE . '_posts_custom_column' , [self::class, 'backgroundPopulateCustomListingColumns'], 10, 2);

        add_filter('manage_' . BackgroundSound::POST_TYPE . '_posts_columns' , [self::class, 'backgroundSoundCustomListingColumns']);
        add_action('manage_' . BackgroundSound::POST_TYPE . '_posts_custom_column' , [self::class, 'backgroundSoundPopulateCustomListingColumns'], 10, 2);
    }

    static function backgroundCustomListingColumns($columns) {
        $newColumns = Utils::addBeforeLastElement($columns, 'background_type', esc_html__('Type', 'oxyleaf-app-working-and-relaxing-app'));
        $newColumns = Utils::addBeforeLastElement($newColumns, 'background_file_type', esc_html__('File type', 'oxyleaf-app-working-and-relaxing-app'));
        return Utils::addBeforeLastElement($newColumns, 'background_preview', esc_html__('Preview', 'oxyleaf-app-working-and-relaxing-app'));
    }

    static function backgroundPopulateCustomListingColumns($column, $postId) {
        if ($column === 'background_type') {
            $isPremiumBackground = get_post_meta($postId, 'is_premium_' . Background::POST_META_KEY, true);

            if ($isPremiumBackground === 'on') {
                echo '<span style="color: #10d000;">'. esc_html__('Premium', 'oxyleaf-app-working-and-relaxing-app') .'</span>';
            } else {
                $requiresLogIn = get_post_meta($postId, 'requires_login_' . Background::POST_META_KEY, true);
                echo '<span style="color: #f39c12;">'. esc_html__('Free', 'oxyleaf-app-working-and-relaxing-app') .'</span>';
                if ($requiresLogIn) {
                    echo '<br><span style="color: #f39c12;">('. esc_html__('Requires login', 'oxyleaf-app-working-and-relaxing-app') .')</span>';
                }
            }
        }

        if ($column === 'background_file_type') {
            $type = get_post_meta($postId, 'type_' . Background::POST_META_KEY, true);

            if ($type === 'video') {
                echo '<span>'. esc_html__('Video', 'oxyleaf-app-working-and-relaxing-app') .'</span>';
            } else {
                echo '<span>'. esc_html__('Image', 'oxyleaf-app-working-and-relaxing-app') .'</span>';
            }
        }

        if ($column === 'background_preview') {
            $type = get_post_meta($postId, 'type_' . Background::POST_META_KEY, true);

            $imageId = '';
            if ($type === 'video') {
                $imageId = get_post_meta($postId, 'video_icon_file_id_' . Background::POST_META_KEY, true);
            } else {
                $imageId = get_post_meta($postId, 'bk_image_file_id_' . Background::POST_META_KEY, true);
            }
            if ($imageId && is_numeric($imageId)) {
                $iconUrl = wp_get_attachment_image_url($imageId, 'thumbnail');
            }

            if ($iconUrl !== '') {
                echo '<span><img style="max-height: 70px;" src="'. esc_url($iconUrl) .'" /></span>';
            }
        }
    }

    static function backgroundSoundCustomListingColumns($columns) {
        return Utils::addBeforeLastElement($columns, 'background_type', esc_html__('Type', 'oxyleaf-app-working-and-relaxing-app'));
    }

    static function backgroundSoundPopulateCustomListingColumns($column, $postId) {

        if ($column === 'background_type') {
            $isPremiumSound = get_post_meta($postId, 'is_premium_' . BackgroundSound::POST_META_KEY, true);

            if ($isPremiumSound === 'on') {
                echo '<span style="color: #10d000;">'. esc_html__('Premium', 'oxyleaf-app-working-and-relaxing-app') .'</span><br>';
            } else {
                $requiresLogIn = get_post_meta($postId, 'requires_login_' . BackgroundSound::POST_META_KEY, true);
                echo '<span style="color: #f39c12;">'. esc_html__('Free', 'oxyleaf-app-working-and-relaxing-app') .'</span>';
                if ($requiresLogIn) {
                    echo '<br><span style="color: #f39c12;">('. esc_html__('Requires login', 'oxyleaf-app-working-and-relaxing-app') .')</span>';
                }
            }
        }
    }
    
}