<?php

namespace Pay4AllForMypos\Support;

class Paths
{
    public static function get_allowed_subdirs()
    {
        return [
            'locks',
            'logs',
            'temp'
        ];
    }

    public static function get_public_subdirs()
    {
        return [];
    }

    public static function get_options()
    {
        return get_option( PAY4ALL_FOR_MYPOS_PREFIX . '_options_group' );
    }

    public static function update_options( $data )
    {
        $options                  = static::get_options();
        $options                  = array_merge( $options, $data );
        $options[ 'no_sanitize' ] = true;
        // Careful: password cslashes
        update_option( PAY4ALL_FOR_MYPOS_PREFIX . '_options_group', $options );
    }

    /**
     * Returns the settings array of the registered WC payment gateway by
     * its slug. Returns null when WooCommerce isn't loaded or when the
     * gateway isn't registered (typical on Pay4All Shop-only installs).
     *
     * @param string $gateway_slug e.g. `pay4all_for_mypos`.
     * @return array|null
     */
    public static function get_gateway_settings( $gateway_slug )
    {
        if ( ! function_exists( 'WC' ) ) {
            return null;
        }
        $gateways = \WC()->payment_gateways->payment_gateways();
        if ( ! isset( $gateways[ $gateway_slug ] ) ) {
            return null;
        }
        return $gateways[ $gateway_slug ]->settings;
    }

    public static function get_license_options()
    {
        return get_option( PAY4ALL_FOR_MYPOS_PREFIX . '_license_options' );
    }

    public static function update_license_options( $key, $status )
    {
        update_option( PAY4ALL_FOR_MYPOS_PREFIX . '_license_options', [ 'license_key' => $key, 'license_status' => $status ] );
    }

    public static function get_plugin_uploads_dir_path()
    {
        return static::get_or_create_dir( PAY4ALL_FOR_MYPOS_UPLOAD_DIR );
    }

    /**
     * Returns the plugin  uploads directory with optional subdirectory. Includes trailing slash.
     *
     * @param null $subdir
     * @return string
     */
    public static function get_upload_path( $subdir = null )
    {
        $dir = PAY4ALL_FOR_MYPOS_UPLOAD_DIR;

        if ( $subdir ) {

            if ( in_array( $subdir, static::get_allowed_subdirs() ) ) {

                $dir .= $subdir . DIRECTORY_SEPARATOR;

            }
        }

        return $dir;
    }

    public static function create_dirs()
    {
        $upload_path = PAY4ALL_FOR_MYPOS_UPLOAD_DIR;

        if ( ! file_exists( $upload_path ) ) {
            wp_mkdir_p( $upload_path );
            self::write_silence_index( $upload_path );
        }

        $folders = static::get_allowed_subdirs();
        foreach ( $folders as $folder ) {
            $path = $upload_path . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR;
            if ( ! file_exists( $path ) ) {
                wp_mkdir_p( $path );
                self::write_silence_index( $path );
            }
        }
    }


    /**
     * Returns the path to the log file. Creates an empty file if missing.
     *
     * @return string
     */
    public static function get_log_file_path()
    {
        $name = gmdate( 'd-m-Y' );
        $path = static::get_upload_path( 'logs' ) . $name . '.log';

        if ( ! file_exists( $path ) ) {
            // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- log seed only, WP_Filesystem is overkill and requires FTP credentials on some hosts.
            @file_put_contents( $path, '' );
        }

        return $path;
    }

    private static function get_or_create_dir( $path )
    {
        if ( ! file_exists( $path ) ) {
            wp_mkdir_p( $path );
            self::write_silence_index( $path );
        }

        return $path;
    }

    /**
     * Drops a "silence is golden" index.php into $dir to prevent Apache
     * directory listing without touching .htaccess (WP.org's guideline 6
     * forbids plugins from writing their own .htaccess files, and Nginx
     * ignores it anyway - a bare index.php is the portable equivalent).
     */
    private static function write_silence_index( string $dir ) : void
    {
        $file = rtrim( $dir, "/\\" ) . DIRECTORY_SEPARATOR . 'index.php';
        if ( file_exists( $file ) ) { return; }
        // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- one-shot silencer file, WP_Filesystem asks for FTP creds on shared hosts.
        @file_put_contents( $file, "<?php\n// Silence is golden.\n" );
    }
}
