<?php
namespace Phox\core;

!defined('ABSPATH') && exit;

class Upgrade {
    /**
     * The Singleton's instance is stored in a static field. This field is an
     * array, because we'll allow our Singleton to have subclasses. Each item in
     * this array will be an instance of a specific Singleton's subclass. You'll
     * see how this works in a moment.
     */
    private static $instances = [];

    /**
     * The theme version as stored in the db.
     *
     * @access private
     * @var mixed $database_theme_version
     */
    private $database_theme_version;

    /**
     * The previous version.
     *
     * @access private
     * @var string $previous_theme_version
     */
    private $previous_theme_version;

    /**
     * The current version.
     *
     * @access private
     * @var string
     */
    private $current_theme_version;

    public static function getInstance(): Upgrade
    {
        $cls = static::class;
        if (!isset(self::$instances[$cls])) {
            self::$instances[$cls] = new static();
        }

        return self::$instances[$cls];
    }

    public function __construct()
    {
        if ( is_admin())
        {
            $this->database_theme_version = get_option( 'phox_version', false );
            $this->current_theme_version = WDES_THEME_VERSION;

            if ( false === $this->database_theme_version  ) {
                $this->fresh_installation();
            }
        }

    }

    /**
     * Actions to run on a fresh installation.
     */
    public function fresh_installation() {
        update_option( 'phox_version', $this->current_theme_version );
    }

}