<?php
// Copyright 1999-2024. WebPros International GmbH. All rights reserved.

namespace Plesk\MigrationLibV8\SiteImport;

use Plesk\MigrationLibV8\Common\Backend\Files\Progress;
use Plesk\MigrationLibV8\Common\SessionDirectory;
use Plesk\MigrationLibV8\SiteImport\Backend\Files\Config;
use Plesk\MigrationLibV8\SiteImport\Backend\Files\MigrationList;
use Plesk\MigrationLibV8\SiteImport\Backend\Files\SiteInfo;
use Plesk\MigrationLibV8\SiteImport\Backend\Files\Status;

class SessionObjects
{
    /** @var \Plesk\MigrationLibV8\SiteImport\SessionObjects[] */
    private static $_instances = [];
    /** @var \Plesk\MigrationLibV8\SiteImport\Backend\Files\Status|null */
    private $_status = null;
    /** @var \Plesk\MigrationLibV8\SiteImport\Backend\Files\Config|null */
    private $_config = null;
    /** @var \Plesk\MigrationLibV8\SiteImport\SessionFiles|null */
    private $_sessionFiles = null;
    /** @var \Plesk\MigrationLibV8\SiteImport\Backend\Files\MigrationList[]|null */
    private $_siteMigrationLists = [];
    /** @var \Plesk\MigrationLibV8\SiteImport\Backend\Files\SiteInfo|null */
    private $_siteInfo = null;
    /** @var \Plesk\MigrationLibV8\Common\Backend\Files\Progress|null */
    private $_progress = null;
    /** @var SessionDirectory|null  */
    protected $_session = null;

    /**
     * SessionObjects constructor.
     *
     * @param SessionDirectory $session
     */
    private function __construct(SessionDirectory $session)
    {
        $this->_session = $session;
    }

    /**
     * @param \Plesk\MigrationLibV8\Common\SessionDirectory $session
     * @return \Plesk\MigrationLibV8\SiteImport\SessionObjects
     */
    public static function getInstance(SessionDirectory $session)
    {
        if (!array_key_exists($session->getPath(), self::$_instances)) {
            self::$_instances[$session->getPath()] = new self($session);
        }

        return self::$_instances[$session->getPath()];
    }

    /**
     * @return \Plesk\MigrationLibV8\SiteImport\Backend\Files\Config
     */
    public function getConfig()
    {
        if (!$this->_config) {
            $configPath = $this->getSessionFiles()->getConfigPath();
            $this->_config = new Config($configPath);
            $this->_config->setDefaultSourceSectionName('web');
        }

        return $this->_config;
    }

    /**
     * @return \Plesk\MigrationLibV8\Common\Backend\Files\Progress
     */
    public function getProgress()
    {
        if (!$this->_progress) {
            $this->_progress = new Progress(
                $this->getSessionFiles()->getProgressPath()
            );
        }
        return $this->_progress;
    }

    /**
     * @return \Plesk\MigrationLibV8\SiteImport\SessionFiles
     */
    public function getSessionFiles()
    {
        if (!$this->_sessionFiles) {
            $this->_sessionFiles = new SessionFiles($this->_session);
        }
        return $this->_sessionFiles;
    }

    /**
     * @return \Plesk\MigrationLibV8\SiteImport\Backend\Files\Status
     */
    public function getSiteMigrationStatus()
    {
        if (!$this->_status) {
            $this->_status = new Status(
                $this->getSessionFiles()->getStatusFilePath(), $this->getConfig()->getSourceHost()
            );
        }
        return $this->_status;
    }

    /**
     * @param string|null $suffix
     * @return \Plesk\MigrationLibV8\SiteImport\Backend\Files\MigrationList
     */
    public function getSiteMigrationList($suffix)
    {
        if (!array_key_exists($suffix, $this->_siteMigrationLists)) {
            $this->_siteMigrationLists[$suffix] = new MigrationList(
                $this->getSessionFiles()->getSiteMigrationListPath($suffix)
            );
        }
        return $this->_siteMigrationLists[$suffix];
    }

    /**
     * @return \Plesk\MigrationLibV8\SiteImport\Backend\Files\SiteInfo
     */
    public function getSiteInfo()
    {
        if (!$this->_siteInfo) {
            $this->_siteInfo = new SiteInfo(
                $this->getConfig()->getSourceHost(), $this->getSessionFiles()->getSiteInfoFilePath()
            );
        }
        return $this->_siteInfo;
    }
}
